Table of Contents

Memory Cache

Introduction

If you want to use IMemoryCache to cache the parsing of expressions, you can use this plugin. This is the better option when you want explicit cache expiration, eviction policy control, or a cache that is managed by the application's IMemoryCache infrastructure instead of the built-in default cache.

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.