bitfaster / BitFaster.Caching

High performance, thread-safe in-memory caching primitives for .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Any plans to evict expired items on the background?

alex-jitbit opened this issue · comments

First of all, Thanks for this gem. Really handy!

Any plans to "expire" items and remove them from the dictionary on the background? Like MemoryCache does.

Somewhat like a background thread/timer that will evict old items.

To set some context, LRU caches at most n items, and keeps them in memory as long as possible in order to maximize the chance of a cache hit. In other words, budget some fixed amount of memory for caching and try to maximize efficiency. TLRU is the same, but items have an explicit TTL and can therefore age out. So, expiry is only really applicable to TLRU.

I had considered adding a Trim method similar to MemoryCache.Trim(int percent) that could be hooked up to a background thread or some other event in the program like inactivity or memory pressure. It would work slightly differently for each cache:

  • LRU.Trim(int itemCount) trim the n least recently used items.
  • TLRU.Trim(int itemCount) trim all e=expired items, then the remaining n-e least recently used items, if any.

TLRU.Expire() would then be implemented as TLRU.Trim(0) to remove only expired items. You would then be able to explicitly choose whether to spin up a thread or hook into some other existing process, and how aggressively to trim.

The main reason Trim() doesn't exist is because I have never needed it in practice. Do you have a situation in mind where this is a deal breaker and LRU eviction doesn't work?

Do you have a situation in mind where this is a deal breaker

I'm just looking for a complete replacement for MemoryCache which is slower than yours