Skip to main content

Cache that invalidates when your data changes, not when a timer expires. Rust-powered semantic invalidation for Python.

Project description

ZooCache Logo

ZooCache is a high-performance caching library with a Rust core, designed for applications where data consistency and read performance are critical.

Python 3.10+ License: MIT PyPI Downloads CI Benchmarks ReadTheDocs


Quick Start

# Install
uv add zoocache

# Use
from zoocache import cacheable, invalidate, configure, add_deps

configure()  # Configure first!

@cacheable()
def get_user(uid):
    add_deps([f"user:{uid}"])  # Register dependencies inside
    return db.fetch_user(uid)  # Runs once

get_user(1)  # Database
get_user(1)  # Cache - instant

invalidate("user:1")  # Invalidate instantly

Why ZooCache?

Traditional caches use TTL (Time To Live), which causes stale data and cache thrashing. ZooCache uses Semantic Invalidation with a PrefixTrie to invalidate exactly what changed — instantly.

Feature 🐾 ZooCache 🔴 Redis 🐶 Dogpile diskcache
Semantic Invalidation ✅ Trie-based ❌ Manual ❌ Manual ❌ TTL only
Smart Serialization ✅ MsgPack+LZ4 ❌ No ❌ No ❌ No
Distributed Sync ✅ Redis Bus ❌ Pub/Sub ❌ No ❌ No
Observability ✅ Full ❌ Basic ❌ No ❌ No

Key Features

  • 🧠 Semantic Invalidation: Use a PrefixTrie for hierarchical invalidation. Clear "user:*" to invalidate all keys related to a specific user instantly.
  • 💾 Flexible Storage Backends: Choose between in-memory, LMDB for persistence, or Redis for distributed caching.
  • 🛡️ Causal Consistency: Built-in support for Hybrid Logical Clocks (HLC) ensures consistency even in distributed systems.
  • Anti-Avalanche (SingleFlight): Protects your backend from "thundering herd" effects by coalescing concurrent identical requests.
  • 📦 Smart Serialization: Transparently handles MsgPack and LZ4 compression for maximum throughput and minimum storage.
  • 🔄 Self-Healing Distributed Cache: Automatic synchronization via Redis Bus with robust error recovery.
  • 📊 Observability: Built-in support for Logs, Prometheus, and OpenTelemetry.

Installation

# Using uv (recommended)
uv add zoocache

# Using pip
pip install zoocache

Optional Extras

# CLI & TUI for monitoring
uv add "zoocache[cli]"

# FastAPI integration
uv add "zoocache[fastapi]"

# Django integration
uv add "zoocache[django]"

# Litestar integration
uv add "zoocache[litestar]"

# All integrations
uv add "zoocache[cli,fastapi,django,litestar]"

# Full telemetry (Prometheus + OpenTelemetry)
uv add "zoocache[telemetry]"

Quick Start

1. Basic Caching

from zoocache import cacheable, invalidate, add_deps

@cacheable()
def get_user(user_id: int):
    add_deps([f"user:{user_id}"])
    return db.fetch_user(user_id)

# First call: executes the function
user = get_user(42)

# Second call: returns cached result instantly
user = get_user(42)

2. Invalidation

def update_user(user_id: int, data: dict):
    db.save(user_id, data)
    invalidate(f"user:{user_id}")  # All cached 'get_user' calls for this ID die instantly

3. Hierarchical Dependencies

from zoocache import cacheable, add_deps

@cacheable()
def get_product(pid):
    add_deps([f"product:{pid}"])
    return db.get_product(pid)

@cacheable()
def get_reviews(pid):
    add_deps([f"product:{pid}:reviews"])
    return db.get_reviews(pid)

# Invalidate product AND its reviews
invalidate("product:42")

# Invalidate only reviews
invalidate("product:42:reviews")

4. Dynamic Dependencies

@cacheable()
def get_dashboard(user_id: int):
    user = db.get_user(user_id)
    add_deps([f"user:{user_id}"])
    
    if user.is_admin:
        reports = db.get_admin_reports()
        add_deps(["reports:admin"])
        return {"user": user, "reports": reports}
    
    return {"user": user}

5. Distributed Mode

from zoocache import configure

# Configure for distributed caching
configure(
    storage_url="redis://localhost:6379",
    bus_url="redis://localhost:6379",
    prefix="myapp_prod",
    default_ttl=3600,
)

Optional CLI & TUI

ZooCache includes an optional CLI for real-time monitoring and cache management:

uv run zoocache cli

ZooCache CLI


Performance

Zoocache is continuously benchmarked to ensure zero performance regressions.

ZooCache Performance

Note: Benchmark scale: 5,000 operations. Redis running on localhost to eliminate network latency.


When to Use ZooCache

✅ Good Fit

  • Complex Data Relationships: Use dependencies to invalidate groups of data.
  • High Read/Write Ratio: Where TTL causes stale data or unnecessary cache churn.
  • Distributed Systems: Native Redis Pub/Sub invalidation and HLC consistency.
  • Strict Consistency: When users must see updates immediately (e.g., pricing, inventory).

❌ Not Ideal

  • Pure Time-Based Expiry: If you only need simple TTL for session tokens.
  • Simple Key-Value: If you don't need dependencies or hierarchical invalidation.
  • Minimal Dependencies: For small, local-only apps where basic lru_cache suffices.

Documentation

🚀 Getting Started

🔌 Integrations

💡 Concepts & Deep Dives

⚙️ Configuration

🛠️ How-to Guides

📚 Reference


Architectural Decisions (ADR)


License

This project is licensed under the MIT License — see the LICENSE file for details.

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

zoocache-2026.3.3.tar.gz (4.6 MB view details)

Uploaded Source

Built Distributions

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

zoocache-2026.3.3-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl (1.0 MB view details)

Uploaded PyPymanylinux: glibc 2.28+ x86-64

zoocache-2026.3.3-cp310-abi3-win_amd64.whl (871.6 kB view details)

Uploaded CPython 3.10+Windows x86-64

zoocache-2026.3.3-cp310-abi3-manylinux_2_28_x86_64.whl (1.0 MB view details)

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

zoocache-2026.3.3-cp310-abi3-manylinux_2_28_aarch64.whl (971.1 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.28+ ARM64

zoocache-2026.3.3-cp310-abi3-macosx_11_0_arm64.whl (878.7 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

zoocache-2026.3.3-cp310-abi3-macosx_10_12_x86_64.whl (949.2 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file zoocache-2026.3.3.tar.gz.

File metadata

  • Download URL: zoocache-2026.3.3.tar.gz
  • Upload date:
  • Size: 4.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for zoocache-2026.3.3.tar.gz
Algorithm Hash digest
SHA256 1aa3872c8ee69ca07ed751a4a5822997279670ccc3269363550c840bdba429ea
MD5 550ebe1e5b70b25c3f420c979a8d0b72
BLAKE2b-256 ac34204047a69adc7af7130699707b756d8ffae84617fd7ffe74685916d6f8ad

See more details on using hashes here.

Provenance

The following attestation bundles were made for zoocache-2026.3.3.tar.gz:

Publisher: release.yml on albertobadia/zoocache

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

File details

Details for the file zoocache-2026.3.3-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for zoocache-2026.3.3-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7d809156e77d19f86e616ff9e7ca18e739a9a20eb22b353ca82b800ea00bee12
MD5 741353097c114c5d43b6150605467732
BLAKE2b-256 298462cc979312b2f603132f386b105b3a26d4155a0ea594f1ed29f4586ea097

See more details on using hashes here.

Provenance

The following attestation bundles were made for zoocache-2026.3.3-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl:

Publisher: release.yml on albertobadia/zoocache

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

File details

Details for the file zoocache-2026.3.3-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: zoocache-2026.3.3-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 871.6 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for zoocache-2026.3.3-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 567d5795ceceb4e6b6d4305ccbccd3ce23d51d784a04e7c7995949dd1a7e4aec
MD5 70f02878a59073ef667513ddeed120cd
BLAKE2b-256 cd403ab5aff6553e3aed455bbb0ce5bb7d913a60f243827edb6f6f860e305c8f

See more details on using hashes here.

Provenance

The following attestation bundles were made for zoocache-2026.3.3-cp310-abi3-win_amd64.whl:

Publisher: release.yml on albertobadia/zoocache

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

File details

Details for the file zoocache-2026.3.3-cp310-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for zoocache-2026.3.3-cp310-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a2e32bd5bda7777528025731612db264c3ed366d21b053f28781f66946bf08d1
MD5 c2d39be9de6144e10d64621b71daaef8
BLAKE2b-256 b491b9025f08a0019d1e2cad55933f859983dd9b9f3119563331b9e89901e742

See more details on using hashes here.

Provenance

The following attestation bundles were made for zoocache-2026.3.3-cp310-abi3-manylinux_2_28_x86_64.whl:

Publisher: release.yml on albertobadia/zoocache

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

File details

Details for the file zoocache-2026.3.3-cp310-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for zoocache-2026.3.3-cp310-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 691210666c87debc77ccc66a292bae8c850ee0549d220d3f9ada750495ad02c8
MD5 5bb881f2a44c46125410dec1ade324c5
BLAKE2b-256 c95f46758f75d22db86a0440272d0fe346594c11d1121b588eb2504c450a79af

See more details on using hashes here.

Provenance

The following attestation bundles were made for zoocache-2026.3.3-cp310-abi3-manylinux_2_28_aarch64.whl:

Publisher: release.yml on albertobadia/zoocache

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

File details

Details for the file zoocache-2026.3.3-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zoocache-2026.3.3-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d0d85e524890d7751f15198a5048fe25475a414358597824083a64093bfbceac
MD5 41ee4615b69bbb8df1ae79b38c7745f3
BLAKE2b-256 66ac9571427aa46580eab6d7f522b2b3a8fee79be62d447a2d66ae93a2a33d31

See more details on using hashes here.

Provenance

The following attestation bundles were made for zoocache-2026.3.3-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on albertobadia/zoocache

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

File details

Details for the file zoocache-2026.3.3-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for zoocache-2026.3.3-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 479e85a880531b821801846271c2b0c00491dafa046865007238a3574d5e1a94
MD5 f4b9ae9add5b1dff41a6bc41773acfb0
BLAKE2b-256 4c08154409e54b19a62c4e6f1b4ee045f4f881e06f8fff6fff0dc69ef5d56643

See more details on using hashes here.

Provenance

The following attestation bundles were made for zoocache-2026.3.3-cp310-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yml on albertobadia/zoocache

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