Expand description
A key-value cache over an ordered list of stores you bring.
The crate defines one trait, CacheStore, and one type, Cache, that
runs a list of stores as levels, nearest first:
- get asks each level in order and stops at the first hit; the levels before it are filled on the way back, so the next read is near.
- put writes every level.
- delete removes the key from every level.
A level that fails is never fatal to a read: it counts as a miss and the
next level is asked. Failures are not hidden either — Cache::on_error
receives each one with the index of its level. A failed delete is returned,
since a key left behind in one level would be served again.
Backends are yours: implement CacheStore for Redis, a database, a file,
anything. The only store shipped is Lru, an in-process LRU (feature
memory, on by default, std only). Keys are strings and values bytes; how
values are encoded is the caller’s business.
use tiercache::{Cache, Lru};
let cache = Cache::new(vec![Box::new(Lru::new(1024))]);
rt(async {
cache.put("vi/niết bàn", b"[...]").await;
assert_eq!(cache.get("vi/niết bàn").await.as_deref(), Some(&b"[...]"[..]));
});The crate has no runtime and no dependency: the futures are boxed std futures, run by whatever executor the caller uses.
Structs§
- Cache
- An ordered list of
CacheStores, nearest first. See the crate docs. - Delete
Error - The levels whose delete failed: the key may still be served from them.
- Lru
- An in-process store holding at most
capacityentries; the least recently read or written one goes first. Every operation isO(log n).
Enums§
- Op
- What a level failed at, for
Cache::on_error.
Traits§
- Cache
Store - One cache level. Implement it for any backend.