Skip to main content

cached_method

The @cached_method decorator is the equivalent of functools.cached_property for methods. This means that each instance has its own cache, so that the caches get garbage collected as soon as the owning objects are. The main advantages of cached_method over applying functools.lru_cache directly to methods are

  1. the surrounding class need not be hashable,
  2. and the class objects are not collected in a global cache, extending their lifetime. This makes cached_method applicable to classes holding references to scarce resources such as GPU memory that you want to be freed as soon as possible. Furthermore, the decorator can cache the output of __hash__ because it does not hash the object itself for cache lookups.

Implementation-wise cached_method closely follows functools.cached_property though it eschews the internal locking, which is now considered a mistake. Since cached methods should be idempotent anyway, we just accept possibly calling the method multiple times in parallel with equivalent arguments if the object is used in multi-threaded contexts.

from cached_method import cached_method

class GPUVector:
    def __init__(self, data):
        # data is some smart tensor object as found in pytorch, tensorflow, etc.
        self.data = data

    # Only cache the 2 most-recently used norms
    @cached_method(maxsize=2)
    def norm(self, p=2):
        return (self.data ** p).sum() ** (1 / p)

    @cached_method
    def __hash__(self):
        # A costly GPU-to-CPU transfer, so we want to hash the result
        return hash(tuple(self.data.to_cpu()))

If you are working with small, hashable objects that do not have to be gargabe collected as soon as possible, consider the method hashing technique described in the Python FAQ. It gives you an easy way to control the total cache size and allows cache hits between equivalent-but-not-identical objects. Of course, caching on the class level means that objects stay live until you clear the cache manually, even if the cache is the last object referencing them.

Release files for cached_method 0.1.0

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

Source distribution (sdist)

Source distribution for cached_method 0.1.0
File Size Uploaded
cached_method-0.1.0.tar.gz 4.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cached_method 0.1.0
File Interpreter ABI Platform
cached_method-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.2 kB

Release files / cached_method-0.1.0.tar.gz

Download URL cached_method-0.1.0.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0958d0422a90e24c215fb61f9633fd9faf566c8f7d15478493989abef8699d73
BLAKE2b-256 checksum
How to use checksums
8557f5796619b29cfbbce98b6027f9f92461a9c159a373566106a11b2bab1e07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.26.0

Release files / cached_method-0.1.0-py3-none-any.whl

Download URL cached_method-0.1.0-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a71720f7cab565131bb5e4189b6ca5902635435e0e017d0fd1261712d8c3dc0c
BLAKE2b-256 checksum
How to use checksums
a369f4be248608758ebad3d5954bf468fc98a822c939afbb04f1eaaecfc6be1c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.26.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

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