A Rust library providing caching structures and procedural macros for easy function memoization.
Cached is a Rust library that provides ready-to-use caching data structures and a suite of macros for effortless function memoization. It solves the problem of redundant computation by automatically storing and retrieving the results of function calls based on their inputs, boosting application performance. The library supports various cache backends, including in-memory, Redis, and disk storage.
Rust developers building performance-sensitive applications, such as web servers, data processing pipelines, or CLI tools, who need to optimize repeated expensive calculations or external API calls.
Developers choose `cached` for its declarative macro system that minimizes boilerplate, its support for multiple cache stores (including async Redis), and its thread-safe design. It offers a more integrated and type-safe caching solution compared to manual implementation or lower-level caching crates.
Rust cache structures and easy function memoization
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
The `#[cached]`, `#[once]`, and `#[io_cached]` macros allow adding caching with minimal code changes, as shown in examples where a single annotation transforms functions like `fib(n)` to cache results automatically.
Supports in-memory, Redis, and disk stores with async variants like `AsyncRedisCache`, providing flexibility for different deployment scenarios, from local caching to distributed systems.
Cache wrappers use mutexes or RwLocks for safe concurrent access, crucial for multi-threaded applications, and mirror behavior like Python's `functools.lru_cache` for consistency.
Can memoize async functions and integrate with async cache stores via features like `redis_tokio`, making it suitable for modern asynchronous Rust codebases without restructuring.
Macros cannot be used directly under `impl` blocks or accept `Self` types, restricting their use in object-oriented designs, as explicitly stated in the README's limitations.
Using `#[io_cached]` requires specifying a `map_error` closure to convert cache errors (e.g., Redis or disk errors), adding extra code and complexity for error management.
Certain features like Redis and multi-threaded async runtimes are incompatible with WASM support, limiting its use in web assembly environments, as noted in the feature flags section.