Struct AsyncCacheStore

Source
pub struct AsyncCacheStore<K, V> { /* private fields */ }

Implementations§

Source§

impl<K: 'static + Eq + Hash + Debug + Sync + Send + Clone, V: 'static + Sync + Send> AsyncCacheStore<K, V>

Source

pub fn new() -> Arc<Self>

Construct a new AsyncCacheStore instance. Note: expire is the number of seconds for the cached value to expire.

Panic: If you set expire to less than 3 seconds. This limitaion exists because we expire value only every seconds, meaning there could be desynchronizations with a TTL lower than 3.

use simple_async_cache_rs::AsyncCacheStore;

#[tokio::main]
async fn main() {
    let cache_ttl = 60; // number of seconds before the cached item is expired.
    let store: AsyncCacheStore<u64, String> = AsyncCacheStore::new();
}
Source

pub async fn get(&self, key: K, ttl: u64) -> Arc<Mutex<Option<V>>>

Fetch the key from the cache or creates with the supplied TTL in seconds. Returns an std::sync::Arc to the tokio::sync::Mutex for the key containing an Option. The tokio::sync::Mutex prevents DogPile effect.

let cache = store.get("key_1".to_string(), 10).await;
let mut result = cache.lock().await;
match &mut *result {
    Some(val) => {
        // You can  get here the cached value for key_1 if it is already available.
    }
    None => {
        // There is no existing entry for key_1, you can do any expansive task to get the value and store it then.
        *result = Some("This is the content for key_1.".to_string());
    }
}
Source

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

Source

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

Source

pub async fn expire(&self, key: &K)

Expire immediatly the an item from the cache.

Source

pub async fn clone_inner_map(&self) -> HashMap<K, Option<V>>
where V: Clone,

Auto Trait Implementations§

§

impl<K, V> !Freeze for AsyncCacheStore<K, V>

§

impl<K, V> !RefUnwindSafe for AsyncCacheStore<K, V>

§

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

§

impl<K, V> Sync for AsyncCacheStore<K, V>
where K: Send, V: Send,

§

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

§

impl<K, V> !UnwindSafe for AsyncCacheStore<K, V>

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.