Cachebox

5.2.3 · active · verified Thu Apr 16

Cachebox (v5.2.3) is a high-performance Python caching and memoization library, distinguished by its core implementation in Rust. It offers various thread-safe caching algorithms like LRU, FIFO, LFU, MRU, RR, TTL, and VTTL, designed for significantly faster execution and lower memory footprint compared to pure Python alternatives. The library is actively maintained and receives regular updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `@cached` decorator for memoization with a `LRUCache` and how to interact with a cache instance directly like a dictionary. It also shows how to check if a function is cached.

from cachebox import cached, LRUCache
import time

# Using the cached decorator with an LRU cache
@cached(LRUCache(maxsize=128))
def fetch_expensive_data(item_id: int) -> str:
    """Simulates an expensive data fetch."""
    print(f"Fetching data for item_id: {item_id}...")
    time.sleep(0.1) # Simulate network/computation delay
    return f"Data for {item_id}"

print(fetch_expensive_data(1))
print(fetch_expensive_data(2))
print(fetch_expensive_data(1)) # This will be cached

# Using a cache instance directly
my_cache = LRUCache(maxsize=10)
my_cache["key1"] = "value1"
print(my_cache.get("key1"))

# Check if a function is cached
print(f"Is fetch_expensive_data cached? {cached.is_cached(fetch_expensive_data)}")

view raw JSON →