Skip to main content

splitcache

A tiny “two-tier” caching decorator for integer-keyed functions:

  • Small inputs (≤ cutoff) are cached entirely using functools.cache

  • Large inputs (> cutoff) are either:

    • not cached (default when lru_size is None/0), or
    • cached with a bounded LRU using functools.lru_cache(maxsize=...)

This is useful when you have hot paths that repeatedly hit a dense “small” region, but also wander into huge integers (e.g., > $10^{20}$) where unbounded caching would explode memory.

Installation

pip install splitcache

Quick start

from splitcache import split_cache

@split_cache(cutoff=10**9, lru_size=10**6)
def nextprime(p: int) -> int:
    ...

Behavior:

  • If p <= 10**9: cached completely
  • If p > 10**9: cached in an LRU of size $10^6$

To disable caching above the cutoff:

@split_cache(cutoff=10**9, lru_size=None)
def nextprime(p: int) -> int:
    ...

When lru_size is None (or 0), values above the cutoff are recomputed every time.

Note about memory size

For a method with a signature (int, ) -> int with numbers below $10^{45}$ or so, each cache entry below the cutoff is measured at approximately 130 bytes, and above the cutoff each cache entry is measured at approximately 190 bytes.

If any number below $10^9$ is provided to this method, that would be a cache size of over 120GB in memory. If restricted to just primes: there are approximately 48 million primes below $10^9$, which would be a cache size of nearly 6GB. The primes can be further restricted, eg primes $\equiv 1 \bmod 4$.

The point of all this is to express the importance of being aware of the potential size of the caches you are creating.


API

split_cache

  • cutoff (required): cache all calls where cutoff_value(*args, **kwargs) <= cutoff. Must be > 0.

  • lru_size (optional): LRU size for calls above the cutoff.

    • None or 0 ⇒ no cache above cutoff
    • > 0 ⇒ functools.lru_cache(maxsize=lru_size) above cutoff
  • cutoff_value (optional): function that chooses which numeric value to compare against cutoff. Defaults to default_cutoff_value.

Cache controls

split_cache exposes a few helpers on the wrapped function:

  • fn.small_cache_clear(): clear the unbounded “small” cache
  • fn.big_cache_clear(): clear the “big” LRU cache if enabled
  • fn.small_cache_info / fn.big_cache_info: cache info functions when available

Example:

next_prime_1mod4.small_cache_clear()
next_prime_1mod4.big_cache_clear()

# If big cache is enabled:
info = next_prime_1mod4.big_cache_info()
print(info)

Release files for splitcache 0.0.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 splitcache 0.0.1
File Size Uploaded
splitcache-0.0.1.tar.gz 6.2 kB Details

Built distribution (wheel)

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

Total release size: 10.9 kB

Release files / splitcache-0.0.1.tar.gz

Download URL splitcache-0.0.1.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
6f8efb9d8588e864c86cfa338c62c3cf1865a0cabcf711eec21b0f7bfb10f30c
BLAKE2b-256 checksum
How to use checksums
767e41ff134f9219b5fceab29c6b105941e83aebe878fb0bc17fdb264de8645b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 9, 2026.

Transparency log

Release files / splitcache-0.0.1-py3-none-any.whl

Download URL splitcache-0.0.1-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8d552b13005fd774ebe3a223eb9cbe893c2d94a8855ef8e965c4416b1d1532a9
BLAKE2b-256 checksum
How to use checksums
0066002cdf8f19bf998932ff3191e45a061501f5183d26fad24fde1b08e53935
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 9, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.0.1 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