sisa-caching-rs
Async caching for Rust with one API over three stores: an in-memory cache, a Redis-family cache, and a hybrid of the two that keeps every node's local copy coherent.
Which store
| Feature | Store | Use it when |
|---|---|---|
memory |
InMemoryCacheStore |
One process. Fastest; TTLs, an entry limit with LRU eviction, sharded maps. |
redis |
RedisCacheStore |
Several processes share the cache. Redis, Valkey, Dragonfly or Garnet: one primary with or without replicas, a Redis Cluster, or Sentinel. Other languages can read and write the same keys. |
hybrid |
HybridCacheStore |
Several processes, and reads must be local-fast. A memory layer per node in front of Redis, kept coherent by pub/sub invalidation. |
Everything comes from one crate, sisa-caching. No store is on by default, so name the one you use;
tls-rustls or tls-native-tls adds rediss:// URLs. Each part is also its own crate
(core,
memory,
redis,
hybrid), for an application
that uses one store alone. These three stores are the only ones: CacheStore is sealed, so code
generic over it takes any of them, and the Redis store runs on any RedisConnection.
Every store has the same calls: get, set, get_or_set, remove, remove_prefix and
clear, plus get_entry and set_entry for a value with its creation time and TTL. One store
holds values of every type, named per call, so an application creates one store, and moving to
another store doesn't touch its call sites. The crate docs cover settings and behavior in full.
Quick start
[]
= { = "0.3", = ["memory"] } # or "redis", or "hybrid"
In memory:
use ;
// It drops expired entries every 60 seconds by itself.
let cache = new?;
let user: User = cache.get_or_set.await?;
cache.remove_prefix.await?;
On Redis (rediss:// with the tls-rustls or tls-native-tls feature):
use RedisCacheStore;
let cache = connect.await?;
Hybrid, which applies the other nodes' invalidations by itself:
use ;
let settings: HybridCacheSettings = load_config_section?;
let cache = connect.await?;
// At shutdown, after the last write: the other nodes forget this one instead of clearing.
cache.shutdown.await;
When an invalidation never reaches a node, the node clears its local layer at the writer's next
heartbeat (1 second by default), or once the writer's heartbeats have stopped for 5 seconds. The
local ttl_secs, 60 seconds by default, still expires every local copy, even of an entry Redis
keeps until removed.
Each hybrid store has its own pub/sub channel, <store>:backplane. One store usually covers an
application; a second one, such as user_cache next to catalog_cache, gets its own settings
and doesn't affect the first. See
Several hybrid stores.
Compatibility
Tested in CI against Redis 7.4, Redis 8, Valkey 8, Dragonfly 2 and Garnet 2, each as one primary, a three-primary cluster, and a primary and replica behind Sentinel through a failover, except Sentinel on Garnet, which has none. docs/03-compatibility.md says how to connect to each.
Observability
Stores trace through tracing and count hits, misses, loads, failed store calls and evictions
through the OpenTelemetry metrics API, under sisa.caching.*. A hybrid write and the invalidations
it causes on other nodes share one trace.
docs/04-observability.md
lists every span and log line and what to alert on, and the
metrics docs every metric;
the example shows the setup.
Examples
axum-config serves a user API from examples/users-api on the store its CACHE variable
names: memory (the default), redis or hybrid. It caches a scalar (GET /users/count), an
object (GET /users/{id}) and a list (GET /users). The Redis and hybrid stores need a server
at REDIS_URL (default redis://127.0.0.1:6379).
# Two hybrid instances, each in a terminal of its own: a write on one reaches the other a moment
# later. Then, in a third terminal:
CACHE=hybrid PORT=3000
CACHE=hybrid PORT=3001
It pushes traces, metrics and logs over OTLP/HTTP to OTEL_EXPORTER_OTLP_ENDPOINT (default
http://localhost:4318) and still runs without a collector. To print what it sends:
Benchmarks
Each release records its benchmarks on its release page: reads and writes on every store, each compared with the previous release on the same machine, and Criterion's full report. docs/05-benchmarks.md explains what each one measures and how to run them.
More
- Docs: an overview, then architecture, flows, compatibility, observability and benchmarks, in reading order.
- Roadmap and changelog.
- Contributing: building, testing and conventions.
- Security: reporting a vulnerability.
Rust 1.89 or newer. MIT licensed.