Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Per item TTL in TTLCache #157

Closed
judahrand opened this issue Dec 20, 2019 · 5 comments
Closed

[feature] Per item TTL in TTLCache #157

judahrand opened this issue Dec 20, 2019 · 5 comments

Comments

@judahrand
Copy link

Would it be possible to implement a per-item TTL? This could still default to the cache's global TTL if not set.

@tkem
Copy link
Owner

tkem commented Dec 20, 2019

Sure, I played with the idea myself a few years ago... ended up with a custom set method that seemed too exotic to include. Anyway, most use cases I came across didn't really need a per-item TTL, but rather one of a limited set of predefined TTLs. Long-, mid- and short-lived items, for example. Putting these into different caches with different TTLs usually turned out to be a simpler solution.

@judahrand
Copy link
Author

Not something that you'd be keen to merge into master then?

My use case revolves around rate limiting. Specifically the combination of these two issues:

Part of the point of separating the rate limiter from the the storage backend in the Rush package is to remove the need for duplicating the functionally into an object per rate limiter. You could imagine this even more so with external stores like Redis where you might end up running multiple connections. Each of these rate limiters could potentially want different TTLs.

I happened across the TTLCache implementation in the package which if I'm not mistaken is very nearly a full TLRU caching algorithm. It just lacks the per key TTL. I'm very keen to use it in Rush to offer a more robust alternative to the current 'DictionaryStore'.

@tkem
Copy link
Owner

tkem commented Dec 26, 2019

Well, not quite ;-)
However, I've been toying with the idea of a TLRUCache in branch wip/tlru. It's basically replacing the numeric cache-wide TTL argument with a function, which receives the to-be-cached value as its sole argument. So the TTL/TTU can be a property of the value (e.g. derived from a cache header for an HTTP response) or some function "defined by the local network administrator according to local policies and requirements" as stated in https://arxiv.org/pdf/1801.00390.pdf.
I haven't put too much thought into that, yet, so I'd appreciate if you could share your thoughts on that approach!

@malversan
Copy link

Not directly related to the question, but...

That TLRUCache can be easily integrated into the current TTLCache implementation with no need of an additional class, and maintaining TTLCache backwards-compatible.

Just check in object creation if "self.__ttl" is "callable()", and take it into account when setting the inserted item expiration time.

eltoder pushed a commit to eltoder/cachetools that referenced this issue Aug 29, 2020
Another attempt at supporting per-item TTL values (tkem#157). Split TTLCache
into 3 classes:

* TTLCacheBase: doesn't have any fixed TTL values and doesn't implement
full MutableMapping ABC. Instead of __setitem__() it has add() which
requires a TTL value for each item. This can be used as a base class to
implement any TTL logic.

* TTLCache: inherits from TTLCacheBase and implements the same API as
before. For advanced uses add() can be used directly.

* FlexTTLCache: inherits from TTLCacheBase and accepts a function to
generate TTL values per element.
eltoder pushed a commit to eltoder/cachetools that referenced this issue Aug 29, 2020
Another attempt at supporting per-item TTL values (tkem#157). Split TTLCache
into 3 classes:

* TTLCacheBase: doesn't have any fixed TTL values and doesn't implement
full MutableMapping ABC. Instead of __setitem__() it has add() which
requires a TTL value for each item. This can be used as a base class to
implement any TTL logic.

* TTLCache: inherits from TTLCacheBase and implements the same API as
before. For advanced uses add() can be used directly.

* FlexTTLCache: inherits from TTLCacheBase and accepts a function to
generate TTL values per element.
eltoder pushed a commit to eltoder/cachetools that referenced this issue Aug 29, 2020
Another attempt at supporting per-item TTL values (tkem#157). Split TTLCache
into 3 classes:

* TTLCacheBase: doesn't have any fixed TTL values and doesn't implement
full MutableMapping ABC. Instead of __setitem__() it has add() which
requires a TTL value for each item. This can be used as a base class to
implement any TTL logic.

* TTLCache: inherits from TTLCacheBase and implements the same API as
before. For advanced uses add() can be used directly.

* FlexTTLCache: inherits from TTLCacheBase and accepts a function to
generate TTL values per element.
@tkem tkem added this to the v4.3.0 milestone Sep 26, 2021
tkem added a commit that referenced this issue Oct 3, 2021
tkem added a commit that referenced this issue Oct 11, 2021
tkem added a commit that referenced this issue Oct 14, 2021
tkem added a commit that referenced this issue Oct 14, 2021
@tkem tkem modified the milestones: v4.3.0, v5.0.0 Oct 20, 2021
tkem added a commit that referenced this issue Dec 17, 2021
tkem added a commit that referenced this issue Dec 17, 2021
tkem added a commit that referenced this issue Dec 18, 2021
tkem added a commit that referenced this issue Dec 18, 2021
tkem added a commit that referenced this issue Dec 18, 2021
tkem added a commit that referenced this issue Dec 19, 2021
tkem added a commit that referenced this issue Dec 19, 2021
tkem added a commit that referenced this issue Dec 19, 2021
@tkem tkem closed this as completed in 9b731f5 Dec 21, 2021
@dberardo-com
Copy link

is it possible to get the remaining TTL for a single item in the cache?

like cache.getRemainingTTL(key)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants