aspnet/Caching

Size hint based eviction for MemoryCache

JunTaoLuo opened this issue · 3 comments

Following up on #325.

MemoryCache does not know the size of the items it caches but the user of MemoryCache may know the size. In those cases we want to provide a method for size based eviction to guarantee a limit on the memory footprint of the cache. We can also add an option to require size hint when adding an item to the memory cache. Having a size hint also gives us the opportunity to investigate different eviction policies.

Recommended for 2.1

Some notes from our discussion:

  • Make sure we don't register MemoryDistributeCache on our components.
  • Deprecate/remove the AddDistributedMemoryCache extension method. (Also update the comment to reflect that if this method is used, the cache will be shared among all IDistributedCache consumers).
  • Force a size limit on DistributedMemoryCache
  • Have each of our components on the framework that consume IDistributedCache create their own private version of the DistributedMemoryCache if no implementation is registered. The configuration on the limits for these instances can be exposed through named options or through some wrapper/new extension method on a case by case basis.
  • Have the MemoryDistributedCache tell the size of the entries to the underlying MemoryCache when adding entries.

Sample new extension method for session:

services.AddInMemorySession(options => options.MaxSize = 10 * 1024 * 1024);

Sample configuration through named options for the DistributedCacheTagHelper

services.AddMvc();
services.Configure(Some.Constant, options => options.MaxSize = 10 * 1024 * 1024);

@Tratcher @davidfowl @JunTaoLuo

@javiercn the updated POR is collected in #329