1 unstable release
0.1.2 | Apr 20, 2022 |
---|---|
0.1.1 |
|
0.1.0 |
|
#235 in Caching
22KB
502 lines
MemCache
usage
- cache
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");
- async cache
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 existskeys()
return all cached keys including the expired cacheinsert(key, value)
overwrite cache value and expiration time if cache existsexpire(key)
make cache value expired if cache existscontains_key(key)
returns true if the cache contains an entry for the given keyremove(key)
remove cache if cache existsclear_expired()
cleanups the cache by removing expired entries.clear
empty all data include valid cache.
Dependencies
~130KB