Skip to main content

Crate rustlavel_cache

Crate rustlavel_cache 

Source
Expand description

rustlavel-cache: caching and rate limiting.

One Cache trait, three drivers, and a rate limiter built on top of it:

drivershared between processessurvives a restartneeds
memorynononothing
filebetween processes on one hostyesa directory
redisyesyesa 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 idempotency::Idempotency;
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.
idempotency
Idempotency keys: make POST /payments safe to retry.
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 Cache trait.
redis
A Redis client written from scratch on Tokio TCP.
store
The Cache contract every driver implements.
throttle
The throttle middleware.

Enums§

Error
Every failure the framework itself can produce.
Json
A parsed JSON value.

Type Aliases§

Result
The framework-wide result type.