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 miss each time if used on class instance method

See original GitHub issue

Looks like self param is being taken into account. Current workaround that works is using empty args_rewrite list:

class MyClass(object):

    @cache_memoize(30, args_rewrite=lambda x: [])
    def get_data(self):
      #....

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
polehacommented, Oct 12, 2018

That’s not issue but expected behavior. I see at least two correct ways to handle this situation:

@cache_memoize(30, args_rewrite=lambda self, *args: args)
def get_data(self):
    pass

or

from django.utils.decorators import method_decorator


@method_decorator(cache_memoize(30, prefix='get_data'))
def get_data(self):
    pass
1reaction
peterbecommented, Nov 3, 2018

+1 on method_decorator

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python functools lru_cache with instance methods
With a single cache per method, you can set the maxsize so that the total memory use is bounded regardless of the number...
Read more >
Don't wrap instance methods with 'functools.lru_cache ...
Recently, fell into this trap as I wanted to speed up a slow instance method by caching it. When you decorate an instance...
Read more >
Method caches broken between `it` · Issue #2194 - GitHub
Hi, I noticed that RSpec seems to be breaking method caches between it ... It appears that every time you use instance_exec on...
Read more >
3 Common Mistakes when Implementing Spring Cache ...
1. Invocation of cacheable methods from the same class ... In Spring boot, the default advice mode for processing caching annotation is “proxy”....
Read more >
C++ Instance Methods Cache Friendly? - C++ Forum
If we assume the simpler case, the cache behavior will be optimal. A cache that misses simply because it has to fetch memory...
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