Expand description
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.
Re-exports§
pub use config::CacheConfig;pub use config::CacheStore;pub use config::Driver;pub use file::FileStore;pub use memory::MemoryStore;pub use rate_limit::RateLimit;pub use rate_limit::RateLimiter;pub use redis::RedisConfig;pub use redis::RedisStore;pub use store::BoxFuture;pub use store::Cache;pub use store::CacheExt;pub use throttle::Throttle;
Modules§
- config
- Cache configuration and the driver factory.
- file
- The file driver: one file per key under a configurable directory.
- memory
- The in-memory driver: the default, and the one tests use.
- prelude
- What an application importing this crate usually wants.
- rate_
limit - Rate limiting on top of the
Cachetrait. - redis
- A Redis client written from scratch on Tokio TCP.
- store
- The
Cachecontract every driver implements. - throttle
- The
throttlemiddleware.
Enums§
Type Aliases§
- Result
- The framework-wide result type.