Struct engine::store::Cache[][src]

pub struct Cache<K, V> where
    K: Hash + Eq,
    V: Clone + Debug
{ /* fields omitted */ }

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]

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]

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]

impl<K: Debug, V: Debug> Debug for Cache<K, V> where
    K: Hash + Eq,
    V: Clone + Debug
[src]

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]

impl<K, V> Serialize for Cache<K, V> where
    K: Hash + Eq,
    V: Clone + Debug,
    K: Serialize,
    V: Serialize
[src]

Auto Trait Implementations

impl<K, V> RefUnwindSafe for Cache<K, V> where
    K: RefUnwindSafe,
    V: RefUnwindSafe

impl<K, V> Send for Cache<K, V> where
    K: Send,
    V: Send

impl<K, V> Sync for Cache<K, V> where
    K: Sync,
    V: Sync

impl<K, V> Unpin for Cache<K, V> where
    K: Unpin,
    V: Unpin

impl<K, V> UnwindSafe for Cache<K, V> where
    K: UnwindSafe,
    V: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.