pub struct RefKeyEntrySelector<'a, K, Q, V, S>where
    Q: ?Sized,{ /* private fields */ }
Available on crate feature sync only.
Expand description

Provides advanced methods to select or insert an entry of the cache.

Many methods here return an Entry, a snapshot of a single key-value pair in the cache, carrying additional information like is_fresh.

RefKeyEntrySelector is constructed from the entry_by_ref method on the cache.

Implementations§

source§

impl<'a, K, Q, V, S> RefKeyEntrySelector<'a, K, Q, V, S>where K: Borrow<Q> + Hash + Eq + Send + Sync + 'static, Q: ToOwned<Owned = K> + Hash + Eq + ?Sized, V: Clone + Send + Sync + 'static, S: BuildHasher + Clone + Send + Sync + 'static,

source

pub fn or_default(self) -> Entry<K, V>where V: Default,

Returns the corresponding Entry for the reference of the key given when this entry selector was constructed. If the entry does not exist, inserts one by cloning the key and calling the default function of the value type V.

Example
use moka::sync::Cache;

let cache: Cache<String, Option<u32>> = Cache::new(100);
let key = "key1".to_string();

let entry = cache.entry_by_ref(&key).or_default();
assert!(entry.is_fresh());
assert_eq!(entry.key(), &key);
assert_eq!(entry.into_value(), None);

let entry = cache.entry_by_ref(&key).or_default();
// Not fresh because the value was already in the cache.
assert!(!entry.is_fresh());
source

pub fn or_insert(self, default: V) -> Entry<K, V>

Returns the corresponding Entry for the reference of the key given when this entry selector was constructed. If the entry does not exist, inserts one by cloning the key and using the given default value for V.

Example
use moka::sync::Cache;

let cache: Cache<String, u32> = Cache::new(100);
let key = "key1".to_string();

let entry = cache.entry_by_ref(&key).or_insert(3);
assert!(entry.is_fresh());
assert_eq!(entry.key(), &key);
assert_eq!(entry.into_value(), 3);

let entry = cache.entry_by_ref(&key).or_insert(6);
// Not fresh because the value was already in the cache.
assert!(!entry.is_fresh());
assert_eq!(entry.into_value(), 3);
source

pub fn or_insert_with(self, init: impl FnOnce() -> V) -> Entry<K, V>

Returns the corresponding Entry for the reference of the key given when this entry selector was constructed. If the entry does not exist, inserts one by cloning the key and evaluating the init closure for the value.

Example
use moka::sync::Cache;

let cache: Cache<String, String> = Cache::new(100);
let key = "key1".to_string();

let entry = cache
    .entry_by_ref(&key)
    .or_insert_with(|| "value1".to_string());
assert!(entry.is_fresh());
assert_eq!(entry.key(), &key);
assert_eq!(entry.into_value(), "value1");

let entry = cache
    .entry_by_ref(&key)
    .or_insert_with(|| "value2".to_string());
// Not fresh because the value was already in the cache.
assert!(!entry.is_fresh());
assert_eq!(entry.into_value(), "value1");
Concurrent calls on the same key

This method guarantees that concurrent calls on the same not-existing entry are coalesced into one evaluation of the init closure. Only one of the calls evaluates its closure (thus returned entry’s is_fresh method returns true), and other calls wait for that closure to complete (and their is_fresh return false).

For more detail about the coalescing behavior, see Cache::get_with.

source

pub fn or_insert_with_if( self, init: impl FnOnce() -> V, replace_if: impl FnMut(&V) -> bool ) -> Entry<K, V>

Works like or_insert_with, but takes an additional replace_if closure.

This method will evaluate the init closure and insert the output to the cache when:

  • The key does not exist.
  • Or, replace_if closure returns true.
source

pub fn or_optionally_insert_with( self, init: impl FnOnce() -> Option<V> ) -> Option<Entry<K, V>>

Returns the corresponding Entry for the reference of the key given when this entry selector was constructed. If the entry does not exist, clones the key and evaluates the init closure. If Some(value) was returned by the closure, inserts an entry with the value . If None was returned, this method does not insert an entry and returns None.

Example
use moka::sync::Cache;

let cache: Cache<String, u32> = Cache::new(100);
let key = "key1".to_string();

let none_entry = cache
    .entry_by_ref(&key)
    .or_optionally_insert_with(|| None);
assert!(none_entry.is_none());

let some_entry = cache
    .entry_by_ref(&key)
    .or_optionally_insert_with(|| Some(3));
assert!(some_entry.is_some());
let entry = some_entry.unwrap();
assert!(entry.is_fresh());
assert_eq!(entry.key(), &key);
assert_eq!(entry.into_value(), 3);

let some_entry = cache
    .entry_by_ref(&key)
    .or_optionally_insert_with(|| Some(6));
let entry = some_entry.unwrap();
// Not fresh because the value was already in the cache.
assert!(!entry.is_fresh());
assert_eq!(entry.into_value(), 3);
Concurrent calls on the same key

This method guarantees that concurrent calls on the same not-existing entry are coalesced into one evaluation of the init closure. Only one of the calls evaluates its closure (thus returned entry’s is_fresh method returns true), and other calls wait for that closure to complete (and their is_fresh return false).

For more detail about the coalescing behavior, see Cache::optionally_get_with.

source

pub fn or_try_insert_with<F, E>(self, init: F) -> Result<Entry<K, V>, Arc<E>>where F: FnOnce() -> Result<V, E>, E: Send + Sync + 'static,

Returns the corresponding Entry for the reference of the key given when this entry selector was constructed. If the entry does not exist, clones the key and evaluates the init closure. If Ok(value) was returned from the closure, inserts an entry with the value. If Err(_) was returned, this method does not insert an entry and returns the Err wrapped by std::sync::Arc.

Example
use moka::sync::Cache;

let cache: Cache<String, u32> = Cache::new(100);
let key = "key1".to_string();

let error_entry = cache
    .entry_by_ref(&key)
    .or_try_insert_with(|| Err("error"));
assert!(error_entry.is_err());

let ok_entry = cache
    .entry_by_ref(&key)
    .or_try_insert_with(|| Ok::<u32, &str>(3));
assert!(ok_entry.is_ok());
let entry = ok_entry.unwrap();
assert!(entry.is_fresh());
assert_eq!(entry.key(), &key);
assert_eq!(entry.into_value(), 3);

let ok_entry = cache
    .entry_by_ref(&key)
    .or_try_insert_with(|| Ok::<u32, &str>(6));
let entry = ok_entry.unwrap();
// Not fresh because the value was already in the cache.
assert!(!entry.is_fresh());
assert_eq!(entry.into_value(), 3);
Concurrent calls on the same key

This method guarantees that concurrent calls on the same not-existing entry are coalesced into one evaluation of the init closure (as long as these closures return the same error type). Only one of the calls evaluates its closure (thus returned entry’s is_fresh method returns true), and other calls wait for that closure to complete (and their is_fresh return false).

For more detail about the coalescing behavior, see Cache::try_get_with.

Auto Trait Implementations§

§

impl<'a, K, Q, V, S> !RefUnwindSafe for RefKeyEntrySelector<'a, K, Q, V, S>

§

impl<'a, K, Q: ?Sized, V, S> Send for RefKeyEntrySelector<'a, K, Q, V, S>where K: Send + Sync, Q: Sync, S: Sync, V: Send + Sync,

§

impl<'a, K, Q: ?Sized, V, S> Sync for RefKeyEntrySelector<'a, K, Q, V, S>where K: Send + Sync, Q: Sync, S: Sync, V: Send + Sync,

§

impl<'a, K, Q: ?Sized, V, S> Unpin for RefKeyEntrySelector<'a, K, Q, V, S>

§

impl<'a, K, Q, V, S> !UnwindSafe for RefKeyEntrySelector<'a, K, Q, V, S>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> Pointable for T

source§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

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

Initializes a with the given initializer. Read more
source§

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

Dereferences the given pointer. Read more
source§

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

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

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

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

§

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 Twhere U: TryFrom<T>,

§

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.