LazyCache
LazyCache copied to clipboard
Provide a way to set the MemoryCacheEntryOptions size dynamically, based on the size of the entry
[Use the Thumbs Up reaction to vote for this feature, and please avoid adding comments like "+1" as they create noise for others watching the issue.]
Currently I'm using LazyCache with MemoryCache. The way this is implemented it looks like I need to add a new parameter to the cache.GetOrAdd() call. This doesn't have visibility to the section of code that is fetching the entry, so I'm not able to add a size based on the size of the entry. I can only create a static size for every entry in that call.
Something like:
Result result = _cache.GetOrAdd(cacheKey,
() =>
{
var tempResult = _bll.GetSomeVariableSizedResultObject();
return tempResult;
},
entry =>
{
return new MemoryCacheEntryOptions
{
Size = entry.Size
}
});
This issues was discussed a bit in #38. In summary LazyCache will support Size using the MemoryCacheEntryOptions param as you have shown, but dynamically assigning size inside the lazily evaluated callback is not possible. Although it is possible to access the ICacheEntry
options by taking a parameter in your lazy callback, the size option is applied too late and the underlying MemoryCache cache will not use the size as you expect.
This is caused by the way a Lazy is used to defer execution and ensure the callback gets called only once so there is not any simple fixes. The only fixes I can think of are to use a custom implementation of MemoryCache (big job), or to not use Lazy and instead use more aggressive locking (poor performance).
If you really need dynamic sizing I think your best bet would just be to use MemoryCache directly which supports dynamic sizing and not use LazyCache, sacrificing (or reimplementing) the LazyCache locking.
I have a similar requirement but around the expiration date as I wish to cache an authentication token, the token will tell me when it will expire therefore I need to specify the expiration as a lambda that takes the fetched token as its input.
I have a similar requirement but around the expiration date as I wish to cache an authentication token, the token will tell me when it will expire therefore I need to specify the expiration as a lambda that takes the fetched token as its input.
If you want to change the expiration based on a result from inside the cached delegate, then that's already supported and in the docs. Check out https://github.com/alastairtree/LazyCache/wiki/API-documentation-(v-2.x)#get-or-add-an-item-to-the-cache-for-duration-defined-when-you-generate-the-item-to-cache