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.11.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.11-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl (1.0 MB view details)

Uploaded PyPymanylinux: glibc 2.28+ x86-64

zoocache-2026.3.11-cp310-abi3-win_amd64.whl (867.8 kB view details)

Uploaded CPython 3.10+Windows x86-64

zoocache-2026.3.11-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.11-cp310-abi3-manylinux_2_28_aarch64.whl (967.0 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.28+ ARM64

zoocache-2026.3.11-cp310-abi3-macosx_11_0_arm64.whl (875.2 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

zoocache-2026.3.11-cp310-abi3-macosx_10_12_x86_64.whl (946.1 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: zoocache-2026.3.11.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.11.tar.gz
Algorithm Hash digest
SHA256 64960c7cb00e64374d07ce88c0d2682a358fff8edece55b7c884fb133efdf989
MD5 231e40039169d4d0004af2d482ad379c
BLAKE2b-256 393661bc3c59e7081168cf8906ce187e9cbe0247cf626cce537667eadb2147eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for zoocache-2026.3.11.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.11-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for zoocache-2026.3.11-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 54f34ca548084b1f2e8e50ab7b218261b867369b551bf3c8c47dff2925cb2dc6
MD5 46381c69b8ea20fd055afdd393ff39a0
BLAKE2b-256 6135b11cee38c9ba483993d857624a456ebf37fe7a7b0084d5560d0a8f81e469

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: zoocache-2026.3.11-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 867.8 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.11-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 d32fab15e3b493fc802fa2101bab9015bf49e75a9ec65131643dd6c1156fccb5
MD5 9dd5d833846b573d75e360f0330a6d9c
BLAKE2b-256 ff90711093949e7abc2ea3237b9f34233aee3c78ea21eab63e78e39f3d0f6502

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for zoocache-2026.3.11-cp310-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 04813f5bd44681d227140dfbaf8209d12b775dbe5a5951427b5d973a43c07f76
MD5 71a6f4dc3c85385f631eac58ca612267
BLAKE2b-256 92de81ff8fc8f457378c1c2a1a69a378d0ef5ba558536ff9d957ba2e06bf75f8

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for zoocache-2026.3.11-cp310-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 b5bd13db519cf284312b5d3df5afdac660a63783538ffb3fefdfd3a2e3d90e07
MD5 2f24c7e875cc46d4970a2f630222b1fe
BLAKE2b-256 9a88b4839e3256d8c90faa54105b4ac7fb194a98d676289033c9019ed96e6dbd

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for zoocache-2026.3.11-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 98df2b4e41fa430de4962050e6980af9611542db85526ff5495d60535df39b24
MD5 f0fe253bb8798914bb358446e92263a3
BLAKE2b-256 2319695cfeb034f6b24bda665da277bd06a4195e97301be0c780b901b2c90814

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for zoocache-2026.3.11-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8b59a3c50fab256d474e0edc0e4ef22c95cdb30485fe46b1127fb5c1cba1822c
MD5 b4266a28e2e092b3b6a788a281423d8a
BLAKE2b-256 2f26e08d8559709bb5422ff21bc13135fb9f6ae99bcbe3a409340baa23bd418d

See more details on using hashes here.

Provenance

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