MemCache
usage
use mem_cache::{Cache};
let mut i32_cache = Cache::<i32>::new();
// expires_in_secs: 0 -> expires immediate
let v1 = i32_cache.fetch("v1", 10, || 1);
assert_eq!(v1, &1);
let mut string_cache = Cache::<String>::new();
let v1 = string_cache.fetch("v1", 10, || "1".to_string());
assert_eq!(v1, "1");
use mem_cache::{AsyncCache};
let mut i32_cache = AsyncCache::<i32>::new();
// expires_in_secs: 0 -> expires immediate
let v1 = i32_cache.fetch("v1", 10, || Box::pin(async {
Ok(1)
})).await?;
assert_eq!(v1, &1);
let mut string_cache = AsyncCache::<String>::new();
let v1 = string_cache.fetch("v1", 10, || Box::pin(async {
Ok("1".to_string())
})).await?;
assert_eq!(v1, "1");
methods
[async] fetch(key, expires_in_secs, closure)
return cache value if not expires or recalculate closure value
[async] force_fetch(key, expires_in_secs, closure)
force recalculate closure value
[async] get(key)
return key cache value if cache exists
keys()
return all cached keys including the expired cache
insert(key, value)
overwrite cache value and expiration time if cache exists
expire(key)
make cache value expired if cache exists
contains_key(key)
returns true if the cache contains an entry for the given key
remove(key)
remove cache if cache exists
clear_expired()
cleanups the cache by removing expired entries.
clear
empty all data include valid cache.