rustlavel-cache: caching and rate limiting.
One [Cache] trait, three drivers, and a rate limiter built on top of it:
| driver | shared between processes | survives a restart | needs |
|---|---|---|---|
memory |
no | no | nothing |
file |
between processes on one host | yes | a directory |
redis |
yes | yes | a Redis server |
use rustlavel_cache::{Cache, CacheExt, CacheStore, Throttle};
let cache = CacheStore::from_config(&config)?;
let users = cache
.remember("users:active", Duration::from_secs(300), || async {
Ok(load_active_users().await?)
})
.await?;
router.middleware(Throttle::per_minute(&cache, 60));
The Redis client is written from scratch — RESP encoder and decoder,
connection, handshake and pool, all on Tokio's TCP — for the same reason the
HTTP server and the PostgreSQL driver are: a framework that owns its wire
protocols owns its error messages, its performance and its security
posture. See [redis::resp] for the protocol itself.
Every lookup dispatches cache.hit or cache.miss on
[rustlavel_core::events], so Telescope can show a hit rate without this
crate knowing Telescope exists. Nothing is built when no subscriber is
listening.