5 releases

0.0.4 Apr 19, 2022
0.0.3 Apr 19, 2022
0.0.2 Apr 15, 2022
0.0.1 Apr 14, 2022
0.0.0 Apr 14, 2022

#227 in Caching

MIT/Apache

19KB
433 lines

DEPRECATED

please visite https://github.com/cargo-crates/mem_cache

CacherRs  

ci Latest Version downloads

usage

  • sync cacher
use cacher_rs::{Cacher};

let mut i32_cacher = Cacher::<i32>::new();
// expires_in_secs: 0 ->  expires immediate
let v1 = i32_cacher.fetch("v1", 10, || 1);
assert_eq!(v1, &1);

let mut string_cacher = Cacher::<String>::new();
let v1 = string_cacher.fetch("v1", 10, || "1".to_string());
assert_eq!(v1, "1");
  • async cacher
use cacher_rs::{AsyncCacher};

let mut i32_cacher = AsyncCacher::<i32>::new();
// expires_in_secs: 0 ->  expires immediate
let v1 = i32_cacher.fetch("v1", 10, || Box::pin(async {
  Ok(1)
})).await?;
assert_eq!(v1, &1);

let mut string_cacher = AsyncCacher::<String>::new();
let v1 = string_cacher.fetch("v1", 10, || Box::pin(async {
  Ok("1".to_string())
})).await?;
assert_eq!(v1, "1");

methods

  • [async] fetch(key, value, closure) return cache value if not expires or recalculate closure value
  • [async] force_fetch(key, value, closure) force recalculate closure value
  • [async] read(key) return key cache value if cache exists
  • write(key, value) overwrite cache value and expiration time if cache exists
  • expire(key) make cache value expired if cache exists
  • delete(key) remove cache if cache exists

Dependencies

~135KB