Table of Contents

Caching

When <xref:NCalc.Expression.Evaluate> is called on an expression, it is parsed once. If the same expression is reused, parsing is skipped and only the expression tree is evaluated again.

Each parsed expression is cached internally, which means you do not need to manually reuse an Expression instance for the parser cache to help.

The default cache now keeps strong references and evicts the least recently used entries when it reaches its capacity. This is more predictable than a weak-reference cache and avoids scanning the whole dictionary on every insert.

You can disable caching at the framework level by adding NoCache at your global expression context.

MyContext.Value.Options = ExpressionOptions.NoCache;

You can also tell a specific Expression instance not to use the cache.

var expression = new Expression("1 + 1", ExpressionOptions.NoCache);

Default cache size

The default cache size is 128 entries.

If you are on modern .NET runtimes (.NET 8+) you can override it before the first expression is parsed by setting an AppContext value:

AppContext.SetData("NCalc.LogicalExpressionCache.DefaultCapacity", "256");

The value must be a positive integer string. Set it during application startup so the default singleton cache picks it up before first use.

You can customize the cache implementation using Dependency Injection.

If you need expiration control or want the cache to be governed by IMemoryCache, use the Memory Cache plugin instead of the built-in cache.