Skip to main content

methodic_cache

codecov

functools.cache() for methods, done correctly.

methodic_cache.cached_method is a decorator that caches the return value of a method, based on the arguments passed to it.

The peculiarity of this library is that it does not store anything on objects themselves, but rather on a separate WeakKeyDictionary where the lifetime of the cache matches the lifetime of the object.

An advantage of this approach over storing the cache on the object itself when needed is that objects will keep their memory footprint smaller thanks to shared key dictionaries. See PEP 412 and The Dictionary Even Mightier - Brandon Rhodes at PyCon 2017, 00:21:02 for more details.

Features

Installation

pip install methodic_cache

Usage

from methodic_cache import cached_method


class MyClass:
    @cached_method
    def my_method(self, arg1, arg2):
        return arg1 + arg2


my_obj = MyClass()
my_obj.my_method(1, 2)  # returns 3
my_obj.my_method(1, 2)  # returns 3 from the cache

Using classes with __slots__

Classes that define __slots__ need to have a __weakref__ slot to be able to be weakly referenced:

from methodic_cache import cached_method


class MyClass:
    __slots__ = ("my_attr", "__weakref__")  # <-- __weakref__ is required

    def __init__(self, my_attr):
        self.my_attr = my_attr

    @cached_method
    def my_method(self, arg1, arg2):
        print(f"Computing {self.my_attr} + {arg1} + {arg2}...")
        return self.my_attr + arg1 + arg2

my_obj = MyClass(1)
my_obj.my_method(2, 3)
# prints "Computing 1 + 2 + 3..."
# returns 6
my_obj.my_method(2, 3)
# returns 6

Custom cache backends

You can use any cache backend that implements the MutableMapping interface (e.g. dict, lru_cache, functools.lru_cache, etc.). The default cache backend is cachetools.Cache(maxsize=math.inf), which will keep the cache bounded to the lifetime of the self object.

You can use a different cache backend by passing it as the cache_factory argument to cached_method:

from methodic_cache import cached_method
from cachetools import LRUCache


class MyClass:
    @cached_method(cache_factory=lambda: LRUCache(maxsize=1))
    def my_method(self, arg1, arg2):
        print(f"Computing {arg1} + {arg2}...")
        return arg1 + arg2


my_obj = MyClass()
my_obj.my_method(1, 1)
# prints Computing 1 + 1...
# returns 2
my_obj.my_method(1, 1)
# returns 2
my_obj.my_method(2, 2)
# prints Computing 2 + 2...
# returns 4
my_obj.my_method(1, 1)  # <-- this will be recomputed because the cache is full
# prints Computing 1 + 1...
# returns 2

Metadata

Release files for methodic-cache 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for methodic-cache 0.3.1
File Size Uploaded
methodic_cache-0.3.1.tar.gz 5.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for methodic-cache 0.3.1
File Interpreter ABI Platform
methodic_cache-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.5 kB

Release files / methodic_cache-0.3.1.tar.gz

Download URL methodic_cache-0.3.1.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
54b2355f49260673e73309634cb06d86e5b9c15bd7639967608a3e0424ba198a
BLAKE2b-256 checksum
How to use checksums
a52e7d191abcb41b0942e64096371b6fe4c942f6cfbac8b8a63a7cf32ebac293
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.11.1 Darwin/22.3.0

Release files / methodic_cache-0.3.1-py3-none-any.whl

Download URL methodic_cache-0.3.1-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
81eae57ee826b735bdb5464efccdd4e6e09050e4e54e2d87bddf2ce705389fa2
BLAKE2b-256 checksum
How to use checksums
256afbab1056959c57a0c7eb8482a8fc890aabaae7b9e82b28c1c1a889b04867
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.11.1 Darwin/22.3.0

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page