Skip to main content

contextcache

CI

Cache a python function only in certain contexts.

Usage

Here's an example:

cat example.py     
import contextcache

# Define a private CacheContextVar to store the cached values. Don't touch this CacheContextVar from anywhere else!
# You need to define a separate CacheContextVar for every function for which you want to enable caching.
# Use `None` as the default.
_double_cache = contextcache.CacheContextVar("double_cache", default=None)


# Use the `enable_caching` decorator to enable context caching for `double`.
@contextcache.enable_caching(_double_cache)
def double(n: int) -> int:
    print(f"Doubling {n}, working...")
    return n * 2


# Without caching.
print(f"Without caching")
print(double(1))
print(double(1))

# With caching.
with contextcache.use_caching(double):
    print(f"\nWith caching")
    print(double(1))
    print(double(1))

# Without caching, again.
print(f"\nWithout caching, again")
print(double(1))
print(double(1))

Here's the output:

python example.py
Without caching
Doubling 1, working...
2
Doubling 1, working...
2

With caching
Doubling 1, working...
2
2

Without caching, again
Doubling 1, working...
2
Doubling 1, working...
2

See the tests for further examples.

Caveats

  • Function arguments must be hashable.

Metadata

Release files for contextcache 0.3.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 contextcache 0.3.0
File Size Uploaded
contextcache-0.3.0.tar.gz 2.3 kB Details

Built distribution (wheel)

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

Total release size: 5.1 kB

Release files / contextcache-0.3.0.tar.gz

Download URL contextcache-0.3.0.tar.gz
Size 2.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ee04bf8ec0683c5a8d52df8097442328499d7c77c534c6f73167fe5acd63273a
BLAKE2b-256 checksum
How to use checksums
55334cfd58fc6df62e1b7640de2b5215c9505696e9266eae833a8170c5452fb0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.10.6 Darwin/22.6.0

Release files / contextcache-0.3.0-py3-none-any.whl

Download URL contextcache-0.3.0-py3-none-any.whl
Size 2.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d0d91474eeee45606c84ea6a292b49277e63da807bb6360afd7e525d8cc4e363
BLAKE2b-256 checksum
How to use checksums
6b8b89416d3191a186825e394d0ae90e928968848df203f3383bfaf9fa845415
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.10.6 Darwin/22.6.0

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.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