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.
pip install cacheboxVerified import paths — ran on the pinned version, not inferred.
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.
Replace `@cachebox.cachedmethod` with `@cachebox.cached` and implement a `key_maker` if the `self` parameter needs to be ignored for hashing.
Upgrade your Python environment to 3.9 or newer. The library officially supports Python 3.9+.
If experiencing runtime errors or performance degradation on `musl` Linux with `cachebox` v5.2.0, upgrade to v5.2.1 or newer.
Update your code to use the `cached` decorator instead. For instance methods, if you need to ignore `self` for key generation, provide a `key_maker` function. Example: `@cached(cachebox.LRUCache(128), key_maker=lambda args, kwargs: args[1:])`.
Ensure you are using `cachebox` v5.2.1 or newer, which includes a fix for `musl` compatibility related to `mimalloc`. If the issue persists, consider trying a different Python environment or consulting `cachebox`'s GitHub issues for `musl`-specific build instructions.
Use a cache implementation with an eviction policy (e.g., `LRUCache`, `FIFOCache`, `TTLCache`) if you want the cache to automatically discard old items when `maxsize` is reached. If you intend for the cache to have no size limit, set `maxsize=0` during initialization, or simply use a standard Python `dict`.