pub struct Cache<T, V> { /* private fields */ }Implementations§
Source§impl<T, V> Cache<T, V>
impl<T, V> Cache<T, V>
Sourcepub fn new(item_duration: Option<Duration>) -> Self
pub fn new(item_duration: Option<Duration>) -> Self
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();
}
}
});
cache.set(KEY, VALUE, None);
assert_eq!(VALUE, cache.get(&KEY).unwrap())
}Sourcepub fn set(
&self,
key: T,
value: V,
custom_duration: Option<Duration>,
) -> Option<V>
pub fn set( &self, key: T, value: V, custom_duration: Option<Duration>, ) -> Option<V>
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.
Sourcepub fn remove_expired(&self)
pub fn remove_expired(&self)
Remove all expired items from the cache.
Auto Trait Implementations§
impl<T, V> Freeze for Cache<T, V>
impl<T, V> !RefUnwindSafe for Cache<T, V>
impl<T, V> Send for Cache<T, V>
impl<T, V> Sync for Cache<T, V>
impl<T, V> Unpin for Cache<T, V>
impl<T, V> UnwindSafe for Cache<T, V>where
T: UnwindSafe,
V: UnwindSafe,
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