-
Notifications
You must be signed in to change notification settings - Fork 163
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
Invalidating cached values in "cachemethod" #218
Comments
There is an example for removing individual cache items using |
AFAICS this is also quite similar to #176. |
I am in similar situation. Is there any way to delete a specific cache key from TTLCache. The key in TTLCache is stored as TTLCache({(<class 'cachetools.keys._HashedTuple'>, 'token', 'd4e498d636e76dd3ea19fb727dc8f828'): TokenClass object at 0x000001F939670250>} How do I reconstruct this cache key to pop its value? token_function.cache.pop(token_function.cache_key(?), None) When I tried to reconstruct the tuple to pass it as a key, it raises token_function.cache.pop(token_function.cache_key('token', 'd4e498d636e76dd3ea19fb727dc8f828'), None) I'm not able to reconstruct the first element of this tuple |
@infohash: Are you talking about the
|
I am using from cachetools import TTLCache, cachedmethod
class TokenClass:
def __init__(self):
self.cache = TTLCache(maxsize=128, ttl=300)
@cachedmethod(cache=lambda self: self.cache)
def token_function(self, token):
... It works fine for storing the cache, the problem happens when I try to remove a specific key from it. I have changed my design so I don't need to pop a key from TTLCache anymore. But it's something you can check because it almost works. |
So, the
It's cumbersome, and I'm not too happy with it myself, so it will probably remain an experimental, undocumented feature until someone comes up with something better (or somebody provides sensible documentation for this)... |
@tkem I have solved it finally. So if the method is called with the keyword argument, the cache_key must also take the keyword argument. tc.token_function.cache(tc).pop(tc.token_function.cache_key(tc, token=d4e498d636e76dd3ea19fb727dc8f828'), None) |
@infohash: Glad you solved it. Yes, documentation should (and eventually, will) be added for this. |
@tkem, @ShivKJ, I've implemented a simple |
|
Hi,
Is there a way to update/remove cache value returned by
cachemethod
?For example, in the below code,
Suppose, I want to remove cached value for a given
i
from cache, then it is not possible to do directly.Meanwhile, I learned about
hashkey
function inkeys
module. But it is not exposed in cachetools__init__
, so can not be imported (of course there are hacks around it). Using this, we can updated cached value.So, I think there could be two solutions to it,
cachemethod
wraps a method to enable caching feature in it, so apart from the caching feature, it can add functionality to invalidate. For example,keys
module in cachetools__init__.py
.I think approach 1 would be more user friendly.
The text was updated successfully, but these errors were encountered: