Cache

Struct Cache 

Source
pub struct Cache<T, V> { /* private fields */ }

Implementations§

Source§

impl<T, V> Cache<T, V>
where T: Eq + Hash, V: Clone,

Source

pub fn new(item_duration: Option<Duration>) -> Self

Construct a new Cache with a default item expiration time. An item duration of None means items do not expire by default.

§Example
use async_std::sync::Arc;
use async_std::task;
use r_cache::cache::Cache;
use std::time::Duration;

const KEY: i8 = 0;
const VALUE: &str = "VALUE";

#[async_std::main]
async fn main() {
    let cache = Arc::new(Cache::new(Some(Duration::from_secs(5 * 60))));
    task::spawn({
        let cache = Arc::clone(&cache);
        async move {
            loop {
                task::sleep(Duration::from_secs(10 * 60)).await;
                cache.remove_expired();
            }
        }
    });

    cache.set(KEY, VALUE, None);

    assert_eq!(VALUE, cache.get(&KEY).unwrap())
}
Source

pub fn get(&self, key: &T) -> Option<V>
where T: Eq + Hash, V: Clone,

Get a cache item associated with a given key.

Source

pub fn set( &self, key: T, value: V, custom_duration: Option<Duration>, ) -> Option<V>
where T: Eq + Hash,

Set an item in the cache with an associated key. The item will have the default cache expiration time if custom duration of None is given.

Source

pub fn remove_expired(&self)
where T: Eq + Hash + Clone,

Remove all expired items from the cache.

Source

pub fn remove(&self, key: &T) -> Option<V>
where T: Eq + Hash,

Remove an item from the cache associated with a given key.

Source

pub fn clear(&self)

Clear the entire cache of all items regardless of expiry times.

Source

pub fn shrink(&self)
where T: Eq + Hash,

Reclaim memory from removed / cleared items

Auto Trait Implementations§

§

impl<T, V> Freeze for Cache<T, V>

§

impl<T, V> !RefUnwindSafe for Cache<T, V>

§

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

§

impl<T, V> Sync for Cache<T, V>
where T: Send + Sync, V: Send + Sync,

§

impl<T, V> Unpin for Cache<T, V>

§

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

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.