Closed mtfarkas closed 3 years ago
That's not a bug, you are using it wrong.
The signature for the Add
method is this:
void Add<T>(string key, T item, MemoryCacheEntryOptions policy);
So by calling Add
you are "adding an item to the cache" and not "specifying a factory to be used with a cache key for later retrieval".
So basically while you think you are specifying the factory for later use, you are really adding the factory itself (a lambda) to the cache as the item: then when, later on, you ask for the item in the cache it will give you back that item, which is the lambda itself.
Hope this helps.
ps: @alastairtree if I've said something wrong please correct me.
You're entirely right, but for some reason the IntelliSense in Visual Studio shows an overload that does take a Func
No worries! Even though this is not my lib now I'm curious about the situation you're talking about, will try to reproduce it out of curiosity. Best.
All good then, use the GetOrAdd method for the behaviour you are seeking.
Describe the bug If I add a
Func<T>
factory function to cache via theAdd
method, then later want to retrieve the value it provides viaGet
, the returned value will be aFunc<T>
instead of a value of typeT
.To Reproduce Add a
Func<T>
to cache via theAdd
method, then retrieve it immediately via theGet
method.Expected behavior The factory function is called and the resulting value is returned instead of the function itself.
Framework and Platform
Additional context N/A