Cache invalidation for class or instance methods
See original GitHub issueMigrated issue, originally created by David Beitey (davidjb)
When attempting to invalidate or set a value for a class or instance method that’s being cached via CacheRegion.cache_on_arguments
, the first argument passed to the decorated function’s invalidate
or set
methods is ignored. For example, if I have
#!python
class Foo(object):
@cache.cache_on_arguments()
def do_something(self, text):
return text + 'dummy'
then if one wants to clear or set the cache for this method, they need to do this:
#!python
foo = Foo()
cached = foo.do_something('asdf')
foo.do_something.invalidate(anything_goes, 'asdf')
foo.do_something.set('value', anything_goes, 'asdf')
as the first argument passed into either function is ignored as part of the cache key. This is probably expected behaviour for now, but I think the situation should be documented either way.
Since the first argument going into those invalidate/set is ignored regardless by the key generator, I think it would be easier to have the key generator use all arguments if the decorated method belongs to a class or instance. This would be far less prone to error as I’ve found when trying to clear such a cached method & forgetting the first argument.
Issue Analytics
- State:
- Created 11 years ago
- Comments:10
Michael Bayer (zzzeek) wrote:
yeah the issue is how to handle a user-defined key generation function, particularly one that is dependent on actually handling the “self” or “cls” argument. Also the region only accepts a single function as the key generator.
im also not even sure i find the “region-level key generation function” to be very useful, the other day i really wanted one that works at the per-function level, though there seemed to be other awkwardnesses with that. so in general, revisiting how we generate keys from these functions and allow customization is something that needs to happen.
Hi, maintainers.
I found and reproduced this. Researching with the big help of @aodag, I discovered that it is a very deep-rooted problem.
But, he found a work-around for just avoiding simply. It works when passed
None
as the first argument ofinvalidate()
. Like this:MyClass().count_up.invalidate(None, 1)
ref: https://gist.github.com/peacock0803sz/1dd640d91543dbd6c94ce234ba603077It looks tough, but I hope I can fix it well. Many thanks.