In my company’s project, we needed isolated caches for each instance. After researching existing implementations, we adopted cached_method.
Implementation: cached_method/cached_method.py at main · martenlienen/cached_method · GitHub
The cached_method behaves as follows:
-
The decorated method generates a cache instance upon the first access, similar to a cached property.
-
The cache instance is created by wrapping the bound method with
lru_cache, meaning thatselfis not included in the cache key. -
When wrapping the bound method with
lru_cache, weak references are used to prevent circular references. However, even without using weak references, memory leaks do not occur.
Our project is a web API server that utilizes multithreading. When an instance occupied by a thread uses cached_method, its cache is completely isolated from other threads too. This design is simple and robust, and since the cache is released immediately when the instance is released, there is little concern about worsening memory pressure.