Would methodtools or functools's lru_cache work in nameko service class?

eg:

class Service1(object):

name = 'service1'

# Initialize Dependencies
inferservice_rpc = RpcProxy('inferservice')
config = Config()
storage = Storage()

@event_handler('etc', 'mg_results')
def infer(self, message):
    uuid = message['uuid']
    tenant_id = message['tenant_id']
    parameters = message['parameters']

    r = self.getCachedRecommender(tenant_id)
    ...

@lru_cache(maxsize=1)
def getCachedRecommender(self, tenant_id):
    LOG.debug("Looking for tenant_id: {}".format(tenant_id))
    return self._get_recommender(tenant_id)

I tried it… but it seems like i get a new cache instance every time… meaning… i keep seeing the debug message. in getCachedRecommender()

I tested a simple class with above logic and it works fine but not in nameko.

Nameko instantiates the service class for every worker, so that’s why you’re getting a new instance of the cache every time.

There are patterns to cache method calls that work though. Check out https://cachetools.readthedocs.io/en/stable/#cachetools.cachedmethod. With cachemethod the cache is created at the module level instead of inside the decorator.

Thanks ! It work… initially it did not as i was stupid and just copied the code from your link. It turns out, i just need to initialize the dict object at the module level and it worked flawlessly !

1 Like