pub trait Cached<K, V> {
Show 14 methods
// Required methods
fn cache_get<Q>(&mut self, k: &Q) -> Option<&V>
where K: Borrow<Q>,
Q: Hash + Eq + ?Sized;
fn cache_get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
where K: Borrow<Q>,
Q: Hash + Eq + ?Sized;
fn cache_set(&mut self, k: K, v: V) -> Option<V>;
fn cache_get_or_set_with<F: FnOnce() -> V>(&mut self, k: K, f: F) -> &mut V;
fn cache_remove<Q>(&mut self, k: &Q) -> Option<V>
where K: Borrow<Q>,
Q: Hash + Eq + ?Sized;
fn cache_clear(&mut self);
fn cache_reset(&mut self);
fn cache_size(&self) -> usize;
// Provided methods
fn cache_reset_metrics(&mut self) { ... }
fn cache_hits(&self) -> Option<u64> { ... }
fn cache_misses(&self) -> Option<u64> { ... }
fn cache_capacity(&self) -> Option<usize> { ... }
fn cache_lifespan(&self) -> Option<u64> { ... }
fn cache_set_lifespan(&mut self, _seconds: u64) -> Option<u64> { ... }
}
Expand description
Cache operations
use cached::{Cached, UnboundCache};
let mut cache: UnboundCache<String, String> = UnboundCache::new();
// When writing, keys and values are owned:
cache.cache_set("key".to_string(), "owned value".to_string());
// When reading, keys are only borrowed for lookup:
let borrowed_cache_value = cache.cache_get("key");
assert_eq!(borrowed_cache_value, Some(&"owned value".to_string()))
Required Methods§
Sourcefn cache_get<Q>(&mut self, k: &Q) -> Option<&V>
fn cache_get<Q>(&mut self, k: &Q) -> Option<&V>
Attempt to retrieve a cached value
// You can use borrowed data, or the data's borrowed type:
let borrow_lookup_1 = cache.cache_get("key")
.map(String::clone);
let borrow_lookup_2 = cache.cache_get(&"key".to_string())
.map(String::clone); // copy the values for test asserts
Sourcefn cache_get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
fn cache_get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
Attempt to retrieve a cached value with mutable access
// You can use borrowed data, or the data's borrowed type:
let borrow_lookup_1 = cache.cache_get_mut("key")
.map(|value| value.clone());
let borrow_lookup_2 = cache.cache_get_mut(&"key".to_string())
.map(|value| value.clone()); // copy the values for test asserts
Sourcefn cache_set(&mut self, k: K, v: V) -> Option<V>
fn cache_set(&mut self, k: K, v: V) -> Option<V>
Insert a key, value pair and return the previous value
Sourcefn cache_get_or_set_with<F: FnOnce() -> V>(&mut self, k: K, f: F) -> &mut V
fn cache_get_or_set_with<F: FnOnce() -> V>(&mut self, k: K, f: F) -> &mut V
Get or insert a key, value pair
Sourcefn cache_remove<Q>(&mut self, k: &Q) -> Option<V>
fn cache_remove<Q>(&mut self, k: &Q) -> Option<V>
Remove a cached value
// You can use borrowed data, or the data's borrowed type:
let remove_1 = cache.cache_remove("key1");
let remove_2 = cache.cache_remove(&"key2".to_string());
Sourcefn cache_clear(&mut self)
fn cache_clear(&mut self)
Remove all cached values. Keeps the allocated memory for reuse.
Sourcefn cache_reset(&mut self)
fn cache_reset(&mut self)
Remove all cached values. Free memory and return to initial state
Sourcefn cache_size(&self) -> usize
fn cache_size(&self) -> usize
Return the current cache size (number of elements)
Provided Methods§
Sourcefn cache_reset_metrics(&mut self)
fn cache_reset_metrics(&mut self)
Reset misses/hits counters
Sourcefn cache_hits(&self) -> Option<u64>
fn cache_hits(&self) -> Option<u64>
Return the number of times a cached value was successfully retrieved
Sourcefn cache_misses(&self) -> Option<u64>
fn cache_misses(&self) -> Option<u64>
Return the number of times a cached value was unable to be retrieved
Sourcefn cache_capacity(&self) -> Option<usize>
fn cache_capacity(&self) -> Option<usize>
Return the cache capacity
Sourcefn cache_lifespan(&self) -> Option<u64>
fn cache_lifespan(&self) -> Option<u64>
Return the lifespan of cached values (time to eviction)
Sourcefn cache_set_lifespan(&mut self, _seconds: u64) -> Option<u64>
fn cache_set_lifespan(&mut self, _seconds: u64) -> Option<u64>
Set the lifespan of cached values, returns the old value
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.