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

Evict callback for TTLCache #205

Closed
arossert opened this issue Mar 1, 2021 · 12 comments
Closed

Evict callback for TTLCache #205

arossert opened this issue Mar 1, 2021 · 12 comments

Comments

@arossert
Copy link

arossert commented Mar 1, 2021

I'm missing the option to set a callback when an item is evicted using the TTLCache.
I tried using the solution detailed here but it seems that the TTLCache is working a bit differently then the others and not calling the popitem() when entries are evicted by timeout.

Is there a way to get a callback when an item is deleted also by timeout?
I tried overriding the expire() function with no success.

@hellocoldworld
Copy link

hellocoldworld commented Mar 16, 2021

If I understand the docs correctly, items are only evicted by timeout when you try to access them but the item has expired. So, the method you would need to override is __getitem__

@tkem
Copy link
Owner

tkem commented Mar 16, 2021

@arossert Sorry, there is no way to get notified when a TTLCache item expires.

@arossert
Copy link
Author

@tkem Thanks for the update, it will be a coll feature to add 😃

@tkem
Copy link
Owner

tkem commented Aug 9, 2021

To take this into consideration, somebody would have to come up with a really good use case...

@tkem tkem closed this as completed Aug 9, 2021
@charliejacobsen
Copy link

Yes, it would be nice to have this @tkem. (It would also be nice if cachetools didn't use double underscores on member fields as this makes it less friendly to subclassing.)

I'm using TTLCache as trick to doing a TTL-based connection pool, and I'd like to close a connection as soon as it's evicted, rather than waiting till it is garbage collected.

@charliejacobsen
Copy link

eh, on second thought, there's too many edge cases to worry about (delitem, setitem, expire, and more). I'll stick with "wait for gc" and see if that suffices for me.

@jarekrutowicz
Copy link

To take this into consideration, somebody would have to come up with a really good use case...

I think there are many examples you can think of. For example waiting for a async request response with req_id key. If the response does not come within a timeout then evict the cache. I would like to know what requests I didn't know have a reply and were droped. It would be very usefull to be able at least to log that.

@arossert
Copy link
Author

arossert commented Feb 6, 2023

In my case, I'm using the TTLCache as a pool of connection tokens that will be evicted after some time, I would like to know if the pool is empty to alert that a new token needs to be generated (without polling on the pool).

@dberardo-com
Copy link

if TTLCache is not timer-based, then there is no event that one could "subscribe to" inside the code. Based on this comment: #205 (comment) i believe that this is not the case, and so i think it will be misleading to offer a "on('expired')" callback for TTLCache, as the frequency will not be deterministic.

Please correct me if i am wrong, because i also have a use case for using such function in TTLCache. And basically i dont want to handle a pool of Timers myself.

all in all, you could look at this library as an alternative depending on your use case: https://github.com/dbader/schedule

@jarekrutowicz
Copy link

i believe that this is not the case, and so i think it will be misleading to offer a "on('expired')" callback for TTLCache, as the frequency will not be deterministic.

How is that misleading? No matter what is the eviction policy I would like to know when the case was evicted. If the frequency is not deterministic then having a callback is actually more useful.

@dberardo-com
Copy link

i see your point, perhaps using good naming conventions could prevent misunderstandings, or perhaps a big disclaimer on the documentation of such an event hook could do the job :)

cheers

@arossert
Copy link
Author

arossert commented May 3, 2023

I can see this to be very useful

tkem added a commit that referenced this issue Aug 18, 2024
tkem added a commit that referenced this issue Aug 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants