Cache

Struct Cache 

Source
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>

Source

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));
Source

pub fn get<R>(&self, key: &R) -> Option<&'static V>
where R: ?Sized + Hash + Eq, K: Borrow<R>,

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");
Source

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>
where K: Send + Sync, V: Sync + Send,

§

impl<K, V> Sync for Cache<K, V>
where K: Send + Sync, V: Sync + Send,

§

impl<K, V> Unpin for Cache<K, V>

§

impl<K, V> !UnwindSafe for Cache<K, V>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.