Struct simple_async_cache_rs::AsyncCacheStore [−][src]
pub struct AsyncCacheStore<K, V> { /* fields omitted */ }
Implementations
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(cache_ttl); }
Fetch the key from the cache.
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()).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."); } }
Auto Trait Implementations
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>