zzzprojects / Eval-Expression.NET

C# Eval Expression | Evaluate, Compile, and Execute C# code and expression at runtime.
https://eval-expression.net/
Other
449 stars 86 forks source link

Eval.Execute Fails if Cache SizeLimit Set #131

Closed stonstad closed 2 years ago

stonstad commented 2 years ago

EvalManager.Cache = new MemoryCache(new MemoryCacheOptions() { SizeLimit = 100 }); Eval.Execute(...)

// System.InvalidOperationException: 'Cache entry must specify a value for Size when SizeLimit is set.'

JonathanMagnan commented 2 years ago

Thank you for reporting,

We will look at this to set the Size now

Best Regards,

Jon

stonstad commented 2 years ago

Jon, can you share what the default cache policy is? Is the cache expired by duration, or weight? i.e. how long is each entry stored?

https://eval-expression.net/eval-manager

stonstad commented 2 years ago

@JonathanMagnan I'm testing version .82 and this is a question that we are wondering about (above). When an item is entered into the cache, what is the duration or weight used so that we can understand how long the cache is populated? Thanks.

JonathanMagnan commented 2 years ago

Hello @stonstad ,

A full answer will be provided later today as a new version will be released for this issue and which will change the current answer.

JonathanMagnan commented 2 years ago

Hello @stonstad ,

The v4.0.83 has been released.

Can you share what the default cache policy is

The default cache policy had no option at all before.

Here is the current new cache policy introduced with the fix for this issue:

new MemoryCacheEntryOptions() { Size = 1 }

So only the Size is set to fix the fix issue when someone specifies a SizeLimit

How to change the cache policy?

Since the v4.0.83, it is now possible to change the cache policy using the MemoryCacheEntryOptionsFactory options such as:

EvalManager.DefaultContext.MemoryCacheEntryOptionsFactory = () => new MemoryCacheEntryOptions() { Size = 2 };

// or for a specific context
var context = new EvalContext();
context.MemoryCacheEntryOptionsFactory = () => new MemoryCacheEntryOptions() { Size = 2 };

You are not limited to the Size option, you can specify any option.

Let me know if that fixes correctly this issue.

Best Regards,

Jon

stonstad commented 2 years ago

Perfect, thank you! Please feel free to close this issue.