Struct r_cache::cache::Cache[][src]

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

Implementations

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().await;
            }
        }
    });

    cache.set(KEY, VALUE, None).await;

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

Get a cache item associated with a given key.

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.

Remove all expired items from the cache.

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

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

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

Performs the conversion.

Performs the conversion.

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.