Struct engine::store::Cache [−][src]
The cache struct used to store the data in an ordered format.
Implementations
impl<K: Hash + Eq, V: Clone + Debug> Cache<K, V>
[src]
pub fn new() -> Self
[src]
creates a new empty Cache
Example
use engine::store::Cache; use std::time::Duration; let mut cache = Cache::new(); let key: Vec<u8> = b"key".to_vec(); let value: Vec<u8> = b"value".to_vec(); cache.insert(key.clone(), value.clone(), None); assert_eq!(cache.get(&key), Some(&value))
pub fn create_with_scanner(scan_freq: Duration) -> Self
[src]
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));
pub fn get(&self, key: &K) -> Option<&V>
[src]
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))
pub fn get_or_insert<F>(
&mut self,
key: K,
func: F,
lifetime: Option<Duration>
) -> &V where
F: Fn() -> V,
[src]
&mut self,
key: K,
func: F,
lifetime: Option<Duration>
) -> &V where
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));
pub fn insert(
&mut self,
key: K,
value: V,
lifetime: Option<Duration>
) -> Option<V>
[src]
&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());
pub fn remove(&mut self, key: &K) -> Option<V>
[src]
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
[src]
pub fn get_last_scanned_at(&self) -> Option<SystemTime>
[src]
pub fn get_scan_freq(&self) -> Option<Duration>
[src]
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));
pub fn clear(&mut self)
[src]
Clear the stored cache and reset.
Trait Implementations
impl<K: Clone, V: Clone> Clone for Cache<K, V> where
K: Hash + Eq,
V: Clone + Debug,
[src]
K: Hash + Eq,
V: Clone + Debug,
impl<K: Debug, V: Debug> Debug for Cache<K, V> where
K: Hash + Eq,
V: Clone + Debug,
[src]
K: Hash + Eq,
V: Clone + Debug,
impl<K: Hash + Eq, V: Clone + Debug> Default for Cache<K, V>
[src]
Default implementation for Cache<K, V>
impl<'de, K, V> Deserialize<'de> for Cache<K, V> where
K: Hash + Eq,
V: Clone + Debug,
K: Deserialize<'de>,
V: Deserialize<'de>,
[src]
K: Hash + Eq,
V: Clone + Debug,
K: Deserialize<'de>,
V: Deserialize<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
[src]
__D: Deserializer<'de>,
impl<K, V> Serialize for Cache<K, V> where
K: Hash + Eq,
V: Clone + Debug,
K: Serialize,
V: Serialize,
[src]
K: Hash + Eq,
V: Clone + Debug,
K: Serialize,
V: Serialize,
Auto Trait Implementations
impl<K, V> RefUnwindSafe for Cache<K, V> where
K: RefUnwindSafe,
V: RefUnwindSafe,
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for Cache<K, V> where
K: Send,
V: Send,
K: Send,
V: Send,
impl<K, V> Sync for Cache<K, V> where
K: Sync,
V: Sync,
K: Sync,
V: Sync,
impl<K, V> Unpin for Cache<K, V> where
K: Unpin,
V: Unpin,
K: Unpin,
V: Unpin,
impl<K, V> UnwindSafe for Cache<K, V> where
K: UnwindSafe,
V: UnwindSafe,
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> DeserializeOwned for T where
T: for<'de> Deserialize<'de>,
[src]
T: for<'de> Deserialize<'de>,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,