pub struct Cache<K: Eq + Hash + 'static, V: 'static> { /* private fields */ }Expand description
The Cache interface. Store within a LazyLock, and pass the corresponding CacheStatic to the LazyLock::new()
Implementations§
Source§impl<K: Eq + Hash + Clone + 'static, V: 'static> Cache<K, V>
impl<K: Eq + Hash + Clone + 'static, V: 'static> Cache<K, V>
Sourcepub fn new(container: &'static CacheStatic<K, V>) -> Self
pub fn new(container: &'static CacheStatic<K, V>) -> Self
Construct a new Cache. This should be provided to the LazyLock holding this object.
§Example
use common::cache::{CacheStatic, Cache};
use dashmap::DashMap;
use std::{
borrow::Cow,
sync::LazyLock
};
static CACHE: CacheStatic<String, Cow<'static, str>> = LazyLock::new(DashMap::default);
pub static MY_CACHE: LazyLock<Cache<String, Cow<'static, str>>> = LazyLock::new(|| Cache::new(&CACHE));Sourcepub fn get<R>(&self, key: &R) -> Option<&'static V>
pub fn get<R>(&self, key: &R) -> Option<&'static V>
Get a value from within the Cache, if it exists.
§Example
use common::cache::{CacheStatic, Cache};
use dashmap::DashMap;
use std::{
borrow::Cow,
sync::LazyLock
};
static CACHE: CacheStatic<String, Cow<'static, str>> = LazyLock::new(DashMap::default);
pub static MY_CACHE: LazyLock<Cache<String, Cow<'static, str>>> = LazyLock::new(|| Cache::new(&CACHE));
MY_CACHE.insert("Test".to_string(), Cow::Borrowed("Another"));
let value: &'static str = MY_CACHE.get("Test").unwrap();
assert!(value == "Another");Sourcepub fn insert(&self, key: K, value: V) -> &'static V
pub fn insert(&self, key: K, value: V) -> &'static V
Insert a new value into the Cache, returning the stored value.
§Example
use common::cache::{CacheStatic, Cache};
use dashmap::DashMap;
use std::{
borrow::Cow,
sync::LazyLock
};
static CACHE: CacheStatic<String, Cow<'static, str>> = LazyLock::new(DashMap::default);
pub static MY_CACHE: LazyLock<Cache<String, Cow<'static, str>>> = LazyLock::new(|| Cache::new(&CACHE));
let interior: &'static str = MY_CACHE.insert("Test".to_string(), Cow::Borrowed("Another"));
assert!(interior == "Another");Auto Trait Implementations§
impl<K, V> Freeze for Cache<K, V>
impl<K, V> !RefUnwindSafe for Cache<K, V>
impl<K, V> Send for Cache<K, V>
impl<K, V> Sync for Cache<K, V>
impl<K, V> Unpin for Cache<K, V>
impl<K, V> !UnwindSafe for Cache<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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more