1 unstable release
0.1.0 | Aug 7, 2023 |
---|
#2194 in Encoding
11KB
219 lines
tinycache
Features
tinycache is minimal file cache with binary serialization:
- Fallback with
get_cached_or_fetch
- Binary serialization
- Optional max age for stored values
- optional tracing feature
- Invalidates if fails to read (for fast iteration)
- Uri safe with
sha1
- 4 deps (serde, bincode, tracing, sha1)
Features Overview
Get cached or fetch pattern (read, invalidate if old or non compatible, fetch and write if does not exist):
let cache_ref = TinyRef::new().max_age(Duration::from_secs(max_age_secs));
let expensive_value = cache_ref.get_cached_or_fetch(item_key, move || fetch_value());
Read, write (ya):
let cache_ref = TinyRef::new();
// write does not return result, it logs tracing if there is an issue
// found that to be more useful
cache_ref.write(key.clone(), &token);
if let Some(v) = cache_ref.read::<Token>(key.clone()) {
println!("got {:?}", v);
}
Under the hood using bincode for small file fast read/write:
let bytes = bincode::serialize(value).map_err(StoreErr::ser)?;
Item keys formatted using sha1:
fn fmt_key(k: String) -> Vec<u8> {
let mut hasher = Sha1::new();
hasher.update(k.as_bytes());
hasher.finalize().to_vec()
}
Tracing dep and logging can be disabled with a feature:
[features]
default = ["tracing"]
tracing = ["dep:tracing"]
Dependencies
~1.3–2MB
~45K SLoC