MichaCo / CacheManager

CacheManager is an open source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.
http://cachemanager.michaco.net
Apache License 2.0
2.34k stars 457 forks source link

SizeLimit support for Microsoft.Extensions.Caching.Memory #237

Open MichaCo opened 6 years ago

MichaCo commented 6 years ago

In version 2.x, Microsoft added a SizeLimit option to the MemoryCacheOptions and implemented a new eviction logic based off of that. If SizeLimit is set, each CacheEntry added to the MemoryCache has to have it's Size property defined.

Right now, CacheManager doesn't set Size of each CacheEntry´ because it obviously cannot predict or calculate the actual size (same reason why Microsoft did that change and remove the MemoryPressure based eviction). But that mean, if you configureSizeLimit` right now, you'll get the following hard exception:

Unhandled Exception: System.InvalidOperationException: Cache entry must specify a value for Size when SizeLimit is set.
   at Microsoft.Extensions.Caching.Memory.MemoryCache.SetEntry(CacheEntry entry)

To support SizeLimit for the Microsoft.Extensions.Caching cache handle, I came up with the following options:

If anyone has a better idea, let me know ;)