5 releases
| 0.1.71 | Jan 17, 2026 |
|---|---|
| 0.1.70 | Jan 17, 2026 |
| 0.1.69 | Jan 17, 2026 |
| 0.1.68 | Jan 17, 2026 |
| 0.1.67 | Jan 16, 2026 |
#196 in Caching
Used in ferro-rs
34KB
702 lines
ferro-cache
Caching with tags for the Ferro framework.
Features
- Multiple backends (Redis, in-memory)
- Cache tags for bulk invalidation
- Remember pattern for lazy caching
- TTL (time-to-live) support
Usage
use ferro_cache::{Cache, CacheConfig};
use std::time::Duration;
// Create in-memory cache
let cache = Cache::memory();
// Store a value
cache.put("user:1", &user, Duration::from_secs(3600)).await?;
// Get a value
let user: Option<User> = cache.get("user:1").await?;
// Delete a value
cache.forget("user:1").await?;
Remember Pattern
Get from cache or compute and store:
let users = cache.remember("users:active", Duration::from_secs(3600), || async {
User::where_active().all().await
}).await?;
Cache Tags
Tags allow bulk invalidation of related entries:
// Store with tags
cache.tags(&["users", "admins"])
.put("user:1", &admin, Duration::from_secs(3600))
.await?;
cache.tags(&["users"])
.put("user:2", ®ular_user, Duration::from_secs(3600))
.await?;
// Flush all entries tagged with "users"
cache.tags(&["users"]).flush().await?;
// Both user:1 and user:2 are now invalidated
Redis Backend
Enable the redis-backend feature:
[dependencies]
ferro-cache = { version = "0.1", features = ["redis-backend"] }
let cache = Cache::redis("redis://localhost:6379").await?;
License
MIT
Dependencies
~8–24MB
~204K SLoC