ZiggyCreatures / FusionCache

FusionCache is an easy to use, fast and robust hybrid cache with advanced resiliency features.
MIT License
1.8k stars 94 forks source link

[FEATURE] Ability to completely empty/clear an IFusionCache à la MemoryCache#Compact() #130

Closed QuantumToasted closed 1 year ago

QuantumToasted commented 1 year ago

Is your feature request related to a problem? Please describe. Microsoft.Extensions.Caching.Memory includes a MemoryCache type commonly used in situations which call for IMemoryCache. This type features a method, Compact():

/// Remove at least the given percentage (0.10 for 10%) of the total entries (or estimated memory?), according to the following policy:
/// 1. Remove all expired items.
/// 2. Bucket by CacheItemPriority.
/// 3. Least recently used objects.
/// ?. Items with the soonest absolute expiration.
/// ?. Items with the soonest sliding expiration.
/// ?. Larger objects - estimated by object graph size, inaccurate.
public void Compact(double percentage)
{ ... }

If one were to ever perform a massive arbitrary update, add, or delete action to your database (or other backing store), the cache could become potentially filled with tens, hundreds, or maybe even thousands of stale entries, which could be a problem.

Describe the solution you'd like A method or option to somehow evict arbitrary entries in the cache without knowing their keys, either completely (100% empty) or possibly using similar rules as MemoryCache#Compact(), allowing a cache to be partially emptied based on entry age, size, or recent usages. It may also be beneficial to be able to evict all entries of a certain type or types?

Describe alternatives you've considered I absolutely could just be unaware of a way to do this already, as I just started using FusionCache a few weeks ago to replace my overengineered caching service in my project.

Additional context N/A

jodydonetti commented 1 year ago

Hello @QuantumToasted and thanks for using FusionCache!

The design of FusionCache is currently based on the core IMemoryCache and IDistributedCache interfaces, so all the features available must also be available (or implementable) with the features available on those interfaces.

Sadly, Clear() is not available there, therefore it is not possible to build this feature.

ps: I have some plans to explore a different design in the future, after v1.0 will hit the road. That moment will come quite soon, so stay tuned for more 😉

davidhenley commented 1 year ago

Awesome. Thanks @jodydonetti