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

Extending TTLCache, expire() does not call popitem() #292

Open
mbrancato opened this issue Nov 14, 2023 · 2 comments
Open

Extending TTLCache, expire() does not call popitem() #292

mbrancato opened this issue Nov 14, 2023 · 2 comments
Assignees

Comments

@mbrancato
Copy link

Before reporting a bug, please make sure you have the latest cachetools version installed:

pip install --upgrade cachetools

Describe the bug
For extending a class, the docs say all cache implementations call popitem() to evict items from the cache and indicate that overriding the popitem() can catch expiration from any class.

This does not work with TTLCache.

Expected result
Overriding popitem() should be called when an item is expired.

Actual result
The popitem() method is not called.

Reproduction steps

import time

from cachetools import TTLCache


class MyCache(TTLCache):
    def popitem(self):
        key, value = super().popitem()
        print('Key "%s" evicted with value "%s"' % (key, value))
        return key, value


c = MyCache(maxsize=20, ttl=2)
c["a"] = 1
time.sleep(1)
c["b"] = 2
time.sleep(4)
c["c"] = 3
@GianlucaFicarelli
Copy link

The report seems related to #205 and #103

If it's not possible or planned to add a notification callback when an item is removed because expired in TTLCache, it could be useful to make it more explicit in the docs.

@tkem
Copy link
Owner

tkem commented Jan 23, 2024

Thanks @GianlucaFicarelli for pointing to the closed issues!

This use case seems to be more popular than I thought, so I might think about it again.
Maybe expire() returning a list of evicted keys, so you can override it in a derived class and act on the result of the base class method.
But don't expect anything too soon ;-)

But sure, the docs should definitely be improved in this respect.

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

3 participants