Skip to main content

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.

PyPI Python License: MIT

🌐 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', '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 <= 0ValueError. 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) plus AtomicU64 counters, 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 via cleanup_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.
  • pickle must 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 develop complains about both VIRTUAL_ENV and CONDA_PREFIX being set, run conda deactivate first, or use env -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

rust_py_cache-0.1.2.tar.gz (19.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

rust_py_cache-0.1.2-cp310-abi3-win_amd64.whl (161.0 kB view details)

Uploaded CPython 3.10+Windows x86-64

rust_py_cache-0.1.2-cp310-abi3-manylinux_2_34_x86_64.whl (307.9 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.34+ x86-64

rust_py_cache-0.1.2-cp310-abi3-macosx_11_0_arm64.whl (266.9 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file rust_py_cache-0.1.2.tar.gz.

File metadata

  • Download URL: rust_py_cache-0.1.2.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for rust_py_cache-0.1.2.tar.gz
Algorithm Hash digest
SHA256 c61aacbc8b61c63e722a3d3d767b28381eab31bcf6bda7dee4c05c18a14537b4
MD5 19e49041ef43553dbd5df760f8ffac70
BLAKE2b-256 39c2453052d0d523192413f2897f28197b946276665feb5c1d6b81064405adc0

See more details on using hashes here.

Provenance

The following attestation bundles were made for rust_py_cache-0.1.2.tar.gz:

Publisher: release.yml on robertolima-dev/rust-py-cache

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rust_py_cache-0.1.2-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for rust_py_cache-0.1.2-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 ffb7b5af674b39c999e5131703de78c87c03988a554b5e5e729c79d347f50f48
MD5 f503f33b5ceb364bd4f15808a14341ac
BLAKE2b-256 b9ff698e0846b9e13f5071c49a37379a9bff6036bd75247871f42a9ee9769546

See more details on using hashes here.

Provenance

The following attestation bundles were made for rust_py_cache-0.1.2-cp310-abi3-win_amd64.whl:

Publisher: release.yml on robertolima-dev/rust-py-cache

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rust_py_cache-0.1.2-cp310-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for rust_py_cache-0.1.2-cp310-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 c6ea15d845546b9106930e1d66e1474fb2b6e32b4d9ba8a345ba08e5ba7e40ed
MD5 42b34abed318259323ead03c0c549748
BLAKE2b-256 fb4d8b38cf386742aa3b96a5d3534ec6b6868b6a65b7d8155adcf4dd42a613d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for rust_py_cache-0.1.2-cp310-abi3-manylinux_2_34_x86_64.whl:

Publisher: release.yml on robertolima-dev/rust-py-cache

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rust_py_cache-0.1.2-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rust_py_cache-0.1.2-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7ff45387fcd1a39953cbc17fc170ad5decf31683b6ddd55f900086c4af79a6f3
MD5 f7614e882d6be6ebbb88540c2f7c0a1a
BLAKE2b-256 55c740498f0d0f66564f02917bc938f7d9edfaf5cbd5a3c7bd3aa4134473e98e

See more details on using hashes here.

Provenance

The following attestation bundles were made for rust_py_cache-0.1.2-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on robertolima-dev/rust-py-cache

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page