Skip to main content

Crate tiercache

Crate tiercache 

Source
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.
DeleteError
The levels whose delete failed: the key may still be served from them.
Lru
An in-process store holding at most capacity entries; the least recently read or written one goes first. Every operation is O(log n).

Enums§

Op
What a level failed at, for Cache::on_error.

Traits§

CacheStore
One cache level. Implement it for any backend.

Type Aliases§

BoxError
Any error a store returns.
BoxFuture
The future every store method returns: boxed, so stores of different types can sit in one list.