pub struct Cache<K, V>{ /* private fields */ }
Expand description
The Cache
struct used to store the data in an ordered format.
Implementations§
source§impl<K: Hash + Eq + Clone, V: Clone + Debug> Cache<K, V>
impl<K: Hash + Eq + Clone, V: Clone + Debug> Cache<K, V>
sourcepub fn create_with_scanner(scan_freq: Duration) -> Self
pub fn create_with_scanner(scan_freq: Duration) -> Self
creates an empty Cache
with a periodic scanner which identifies expired entries.
§Example
use engine::store::Cache;
use std::time::Duration;
let scan_freq = Duration::from_secs(60);
let mut cache = Cache::create_with_scanner(scan_freq);
let key: &'static str = "key";
let value: &'static str = "value";
cache.insert(key, value, None);
assert_eq!(cache.get(&key), Some(&value));
sourcepub fn get(&self, key: &K) -> Option<&V>
pub fn get(&self, key: &K) -> Option<&V>
Gets the value associated with the specified key.
§Example
use engine::store::Cache;
use std::time::Duration;
let mut cache = Cache::new();
let key: &'static str = "key";
let value: &'static str = "value";
cache.insert(key, value, None);
assert_eq!(cache.get(&key), Some(&value))
sourcepub fn get_or_insert<F>(
&mut self,
key: K,
func: F,
lifetime: Option<Duration>
) -> &Vwhere
F: Fn() -> V,
pub fn get_or_insert<F>(
&mut self,
key: K,
func: F,
lifetime: Option<Duration>
) -> &Vwhere
F: Fn() -> V,
Gets the value associated with the specified key. If the key could not be found in the Cache
, creates and
inserts the value using a specified func
function. # Example
use engine::store::Cache;
use std::time::Duration;
let mut cache = Cache::new();
let key = "key";
let value = "value";
assert_eq!(cache.get_or_insert(key, move || value, None), &value);
assert!(cache.contains_key(&key));
sourcepub fn insert(
&mut self,
key: K,
value: V,
lifetime: Option<Duration>
) -> Option<V>
pub fn insert( &mut self, key: K, value: V, lifetime: Option<Duration> ) -> Option<V>
Insert a key-value pair into the cache.
If key was not present, a None
is returned, else the value is updated and the old value is returned.
§Example
use engine::store::Cache;
use std::time::Duration;
let mut cache = Cache::new();
let key: &'static str = "key";
let value: &'static str = "value";
let insert = cache.insert(key, value, None);
assert_eq!(cache.get(&key), Some(&value));
assert!(insert.is_none());
sourcepub fn remove(&mut self, key: &K) -> Option<V>
pub fn remove(&mut self, key: &K) -> Option<V>
Removes a key from the cache. Returns the value from the key if the key existed in the cache.
§Example
use engine::store::Cache;
use std::time::Duration;
let mut cache = Cache::new();
let key: &'static str = "key";
let value: &'static str = "value";
let insert = cache.insert(key, value, None);
assert_eq!(cache.remove(&key), Some(value));
assert!(!cache.contains_key(&key));
pub fn contains_key(&self, key: &K) -> bool
pub fn get_last_scanned_at(&self) -> Option<SystemTime>
sourcepub fn get_scan_freq(&self) -> Option<Duration>
pub fn get_scan_freq(&self) -> Option<Duration>
Get the cache’s scan frequency.
§Example
use engine::store::Cache;
use std::time::Duration;
let scan_freq = Duration::from_secs(60);
let mut cache = Cache::create_with_scanner(scan_freq);
let key: &'static str = "key";
let value: &'static str = "value";
cache.insert(key, value, None);
assert_eq!(cache.get_scan_freq(), Some(scan_freq));
Trait Implementations§
source§impl<K: Hash + Eq + Clone, V: Clone + Debug> Default for Cache<K, V>
impl<K: Hash + Eq + Clone, V: Clone + Debug> Default for Cache<K, V>
Default implementation for Cache<K, V>
source§impl<'de, K, V> Deserialize<'de> for Cache<K, V>
impl<'de, K, V> Deserialize<'de> for Cache<K, V>
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl<K, V> Freeze for Cache<K, V>
impl<K, V> RefUnwindSafe for Cache<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for Cache<K, V>
impl<K, V> Sync for Cache<K, V>
impl<K, V> Unpin for Cache<K, V>
impl<K, V> UnwindSafe for Cache<K, V>where
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more