cached

Trait Cached

Source
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§

Source

fn cache_get<Q>(&mut self, k: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

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
Source

fn cache_get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

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
Source

fn cache_set(&mut self, k: K, v: V) -> Option<V>

Insert a key, value pair and return the previous value

Source

fn cache_get_or_set_with<F: FnOnce() -> V>(&mut self, k: K, f: F) -> &mut V

Get or insert a key, value pair

Source

fn cache_remove<Q>(&mut self, k: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

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());
Source

fn cache_clear(&mut self)

Remove all cached values. Keeps the allocated memory for reuse.

Source

fn cache_reset(&mut self)

Remove all cached values. Free memory and return to initial state

Source

fn cache_size(&self) -> usize

Return the current cache size (number of elements)

Provided Methods§

Source

fn cache_reset_metrics(&mut self)

Reset misses/hits counters

Source

fn cache_hits(&self) -> Option<u64>

Return the number of times a cached value was successfully retrieved

Source

fn cache_misses(&self) -> Option<u64>

Return the number of times a cached value was unable to be retrieved

Source

fn cache_capacity(&self) -> Option<usize>

Return the cache capacity

Source

fn cache_lifespan(&self) -> Option<u64>

Return the lifespan of cached values (time to eviction)

Source

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.

Implementations on Foreign Types§

Source§

impl<K, V, S> Cached<K, V> for HashMap<K, V, S>
where K: Hash + Eq, S: BuildHasher + Default,

Source§

fn cache_get<Q>(&mut self, k: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source§

fn cache_get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source§

fn cache_set(&mut self, k: K, v: V) -> Option<V>

Source§

fn cache_get_or_set_with<F: FnOnce() -> V>(&mut self, key: K, f: F) -> &mut V

Source§

fn cache_remove<Q>(&mut self, k: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source§

fn cache_clear(&mut self)

Source§

fn cache_reset(&mut self)

Source§

fn cache_size(&self) -> usize

Implementors§

Source§

impl<K: Hash + Eq + Clone, V> Cached<K, V> for SizedCache<K, V>

Source§

impl<K: Hash + Eq + Clone, V> Cached<K, V> for TimedSizedCache<K, V>

Source§

impl<K: Hash + Eq + Clone, V: CanExpire> Cached<K, V> for ExpiringValueCache<K, V>

Source§

impl<K: Hash + Eq, V> Cached<K, V> for TimedCache<K, V>

Source§

impl<K: Hash + Eq, V> Cached<K, V> for UnboundCache<K, V>