alastairtree / LazyCache

An easy to use thread safe in-memory caching service with a simple developer friendly API for c#
https://nuget.org/packages/LazyCache
MIT License
1.71k stars 159 forks source link

Implement MemoryCache.Trim() to allow explicit removal #171

Open judahr opened 2 years ago

judahr commented 2 years ago

[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.]

Problem: To remove expired items from the cache requires either explicitly doing it or requiring the system to execute a timer. A timer can be resource expensive. Keeping track of what needs to be removed duplicates the role of the cache expiration policy.

Solution 1: Implementing the MemoryCache.Trim() function would be a nice in-between allowing a program to call a single method to trim the size of a possibly overgrown cache. This allows the underlying cache to perform the work with information it is already tracking. This would not be another ExpirationMode.

Solution 2: Implement an explicit call that will tell the provider to go through and remove items that are expired. Rather than a timer, this can be called less frequently based on when the client application knows it is idle or has completed tasks. This would not be another ExpirationMode. It would simply expose to the client app what the ImmediateExpiration is already doing, but less frequently.

Background: Iterating over data and caching expensive calculation results so later iterations can use it. Because the data and calculations are time series, the actual cache duration can be short and the application can perform a trim at intervals based on the data.