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>
impl<K: 'static + Eq + Hash + Debug + Sync + Send + Clone, V: 'static + Sync + Send> AsyncCacheStore<K, V>
Sourcepub fn new() -> Arc<Self>
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();
}
Sourcepub async fn get(&self, key: K, ttl: u64) -> Arc<Mutex<Option<V>>>
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());
}
}
pub async fn exists(&self, key: K) -> bool
pub async fn ready(&self, key: K) -> bool
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>
impl<K, V> Sync for AsyncCacheStore<K, V>
impl<K, V> Unpin for AsyncCacheStore<K, V>where
K: Unpin,
impl<K, V> !UnwindSafe for AsyncCacheStore<K, V>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more