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

The Cache struct used to store the data in an ordered format.

Implementations

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))

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

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))

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

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

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

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

Clear the stored cache and reset.

Returns a list of all keys inside the Cache as references

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Default implementation for Cache<K, V>

Returns the “default value” for a type. Read more

Deserialize this value from the given Serde deserializer. Read more

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.