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