cache_on_arguments not taking class member variables or class object into consideration #214
Answered
by
zzzeek
anandtripathi5
asked this question in
Usage Questions
-
So lets say I'm having a below example
So if I call
|
Beta Was this translation helpful? Give feedback.
Answered by
zzzeek
Dec 22, 2021
Replies: 1 comment
-
so it depends on how prevalent this pattern is, it might be most expedient to use region.get_or_create() directly instead if it's only in a few places. however if you want cache_on_arguments to take things for "self" into account you can use function_key_generator: Class A:
def __init__(self, param_1, param_2):
self.param_1 = param_1
self.param_2 = param_2
def _my_key_generator(namespace, fn, to_str=str):
# this is hardcoded to your fn below, however use inspect.getargspec(fn)
# to build a cache key dynamically
def generate_key(self, param):
return f"{namespace}:{self.param_1}:{self.param_2}:{param}"
return generate_key
@region.cache_on_arguments(function_key_generator=_my_key_generator)
def a(self, param):
return param*self.param_1*self.param_2 |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
anandtripathi5
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
so it depends on how prevalent this pattern is, it might be most expedient to use region.get_or_create() directly instead if it's only in a few places. however if you want cache_on_arguments to take things for "self" into account you can use function_key_generator: