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.4.9.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.4.9-cp310-abi3-win_amd64.whl (873.3 kB view details)

Uploaded CPython 3.10+Windows x86-64

zoocache-2026.4.9-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.4.9-cp310-abi3-manylinux_2_28_aarch64.whl (972.7 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.28+ ARM64

zoocache-2026.4.9-cp310-abi3-macosx_11_0_arm64.whl (880.1 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

zoocache-2026.4.9-cp310-abi3-macosx_10_12_x86_64.whl (951.0 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for zoocache-2026.4.9.tar.gz
Algorithm Hash digest
SHA256 600166cd73864d24fdcc020389682ead1f1615575036e4b322688eec7352b384
MD5 a75803ec96e54a75dfbe41370ca65f08
BLAKE2b-256 43a9034418e275f3e147cae049cc3de98eb056891fb24b48f2f77ad51377e58f

See more details on using hashes here.

Provenance

The following attestation bundles were made for zoocache-2026.4.9.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.4.9-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: zoocache-2026.4.9-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 873.3 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

Hashes for zoocache-2026.4.9-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 dbb12965b8fbfb0032965203c31b6d66e1e0ef99daa06e7f8c2fbf86a613ec64
MD5 dba43c3a1f7257031456ebee30deebcb
BLAKE2b-256 51c16c50dc46af0a6666dc2d914e07609eca61dddbcc76fc1ec954e6f61569e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for zoocache-2026.4.9-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.4.9-cp310-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for zoocache-2026.4.9-cp310-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d377f10448f7c962d901eba65a244bbc3a4138811caccc577dbc6f0f62fc8f41
MD5 388045edc4fee02be816dca3ae7b86f5
BLAKE2b-256 330aeab864a23d68dd757501b50035ae60ff1e8cd631b8d2f724a736e960eb75

See more details on using hashes here.

Provenance

The following attestation bundles were made for zoocache-2026.4.9-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.4.9-cp310-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for zoocache-2026.4.9-cp310-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 75a124197b32c42021245cca1386e5bfe8d5add300961e2e8694050a6ce25575
MD5 76e6147b2d3d64ea00b2c88511c42199
BLAKE2b-256 021d019b0c2e6e63eccd4847b9ea68040cc8130c1788846744d470eb9893cecb

See more details on using hashes here.

Provenance

The following attestation bundles were made for zoocache-2026.4.9-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.4.9-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zoocache-2026.4.9-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5d1f0f1bdffefe7dc86bf3e0970ba674c7c55acf7195868bc4c594d5a4e70f1b
MD5 69f889e2feb69ecf7c35b8762530f2ba
BLAKE2b-256 bd3032e00a0988d45244ebfd72b4184c4db05820d62dd1e648105c0d1a3a06cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for zoocache-2026.4.9-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.4.9-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for zoocache-2026.4.9-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 16467b8d74e1a641fd0829232ef6b829510be22ef0087794acf486014c806401
MD5 66758ad443af109d727a8bbd96ec0621
BLAKE2b-256 39ac0a167394e1de005fb8b8d8ae9d573090a0e2076b87df6ee546e879fd2d1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for zoocache-2026.4.9-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