Table of Contents

Memory Cache

Introduction

If you want to use IMemoryCache to cache the parsing of expressions, you can use this plugin. The advantage of this plugin is that you can set a custom cache expiration.

Installation and Usage

First, you will need to add a reference to NCalc.MemoryCache to your project.

dotnet add package NCalc.MemoryCache

After this, you will need DI in your project. See Dependency Injection for more info.

Add a IMemoryCache implementation to your project. In this example, we will use Microsoft.Extensions.Caching.Memory.

builder.Services.AddMemoryCache() //From Microsoft.Extensions.Caching.Memory
builder.Services.AddNCalc().WithMemoryCache(options =>
{
    options.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5); //The default value is 15 minutes.
})

After this, simply create expressions from IExpressionFactory. For more information see this article.