Skip to content

Cachebox

The fastest caching Python library written in Rust

Cachebox is a high-performance, in-memory caching library for Python. It is written in Rust, has zero Python dependencies, and exposes a familiar dict-like API so you can drop it into existing code with minimal friction.

Key Features

  • ๐Ÿš€ Extremely Fast

    10โ€“50ร— faster than other caching libraries โ€” see benchmarks.

  • ๐Ÿ“Š Low Memory Usage

    Roughly half the memory of a standard Python dictionary for equivalent contents.

  • ๐Ÿงต Thread-Safe

    All cache operations are protected by internal Rust mutexes.

  • ๐Ÿ“ฆ Zero Dependencies

    Distributed as pre-built wheels โ€” no Rust toolchain required at install time.

  • ๐Ÿ”ฅ Full-Featured

    Seven eviction policies, TTL support, @cached decorator, callbacks, and more.

  • ๐Ÿค Compatible

    Python 3.10+ on CPython and PyPy.

When Should I Use Caching?

  • Frequent data access โ€” avoid repeated database queries or API calls for the same keys.
  • Expensive operations โ€” memoize pure, costly computations so they run only once per input.
  • High traffic โ€” absorb load spikes by serving hot data from memory.
  • Web page rendering โ€” cache fragments or full pages that are expensive to generate.
  • Rate limiting โ€” track counters and windows, or reduce calls to third-party APIs.
  • Machine learning โ€” cache predictions for repeated inputs to save inference time.

Quick Example

import cachebox

@cachebox.cached(cachebox.LRUCache(maxsize=128))
def get_user(user_id: int) -> dict:
    # Expensive DB call โ€” cached after the first call
    return db.query("SELECT * FROM users WHERE id = ?", user_id)

# First call hits the database
user = get_user(42)

# Subsequent calls with the same arguments are served from cache
user = get_user(42)

Use a cache class directly when you need full control over keys and lifetime:

from cachebox import FIFOCache

cache = FIFOCache(maxsize=128)
cache["key"] = "value"
assert cache["key"] == "value"
assert cache.get("missing", "default") == "default"

What's Next?

Page Description
Installation Install from PyPI with pip or uv
Getting Started Decorators, key makers, methods, and common patterns
Choosing a Cache Which algorithm to pick for your workload
Tips & Notes Pickling, copying, TTL sweepers, stampede prevention
FAQ Common questions and edge cases
API Reference Full class and function documentation
Migration Guide Breaking changes between major versions