Struct engine::store::Cache

source ·
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§

source§

impl<K: Hash + Eq + Clone, V: Clone + Debug> Cache<K, V>

source

pub fn new() -> Self

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

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

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

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

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

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

pub fn contains_key(&self, key: &K) -> bool

source

pub fn get_last_scanned_at(&self) -> Option<SystemTime>

source

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

pub fn clear(&mut self)

Clear the stored cache and reset.

source

pub fn keys(&self) -> Vec<K>

Returns a list of all keys inside the Cache as references

Trait Implementations§

source§

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

source§

fn clone(&self) -> Cache<K, V>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

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

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<K: Hash + Eq + Clone, V: Clone + Debug> Default for Cache<K, V>

Default implementation for Cache<K, V>

source§

fn default() -> Self

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

impl<'de, K, V> Deserialize<'de> for Cache<K, V>where K: Hash + Eq + Clone + Deserialize<'de>, V: Clone + Debug + Deserialize<'de>,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

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

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,