question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cache invalidation for class or instance methods

See original GitHub issue

Migrated 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:open
  • Created 11 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
sqlalchemy-botcommented, Nov 24, 2018

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.

0reactions
peacock0803szcommented, Dec 17, 2021

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 of invalidate(). Like this: MyClass().count_up.invalidate(None, 1) ref: https://gist.github.com/peacock0803sz/1dd640d91543dbd6c94ce234ba603077

It looks tough, but I hope I can fix it well. Many thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cache invalidation - HCL Product Documentation
Dynacache provides different methods for performing cache invalidation. There are four methods to invalidate the cache.
Read more >
Precise Method and Constant Invalidation in TruffleRuby
In this blog post, we explore precise method and constant invalidation which reduces the number of deoptimizations in typical Ruby workloads ...
Read more >
Is there a canonical way to cache instance methods in python?
It seems simple enough -- and works fine. class memoized(object): '''Decorator. Caches a function's return value each time it is called.
Read more >
Automating Cache Invalidation With Change Data Capture
Naturally, if a cached entity gets modified, the corresponding cache entry must be updated (or purged from the cache), too. As long as...
Read more >
Cache invalidation through a setter or service? - java
Clarification: My job running every second calls, if the cache is invalid, a method that retrieves the Person objects from the database, builds ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found