Skip to content

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

Closed

Description

@anandtripathi5

So lets say I'm having a below example

Class A:
    def __init__(self, param_1, param_2):
        self.param_1 = param_1
        self.param_2 = param_2
    @region.cache_on_arguments()
    def a(self, param):
        return param*self.param_1*self.param_2

So if I call
A(1,2).a(2)
this will cache the function result for other objects too like

A(3,4).a(2)
A(5,6).a(2)

Activity

  1. zzzeek commented on Dec 22, 2021

    @zzzeek
    Member

    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
  2. locked and limited conversation to collaborators on Dec 22, 2021
  3. converted this issue into a discussion #214 on Dec 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions