aliostad / CacheCow

An implementation of HTTP Caching in .NET Core and 4.5.2+ for both the client and the server
MIT License
848 stars 171 forks source link

InMemoryCacheStore max cache time and max cache size #230

Closed Batboa closed 4 years ago

Batboa commented 5 years ago

Has it been considered to add a maximum to the amount of time an item can be present in the InMemoryCacheStore? In this case we want to avoid caching any response for excessive amounts of time. Additionally has it been considered to add limits to the size of the InMemoryCacheStore? For this case we want to limit the total amount of memory our service will allocated towards http caching.

We've implemented these features for our use case, if there's interest in these features we'd like to take the time to contribute to the project.

aliostad commented 5 years ago

Hi,

Sorry for late reply. Yes that would be useful - only if you could explain the high level solution.

Cheers Ali

Batboa commented 5 years ago

Hello Ali,

Setting the maximum time a cached item can live should be a simple addition. The first step would be adding another property on the object of private readonly Timespan _maxCacheTTL = ... or something along that.

Then when deciding the expiration time of the item done here we add the logic if(optimalExpiry > maxTTL) optimalExpiry = maxTTL.

Batboa commented 5 years ago

As for setting a max size on the cache that would be done by a user specifying the max storage size in bytes. The System.Runtime.Caching has a field of CacheMemoryLimit that can be used. And for Microsoft.Extensions.Caching.Memory There is an option called Size.

When storing the entry in the cache we can get the length of the byte[] and use that to tell the cache the size of the data being added in the Microsoft.Extensions.Caching.Memory case.

aliostad commented 4 years ago

Sorry, I noticed I have not got back to you on this. You seem to be trying to override the cache directive for resources. Is that correct?

aliostad commented 4 years ago

@batboa With version 2.6.0, you are able to pass options to the InMemoryCacheStore:

        public InMemoryCacheStore(TimeSpan minExpiry, IOptions<MemoryCacheOptions> options) :
            this(minExpiry, new MemoryCache(options))
        {
            _options = options;
        }

Can you please confirm this solves your problem?