splitcache
A tiny “two-tier” caching decorator for integer-keyed functions:
-
Small inputs (≤
cutoff) are cached entirely usingfunctools.cache -
Large inputs (>
cutoff) are either:- not cached (default when
lru_sizeisNone/0), or - cached with a bounded LRU using
functools.lru_cache(maxsize=...)
- not cached (default when
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 wherecutoff_value(*args, **kwargs) <= cutoff. Must be> 0. -
lru_size(optional): LRU size for calls above the cutoff.Noneor0⇒ no cache above cutoff> 0⇒functools.lru_cache(maxsize=lru_size)above cutoff
-
cutoff_value(optional): function that chooses which numeric value to compare againstcutoff. Defaults todefault_cutoff_value.
Cache controls
split_cache exposes a few helpers on the wrapped function:
fn.small_cache_clear(): clear the unbounded “small” cachefn.big_cache_clear(): clear the “big” LRU cache if enabledfn.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)
| File | Size | Uploaded | |
|---|---|---|---|
| splitcache-0.0.1.tar.gz | 6.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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 logRelease 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