Crates.io | tinycache |
lib.rs | tinycache |
version | 0.1.0 |
source | src |
created_at | 2023-08-07 08:47:08.91345 |
updated_at | 2023-08-07 08:47:08.91345 |
description | minimal file cache with binary serialization |
homepage | https://github.com/copiumnicus/tinycache |
repository | https://github.com/copiumnicus/tinycache |
max_upload_size | |
id | 937779 |
size | 10,615 |
tinycache is minimal file cache with binary serialization:
get_cached_or_fetch
sha1
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"]