jellydator / ttlcache

An in-memory cache with item expiration and generics
MIT License
883 stars 115 forks source link

Add an option for cache to not expire until background refresh completes #95

Closed DavidMeu closed 1 year ago

DavidMeu commented 1 year ago

We have an issue where there's a big latency in the cache reloading. So we wish users to not suffer from that and that the cache can serve them while there's a

background cache reloading

Generally speaking it would be nice to have:

Auto cache refresh on expiration

swithek commented 1 year ago

Can you elaborate on this more? Also, have you tried using Start and Stop or DeleteExpired methods to evict expired items only when you need them to be evicted (e.g., when some background task finishes)?

DavidMeu commented 1 year ago

There's the

Loader interface that is used to load/lazily initialize missing cache items.

Which is great. I just wish it to auto run when an item has expired.

Also, have you tried using Start and Stop or DeleteExpired methods to evict expired items only when you need them to be evicted (e.g., when some background task finishes)?

I have. Although this not exactly what I need. Can you please elaborate on that?

swithek commented 1 year ago

You can find more information on that on the README page:

Even though the cache.Start() method handles expired item deletion well, there may be times when the system that uses ttlcache needs to determine when to delete the expired items itself. For example, it may need to delete them only when the resource load is at its lowest (e.g., after midnight, when the number of users/HTTP requests drops). So, in situations like these, instead of calling cache.Start(), the system could periodically call cache.DeleteExpired():

DavidMeu commented 1 year ago

Saw that. This is still not the paved path I need.

I need:

Auto cache refresh on expiration

i.e firing the loader func on expiration.

swithek commented 1 year ago

That sounds like a use case that's specific to your project and is out of scope of this library. I suggest you try combining DeleteExpired/OnEviction events and Get/Set.