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

Allow cache_it* decorators to provide a way to cache instance methods #24

Open
SantjagoCorkez opened this issue Nov 10, 2014 · 2 comments

Comments

@SantjagoCorkez
Copy link

That'd be a good option to have cache_it() and cache_it_json() either auto-detecting instance methods or being marked of such a use case by, for example, strip_first_arg boolean parameter that'd perform something like

pickle.dumps(args[1:])

There's a side effect in such a use case, I know, but could be an option for those who know what they do.

In my case, using instance methods eliminates a need to call getLogger every time I need to log something and provides an option to use one, instance-wide, logger (which certainly cannot be pickled because of file descriptors inside any logger object).

@SantjagoCorkez SantjagoCorkez changed the title Allow cache_it* decorators do not provide a way to cache instance methods Allow cache_it* decorators to provide a way to cache instance methods Nov 10, 2014
@fjsj
Copy link
Contributor

fjsj commented Nov 10, 2014

Hi SantjagoCorkez,

I think you can't just strip self out from the cache_key, since different instances probably will return different values even for the same method. I believe this is the default use case.

Probably in your case you need something different from this, so I think you can create your own decorator around cache_it decorator. If you need help for doing this, please let me know.

@SantjagoCorkez
Copy link
Author

Well, in my case the solution just strips out the first function arg. No matter what is it: self, cls or any generic argument as in staticmethod. And this guarantees no side effects in my case, because instance's self contains no information that could cause difference between instance of class A(B) and class C(B), where B inits only logger.

As I've said, that could be specially marked in documentation as 'think twice when you use this argument'.

As for more generic solution this could be also implemented as strip_args=0 argument for decorator and pickle.dumps(args[strip_args:]) in code. That could help when one needs to exclude some N-first args of a function from cache entry's hash key.

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

2 participants