fncache
A zero-boilerplate Rust library for function-level caching with pluggable backends, inspired by functools.lru_cache and request-cache.
Features
- Zero Boilerplate: Simple
#[fncache]attribute for instant caching - Pluggable Backends: Memory, File, Redis, RocksDB support
- Async/Sync: Seamless support for both synchronous and asynchronous functions
- Type Safety: Strong typing throughout the caching layer with compile-time guarantees
- Advanced Metrics: Built-in instrumentation with latency, hit rates, and size tracking
- Cache Invalidation: Tag-based and prefix-based cache invalidation
- Background Warming: Proactive cache population for improved performance
Quick Start
Add fncache to your Cargo.toml with the desired features:
[]
= { = "0.1.1", = ["memory"] }
= { = "1", = ["full"] }
= "0.3"
Basic Usage
use ;
use Instant;
async
Note: The first call performs the real work (~1s here). Subsequent calls with the same arguments return the cached result in microseconds.
Async Function Example
use Duration;
use sleep;
async
Backend Examples
-
Memory backend: see
examples/backend_memory.rs- Run:
cargo run --example backend_memory
- Run:
-
File backend: see
examples/backend_file.rs- Run:
cargo run --example backend_file --features file-backend
- Run:
-
Redis backend: see
examples/backend_redis.rs- Requires a running Redis server at
redis://127.0.0.1:6379 - Run:
cargo run --example backend_redis --features redis-backend
- Requires a running Redis server at
Available Features
| Feature | Description | Default |
|---|---|---|
memory |
In-memory cache backend | ✅ |
redis-backend |
Redis backend support | ❌ |
file-backend |
File-based persistent cache | ❌ |
rocksdb-backend |
RocksDB high-performance backend | ❌ |
metrics |
Performance metrics collection | ✅ |
invalidation |
Tag-based cache invalidation | ✅ |
Requirements
- Rust: 1.70+
- Runtime:
tokioonly if you use async cached functions or run async examples - Features/Backends: enable via Cargo features (see table above)
Performance
- Memory Backend: ~1-2μs set latency, ~850-970μs get hit latency
- Redis Backend: ~1ms cache hit latency (network dependent)
- File Backend: ~100μs cache hit latency
- Throughput: Thousands of operations/second (memory backend)
See benches/ for detailed benchmarks.
Documentation
- Architecture - Internal design and architecture
Changelog
See CHANGELOG.md for version history and breaking changes.
License
MIT License - see LICENSE for details.