# Would methodtools or functools's lru\_cache work in nameko service class?

**URL:** <https://discourse.nameko.io/t/would-methodtools-or-functoolss-lru-cache-work-in-nameko-service-class/471>\
**Category:** Uncategorized\
**Created:** [July 21, 2019, 5:53am UTC](https://discourse.nameko.io/t/would-methodtools-or-functoolss-lru-cache-work-in-nameko-service-class/471 "2019-07-21T05:53:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![coddericko](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/coddericko/32/16_2.png) [@coddericko](https://discourse.nameko.io/u/coddericko)\
**Post date:** [July 21, 2019, 5:53am UTC](https://discourse.nameko.io/t/would-methodtools-or-functoolss-lru-cache-work-in-nameko-service-class/471/1 "2019-07-21T05:53:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mattbennett](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/mattbennett/32/13_2.png) [@mattbennett](https://discourse.nameko.io/u/mattbennett)\
**Post date:** [July 22, 2019, 7:55am UTC](https://discourse.nameko.io/t/would-methodtools-or-functoolss-lru-cache-work-in-nameko-service-class/471/2 "2019-07-22T07:55:47Z")

</div>

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](https://cachetools.readthedocs.io/en/stable/#cachetools.cachedmethod). With `cachemethod` the cache is created at the module level instead of inside the decorator.

---

<div class="post-metadata">

**Author:** ![coddericko](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/coddericko/32/16_2.png) [@coddericko](https://discourse.nameko.io/u/coddericko)\
**Post date:** [July 22, 2019, 8:21pm UTC](https://discourse.nameko.io/t/would-methodtools-or-functoolss-lru-cache-work-in-nameko-service-class/471/3 "2019-07-22T20:21:51Z")

</div>

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 !
