rust-py-cache
An ultra-fast local cache for Python, powered by Rust.
A local, in-memory, thread-safe cache with TTL, lazy expiration, and metrics. The
core is written in Rust (PyO3 + maturin) on top of a concurrent DashMap; the
Python API is minimal. Think of it as a "mini Redis" living inside your Python
process.
🌐 Website: rust-py-cache.vercel.app
Installation
pip install rust-py-cache
To work on it locally (requires Rust + maturin):
python -m venv .venv && source .venv/bin/activate
pip install maturin pytest
maturin develop # compiles the Rust core and installs into the venv
pytest # runs the tests
Usage
from rust_py_cache import Cache
cache = Cache()
cache.set("user:1", {"name": "Roberto"}, ttl=60) # ttl in seconds
user = cache.get("user:1") # {"name": "Roberto"}
cache.get("missing", default=0) # 0
cache.exists("user:1") # True (honors TTL)
cache.delete("user:1") # True if removed, False if absent
cache.len() # approximate size
cache.keys() # list of keys
cache.cleanup_expired() # remove expired entries; returns the count
cache.clear() # remove everything (keeps counters)
cache.stats() # {'hits','misses','sets','deletes','expired','evicted','size'}
Bounded cache with LRU eviction
# Cap the number of keys. When full and a new key arrives, evict the
# least-recently-used entries (a ~3% batch) instead of rejecting the write.
cache = Cache(max_size=1000, eviction_policy="lru")
cache.eviction_policy # "lru"
# Default policy is "reject": set() returns False when full (and the key is new).
cache = Cache(max_size=1000) # eviction_policy="reject"
cache.set("a", 1) # True / False
Background expiration
# A background thread reclaims expired entries every N seconds, so you don't
# have to call cleanup_expired() yourself. It stops when the cache is collected.
cache = Cache(cleanup_interval=30) # seconds (int/float)
Memoization decorator
@cache.cached(ttl=60)
def add(a, b):
return a + b
add(2, 3) # runs and caches
add(2, 3) # served from cache
# custom key (fixed string or callable):
@cache.cached(ttl=300, key=lambda user_id: f"user:{user_id}")
def load_user(user_id):
...
See full examples under examples/ (FastAPI and Django).
API
Constructor: Cache(max_size=None, eviction_policy="reject", cleanup_interval=None).
eviction_policy must be "reject" or "lru" (any other value raises
ValueError). cleanup_interval (seconds, > 0) enables the background sweeper.
| Method | Description |
|---|---|
set(key, value, ttl=None) |
Store a value. ttl in seconds (int/float); None = no expiration; ttl <= 0 → ValueError. Overwrites. Returns True, or False when full under eviction_policy="reject" and the key is new. |
get(key, default=None) |
The value, or default if missing/expired (expired entries are removed). |
delete(key) |
True if removed, False if it didn't exist. |
exists(key) |
True/False, honoring TTL. |
keys() |
List of keys (may include expired-but-not-yet-collected ones). |
len() / len(cache) |
Approximate size. |
clear() |
Remove everything (does not reset counters). |
cleanup_expired() |
Remove expired entries; returns how many. |
eviction_policy (property) |
The active policy: "reject" or "lru". |
stats() |
dict with hits, misses, sets, deletes, expired, evicted, size. |
@cache.cached(ttl=None, key=None) |
Memoization decorator. |
How it works
- Serialization: in the MVP, values are serialized with
pickle(on the Python side, via PyO3) and stored as opaque bytes (Vec<u8>) in the Rust core. - Concurrency:
DashMap(a HashMap with per-shard locks) plusAtomicU64counters, with no global lock on the hot path. Thread-safe, no busy loop. - TTL: expiration is lazy by default — an expired key is removed when
accessed (
get/exists) or viacleanup_expired(). Passcleanup_intervalto also run a background sweeper thread that reclaims expired keys on its own. - Eviction: with
max_size+eviction_policy="lru", a full cache evicts the least-recently-used entries (recency updated on everygethit) to admit a new key. It removes them in a batch ofmax_size / 32(at least 1) per pass, so the O(n) scan that finds them runs once every ~n/32 inserts instead of on every one — amortized O(1) per write, with no global lock on theget/setpath. Aseton a full 100k-entry cache went from ~620 µs (one full scan per insert, up to 0.2.x) to ~1.6 µs. Right after a pass the cache sits ~3% belowmax_size.
Limitations
- The cache is process-local: multiple workers = multiple independent caches.
- It does not replace Redis for distributed caching.
- Data is lost when the process restarts.
picklemust not be used to deserialize untrusted data.- Lazy TTL by default: without
cleanup_interval, expired items may linger until accessed or untilcleanup_expired()runs.
Development
cargo test # Rust core tests
maturin develop # rebuild and install
pytest # Python tests
If
maturin developcomplains about bothVIRTUAL_ENVandCONDA_PREFIXbeing set, runconda deactivatefirst, or useenv -u CONDA_PREFIX maturin develop.
Roadmap
Stages and next steps (LRU/LFU eviction, background expiration, configurable serializer, namespaces, etc.) are in ROADMAP.md.
License
MIT
Metadata
Release files for rust-py-cache 0.3.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 | Provenance | |
|---|---|---|---|---|
| rust_py_cache-0.3.1.tar.gz | 39.4 kB | With provenance | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| rust_py_cache-0.3.1-cp310-abi3-win_amd64.whl | CPython 3.10 | abi3 | Windows x86-64 | Details |
| rust_py_cache-0.3.1-cp310-abi3-musllinux_1_2_x86_64.whl | CPython 3.10 | abi3 | Linux musl 1.2+ x86-64 | Details |
| rust_py_cache-0.3.1-cp310-abi3-musllinux_1_2_aarch64.whl | CPython 3.10 | abi3 | Linux musl 1.2+ ARM64 | Details |
| rust_py_cache-0.3.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.10 | abi3 | Linux glibc 2.17+ x86-64 | Details |
| rust_py_cache-0.3.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl | CPython 3.10 | abi3 | Linux glibc 2.17+ ARM64 | Details |
| rust_py_cache-0.3.1-cp310-abi3-macosx_11_0_arm64.whl | CPython 3.10 | abi3 | macOS 11.0+ ARM64 | Details |
| rust_py_cache-0.3.1-cp310-abi3-macosx_10_12_x86_64.whl | CPython 3.10 | abi3 | macOS 10.12+ x86-64 | Details |
Total release size: 2.5 MB
Release files / rust_py_cache-0.3.1.tar.gz
| Download URL | rust_py_cache-0.3.1.tar.gz |
|---|---|
| Size | 39.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
406d54394da08aaf87c9d040c79c9919e877b099a10df14378cb3927c2b60601
|
|
BLAKE2b-256 checksum How to use checksums |
0cc08649e7da11adc67173770f6645ff60ef69425633faff4c3a5c9531527177
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Oct 20, 2026.
Transparency logRelease files / rust_py_cache-0.3.1-cp310-abi3-win_amd64.whl
| Download URL | rust_py_cache-0.3.1-cp310-abi3-win_amd64.whl |
|---|---|
| Size | 180.4 kB |
| Tags | CPython 3.10 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
6b536b6c6b6504d847d45ce9c4cf0c0671afd9aacb9eb053e2b71c25bd235a85
|
|
BLAKE2b-256 checksum How to use checksums |
5eba17c58040f97ca35e7040ea1db5f9884ff6a45f50999ea83e3215e3acd409
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Oct 20, 2026.
Transparency logRelease files / rust_py_cache-0.3.1-cp310-abi3-musllinux_1_2_x86_64.whl
| Download URL | rust_py_cache-0.3.1-cp310-abi3-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 543.5 kB |
| Tags | CPython 3.10 Linux musl 1.2+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
107599824f6f770c1cfb0f2ab9bcccb625c8b0ad0ce3401720f213c8ee417fed
|
|
BLAKE2b-256 checksum How to use checksums |
5be90df2c30999fcf7e387d05bbf63179da8e663aba060d629b5086c570126fe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Oct 20, 2026.
Transparency logRelease files / rust_py_cache-0.3.1-cp310-abi3-musllinux_1_2_aarch64.whl
| Download URL | rust_py_cache-0.3.1-cp310-abi3-musllinux_1_2_aarch64.whl |
|---|---|
| Size | 511.7 kB |
| Tags | CPython 3.10 Linux musl 1.2+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
84efc1c9685762ec665a1edff14aa70a7b3c6bc2a24fea17cf4c47b38b9181c7
|
|
BLAKE2b-256 checksum How to use checksums |
0aeebaa61a94d73f5f0bd8f58ce2ec6993645dc0c0adfb74151f6a77d53ad144
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Oct 20, 2026.
Transparency logRelease files / rust_py_cache-0.3.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | rust_py_cache-0.3.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 337.9 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
48ca68923a2cb80eb9009191d2a3ec66938fd817aaed5f9df0434dc9d3aa54ab
|
|
BLAKE2b-256 checksum How to use checksums |
362ae14d50c059737405a1bfdf63a7ab54d63473acb23fe5a56dfde48b1c8538
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Oct 20, 2026.
Transparency logRelease files / rust_py_cache-0.3.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | rust_py_cache-0.3.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 335.1 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
4fcfeac2e1dcd2caeae49d53e5d3e5d944ffff481de411a10eb5f4a954581cd2
|
|
BLAKE2b-256 checksum How to use checksums |
a2292bb4ae5c429f7430f7b84567ed9e8237c12b7cc4a4d694523fbdd2f6ecb8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Oct 20, 2026.
Transparency logRelease files / rust_py_cache-0.3.1-cp310-abi3-macosx_11_0_arm64.whl
| Download URL | rust_py_cache-0.3.1-cp310-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 291.8 kB |
| Tags | CPython 3.10 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
842ef8bc84543a30ee92fb647a17bd73c12cc2b7aaec174ee5f388c22ee0c55b
|
|
BLAKE2b-256 checksum How to use checksums |
4c041b1f36ef4880a6767eaa6b647ada6b7c7d8c5b7541d03604ae53d9f32937
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Oct 20, 2026.
Transparency logRelease files / rust_py_cache-0.3.1-cp310-abi3-macosx_10_12_x86_64.whl
| Download URL | rust_py_cache-0.3.1-cp310-abi3-macosx_10_12_x86_64.whl |
|---|---|
| Size | 296.3 kB |
| Tags | CPython 3.10 abi3 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
0b29646d79c5fab5f93abf4608abdd5417fe8f7f524d2fa8f95a851cf203be6d
|
|
BLAKE2b-256 checksum How to use checksums |
1ed036f6532742545ed0da79bd994acd4a42534738ba5c2c0a8ed0e13ad52c1d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Oct 20, 2026.
Transparency logSecurity for rust-py-cache 0.3.1
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.
Full provenance
All 8 files in this release have been uploaded with provenance metadata.
Attestation for rust_py_cache-0.3.1.tar.gz
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 Oct 20, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.