neon-sunset / fast-cache

The fastest cache library written in C# for items with set expiration time. Easy to use, thread-safe and light on memory.
https://www.nuget.org/packages/FastCache.Cached/
MIT License
88 stars 8 forks source link

CleanCache #44

Closed troepolik closed 1 year ago

troepolik commented 1 year ago

Is it possible to clean cache? I didn't find method like Cached<T>.Clear()

troepolik commented 1 year ago

O, I guess it is CacheManager.Trim<TKey, TValue>(100);

neon-sunset commented 1 year ago

Hi! Cache items are evicted automatically. However, you can induce unscheduled full eviction (removes expired) or full clear (removes all) as following:

// Assuming you have a type SessionToken keyed with string
using FastCache.Services;

// Remove all items
CacheManager.QueueFullClear<string, SessionToken>();

// Remove expired
CacheManager.QueueFullEviction<string, SessionToken>();

Let me know if you have a scenario where automatic eviction performs sub-optimally - it might be a design oversight.

As for CacheManager.Trim<K, V>(), it will not be as efficient at fully clearing the cache since it is optimized for reducing the cache item count by fixed percentage or save-with-limit operations - it can either schedule trim or run it inline if below certain threshold count.

troepolik commented 1 year ago

Thank you for fast detailed response. Yes I have couple scenario of clear cache 1) to benchmark misses (I have to clear cache before each iteration of benchmark) 2) business scenario : I have event in the queue like "some configuration changed (by user)" that I have to handle in other services by clear some caches (all possible keys for <TKey, TValue>)

and unfortunately now I see CacheManager.QueueFullClear<string, SessionToken>(); preforms clear in a background. It would be better to have separate method without Task.Run

neon-sunset commented 1 year ago

I see, in case you are using BDN, [IterationSetup] will force it to run in Monitoring mode producing inconsistent / possibly not representative numbers (unless you are doing IO bound or otherwise long-running work). For the second scenario, while my library will work, it is designed for "cache with timed expiration" (i.e. expiration time is the discriminator between valid / not valid cache items) specifically to achieve low overhead, aside from the cost of using NonBlocking itself. In other scenarios, it is either expected to either remove an item or a range of items specifically (CachedRange<V>.Remove(K[] keys)) or update an item or a range accordingly.

The queuing limitation comes from the nature of being a static cache where only one eviction operation can run per cache generic variant at a time. It is not designed to be constantly cleared manually, although, for benchmarking purposes, you could try the following snippet:

// Slow
foreach (var cached in CacheManaged.EnumerateEntries<K, V>())
{
  cached.Remove();
}

// Or, if you know which items you have added
CachedRange<V>.Remove(keys);

with the assumption that you want to remove not yet expired items added by benchmark fixture.

troepolik commented 1 year ago

"with the assumption that you want to remove not yet expired items added by benchmark fixture." No, I want remove just all of them. like CacheManager.QueueFullClear<string, SessionToken>(); but without queue like CacheManager.Clear<string, SessionToken>(); would be great now I have workaround for benchmark: on start: Environment.SetEnvironmentVariable("FASTCACHE_INLINE_TRIM_COUNT_THRESHOLD", uint.MaxValue.ToString()); and clear by: CacheManager.Trim<TKey, TValue>(100); ))

neon-sunset commented 1 year ago

If it works for you, it's just an opinionated library that focuses on perf + being terse to use :D

neon-sunset commented 1 year ago

I'm going to close this issue as answered. Let me know if you have any questions.

troepolik commented 1 year ago

Any way, I've created pr for give us ability to call awaitable Full clean. To be sure it was cleared. Could you be so kind to approve it or give me feedback what can I fix in it to merge?