Skip to main content

functools.cache() for methods, done correctly

Project description

methodic_cache

codecov

functools.cache() for methods, done correctly.

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

methodic_cache-0.3.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

methodic_cache-0.3.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file methodic_cache-0.3.0.tar.gz.

File metadata

  • Download URL: methodic_cache-0.3.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.1 Darwin/22.3.0

File hashes

Hashes for methodic_cache-0.3.0.tar.gz
Algorithm Hash digest
SHA256 6701a793a5cd72c2a5fdca9155ae0812f8905634b877fa7c493dd16bb96e6c2f
MD5 cd847cd0618d5f212331ed05ede61e0a
BLAKE2b-256 2bc47353f505f9762e1005ca40daaf626bed1814fedbaba24db62f7c5ee36371

See more details on using hashes here.

File details

Details for the file methodic_cache-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: methodic_cache-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.1 Darwin/22.3.0

File hashes

Hashes for methodic_cache-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 258bdc83b8434e69a6522aa7deca485480fdef88eba2a666f7ea508f4df2b559
MD5 a0d91443001747a6a8a5681d292602c7
BLAKE2b-256 57b5743adeb0d569d4eb5ae0b4a926977010618bf37a44df6ad8fa195491ab3c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page