An ultra-fast local cache for Python, powered by Rust.
Project description
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.
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', 'size'}
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
| Method | Description |
|---|---|
set(key, value, ttl=None) |
Store a value. ttl in seconds (int/float); None = no expiration; ttl <= 0 → ValueError. Overwrites. |
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. |
stats() |
dict with hits, misses, sets, deletes, expired, 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 — an expired key is removed when accessed
(
get/exists) or viacleanup_expired(). There is no background thread in the MVP.
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: expired items may linger until accessed or until
cleanup_expired().
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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rust_py_cache-0.1.1.tar.gz.
File metadata
- Download URL: rust_py_cache-0.1.1.tar.gz
- Upload date:
- Size: 18.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a519a7ff9d081c2e030ece15aa690c70550eb731362af59e47670a0cc645b5d9
|
|
| MD5 |
2052ba92924228d9b37d2d588ac7bad2
|
|
| BLAKE2b-256 |
c2391e8c25f1ee8c62a41f795eb3892af2cbd803ba9597d0fef88d393c0e9f6a
|
Provenance
The following attestation bundles were made for rust_py_cache-0.1.1.tar.gz:
Publisher:
release.yml on robertolima-dev/rust-py-cache
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rust_py_cache-0.1.1.tar.gz -
Subject digest:
a519a7ff9d081c2e030ece15aa690c70550eb731362af59e47670a0cc645b5d9 - Sigstore transparency entry: 1911672099
- Sigstore integration time:
-
Permalink:
robertolima-dev/rust-py-cache@8c8c17f249f0cfc58dc68ce5121a55ebf408082e -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/robertolima-dev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8c8c17f249f0cfc58dc68ce5121a55ebf408082e -
Trigger Event:
push
-
Statement type:
File details
Details for the file rust_py_cache-0.1.1-cp310-abi3-win_amd64.whl.
File metadata
- Download URL: rust_py_cache-0.1.1-cp310-abi3-win_amd64.whl
- Upload date:
- Size: 161.0 kB
- Tags: CPython 3.10+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9b78b3f7a8d4017fc407827f438bfaff4cb22e2222ac44d80e820d50c4d231c
|
|
| MD5 |
6951382cceebe2a14ee40d6370222781
|
|
| BLAKE2b-256 |
1e720e8f0b96cb397c0a45310beae8e112205204b1f34dc133fdf69f884fc84d
|
Provenance
The following attestation bundles were made for rust_py_cache-0.1.1-cp310-abi3-win_amd64.whl:
Publisher:
release.yml on robertolima-dev/rust-py-cache
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rust_py_cache-0.1.1-cp310-abi3-win_amd64.whl -
Subject digest:
a9b78b3f7a8d4017fc407827f438bfaff4cb22e2222ac44d80e820d50c4d231c - Sigstore transparency entry: 1911672346
- Sigstore integration time:
-
Permalink:
robertolima-dev/rust-py-cache@8c8c17f249f0cfc58dc68ce5121a55ebf408082e -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/robertolima-dev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8c8c17f249f0cfc58dc68ce5121a55ebf408082e -
Trigger Event:
push
-
Statement type:
File details
Details for the file rust_py_cache-0.1.1-cp310-abi3-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: rust_py_cache-0.1.1-cp310-abi3-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 307.9 kB
- Tags: CPython 3.10+, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b07dfdcdfa99e83500b9cbba23135a3e9f0a643e458a3dd4343a64cefaa1f0f4
|
|
| MD5 |
0dea24a01a782232ba5e3352a4edf064
|
|
| BLAKE2b-256 |
867668ec8b2a45f7a602b25f700c6a9df2818876a3a7dd2058ffca2f62f0e04f
|
Provenance
The following attestation bundles were made for rust_py_cache-0.1.1-cp310-abi3-manylinux_2_34_x86_64.whl:
Publisher:
release.yml on robertolima-dev/rust-py-cache
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rust_py_cache-0.1.1-cp310-abi3-manylinux_2_34_x86_64.whl -
Subject digest:
b07dfdcdfa99e83500b9cbba23135a3e9f0a643e458a3dd4343a64cefaa1f0f4 - Sigstore transparency entry: 1911672275
- Sigstore integration time:
-
Permalink:
robertolima-dev/rust-py-cache@8c8c17f249f0cfc58dc68ce5121a55ebf408082e -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/robertolima-dev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8c8c17f249f0cfc58dc68ce5121a55ebf408082e -
Trigger Event:
push
-
Statement type:
File details
Details for the file rust_py_cache-0.1.1-cp310-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: rust_py_cache-0.1.1-cp310-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 266.9 kB
- Tags: CPython 3.10+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
266fc31da4951c7d369837d679710a42b8fa9f1a4af99919dd81320f1fc8c6a3
|
|
| MD5 |
f5e3b97cbe2e181f4819740f87ed677a
|
|
| BLAKE2b-256 |
74d2e6a9489575985ae9e616be5e54f9e7497fc261c1ab768382ca94d7c26bd5
|
Provenance
The following attestation bundles were made for rust_py_cache-0.1.1-cp310-abi3-macosx_11_0_arm64.whl:
Publisher:
release.yml on robertolima-dev/rust-py-cache
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rust_py_cache-0.1.1-cp310-abi3-macosx_11_0_arm64.whl -
Subject digest:
266fc31da4951c7d369837d679710a42b8fa9f1a4af99919dd81320f1fc8c6a3 - Sigstore transparency entry: 1911672215
- Sigstore integration time:
-
Permalink:
robertolima-dev/rust-py-cache@8c8c17f249f0cfc58dc68ce5121a55ebf408082e -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/robertolima-dev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8c8c17f249f0cfc58dc68ce5121a55ebf408082e -
Trigger Event:
push
-
Statement type: