Closed Batboa closed 4 years ago
Hi,
Sorry for late reply. Yes that would be useful - only if you could explain the high level solution.
Cheers Ali
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
.
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.
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?
@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?
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.