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

Cache miss each time if used on class instance method #8

Closed
darklow opened this issue Sep 23, 2018 · 5 comments
Closed

Cache miss each time if used on class instance method #8

darklow opened this issue Sep 23, 2018 · 5 comments

Comments

@darklow
Copy link

darklow commented Sep 23, 2018

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):
      #....
@poleha
Copy link
Contributor

poleha commented 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

@peterbe
Copy link
Owner

peterbe commented Nov 3, 2018

+1 on method_decorator

@darklow darklow closed this as completed Nov 21, 2018
@matt-dalton
Copy link

The only downside with this is that you then can't use the hot cache pattern. e.g.

MyClass.get_data(MyClass, refresh=True)

gives get_data() takes 1 positional argument but 2 were given

Is there a workaround you can think of here?

@matt-dalton
Copy link

Ahh actually, in my case this works if I add in an args_rewrite=lambda: 'manager_fn', which is fine because I want to cache the result globally across a model

@fjsj
Copy link

fjsj commented Dec 9, 2021

I suggest documenting the method_decorator trick. Can I open a PR with that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants