A fast, persistent, process-safe key-value cache for Python, with a C++20 core.
Store Python objects under keys on disk; any thread or process on the machine reads them back, safely, even at the same time. It speaks diskcache's API, so most code switches by changing the import.
pip install pysciqlop-cache
Wheels for Linux, macOS and Windows, x86-64 and ARM64, Python 3.10 to 3.15 (free-threaded builds included) and PyPy 3.11 and 3.12. 📖 Documentation
from pysciqlop_cache import Cache cache = Cache("/tmp/my-cache") cache["sensor/temperature"] = {"ts": 1710000000, "values": [21.3, 21.5, 21.4]} print(cache["sensor/temperature"]) # {'ts': 1710000000, 'values': [21.3, 21.5, 21.4]}
Cache slow functions and API calls. Results survive restarts and are shared by every
process. ignore keeps sessions and clients out of the key; async def works too.
@cache.memoize(expire=3600, ignore={"session"}) def fetch(url, session): return session.get(url).json()
Share results between worker processes. Workers of a multiprocessing pool share
one cache, whatever the start method (fork, forkserver, spawn); no server, no lock files.
from multiprocessing import Pool @cache.memoize() def square(i): return i * i if __name__ == "__main__": with Pool(4) as pool: print(sum(pool.map(square, range(100)))) # computed once, then read back # 328350
Keep big numpy arrays or data products on disk. PickleOOBSerializer moves array
bytes in C++ without holding the GIL, compressing them when it pays off, so threads
loading data don't block each other.
import numpy as np from pysciqlop_cache import Cache, PickleOOBSerializer arrays = Cache("/tmp/arrays", serializer=PickleOOBSerializer()) arrays["day/2026-01-01"] = np.random.random((1_000_000, 4))
A dict bigger than memory. Index is a persistent MutableMapping with no
expiration or eviction overhead.
from pysciqlop_cache import Index catalog = Index("/tmp/catalog") catalog.update({"run-1": {"status": "done"}, "run-2": {"status": "failed"}}) print(catalog.get_many(["run-1", "run-3"])) # one consistent snapshot # {'run-1': {'status': 'done'}}
Coordinate processes. Atomic counters and a cross-process lock.
cache.incr("downloads") with cache.lock("refresh-token"): ... # one process at a time
Coming from diskcache? One command copies a Cache, FanoutCache or Index over,
and the compatibility page
lists the few differences.
python -m pysciqlop_cache.migrate /path/to/diskcache /path/to/sciqlop-cache
- Persistent and shareable: threads and processes, including forked pools, share one cache directory; it can be copied to another machine, even another OS.
- Bounded: optional size limit with LRU eviction, per-key expiration, tags.
- Fast: small values live in SQLite, large ones in memory-mapped files (the split is
configurable). Compared
with diskcache: writes 1.2 to 1.7x faster for small values and 2 to 4x from 64 KB,
repeated reads of values above 8 KB up to 3x, batched small writes 3.7x, and write
latency stays flat up to a million entries. diskcache writes 8 to 32 KB values about 2x
faster;
max_blob_sizecloses that gap (benchmarks). - Crash-, fork- and race-tested: interrupted writes and crashed processes leave no
half-written values;
cache.check(fix=True)repairs what an external hand broke. - Typed: ships type information for mypy and pyright.
Not for: network filesystems (NFS, SMB), or processes on different machines sharing one cache: SQLite's locking needs a local disk. Values are pickled by default, and unpickling runs code: keep the folder private, or use the msgspec serializer (security).
| Quickstart | dict API, expiration and tags, size limits, memoization, transactions |
| Choosing a store | Cache, Index, FanoutCache, FanoutIndex, max_blob_size |
| Threads, processes and transactions | what is safe, what runs in parallel, locks |
| Serializers | pickle, msgspec, and PickleOOBSerializer for big numpy arrays |
| Coming from diskcache | one-command migration, API differences |
| C++ guide | the same engine from C++20 |
| Performance | benchmarks against diskcache |
| How it works | storage, crash and fork safety, self-healing |
| API reference | every class and method |
Measured against diskcache, both with their default settings, on an NVMe disk. Values from public datasets (CIFAR-10 images, NYC taxi trips as a DataFrame, Hurricane ISABEL simulation fields, household power readings) and random noise, from 100 KB to 1 GB, on Linux:
More charts and the methodology are in the performance page.
pip install . # or, for development: meson setup build -Dwith_tests=true meson compile -C build meson test -C build
See installation for the build options.
MIT
