Skip to content

cache_on_arguments not taking class member variables or class object into consideration #214

Discussion options

You must be logged in to vote

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

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by anandtripathi5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #213 on December 22, 2021 13:53.