rainier-cache
A [Cache] port and its drivers — Laravel's Cache, with the same shape
and the same drivers you would expect.
use ;
use Duration;
# async
Drivers
| Driver | Feature | Shared between instances |
|---|---|---|
[MemoryCache] |
— | no |
RedisCache |
redis-driver |
yes |
RedisCache on a cluster |
redis-cluster |
yes, sharded |
MemcachedCache |
memcached |
yes |
DynamoDbCache |
dynamodb |
yes, and no server to run |
The transports live in rainier-drivers
rather than here, because Redis is wanted by the queue too and an
application should configure it once.
Needs the matching feature, so this one is not compiled as a doctest:
// One server…
let cache = RedisCache::connect(RedisConnector::open("redis://127.0.0.1/")?).await?;
// …or a sharded cluster, with no change to anything downstream.
let cache = RedisCache::connect(
RedisConnector::open_cluster(["redis://10.0.0.1:6379", "redis://10.0.0.2:6379"])?,
).await?;
A miss is not a failure
get returns Ok(None) for an absent key and Err only when
the cache could not be reached. Conflating the two is how a cache outage
becomes an application outage — and a cache is the one dependency an
application should be able to lose.
Driver errors are ServiceUnavailable,
so they land in the right bucket on a dashboard rather than looking like
bugs.
Namespace a shared cache
Two applications caching user:1 on one Redis database read each other's
values, and the symptom is a user seeing another application's data.
[PrefixedCache] fixes it, and makes "flush mine" refuse rather than empty
the server:
# use ;
# use Arc;
let shared: = new;
let mine = new;
What not to cache in memory
A [MemoryCache] is per-process, so anything cached for correctness
rather than for speed is wrong in it: a rate-limit counter becomes N × limit across N instances, and a lock is not a lock.
[Cache::add] and [Cache::forget_if] are atomic on every driver, so a
[Lock] over a shared one is a real lock. Over a MemoryCache it is a real
lock within this process and nothing at all between two —
[LockManager::is_shared] is the check worth making at boot.