Struct quick_cache::sync::Cache

source ·
pub struct Cache<Key, Val, We = UnitWeighter, B = DefaultHashBuilder>(_);
Expand description

A concurrent cache.

Value

Cache values are cloned when fetched. Users should wrap their values with Arc<_> if necessary to avoid expensive clone operations. If interior mutability is required Arc<Mutex<_>> or Arc<RwLock<_>> can also be used.

Thread Safety and Concurrency

The cache instance can wrapped with an Arc (or equivalent) and shared between threads. All methods are accessible via non-mut references so no further synchronization (e.g. Mutex) is needed.

Implementations§

source§

impl<Key: Eq + Hash, Val: Clone> Cache<Key, Val, UnitWeighter, DefaultHashBuilder>

source

pub fn new(items_capacity: usize) -> Self

Creates a new cache with holds up to items_capacity items (approximately).

source§

impl<Key: Eq + Hash, Val: Clone, We: Weighter<Key, (), Val> + Clone> Cache<Key, Val, We, DefaultHashBuilder>

source

pub fn with_weighter( estimated_items_capacity: usize, weight_capacity: u64, weighter: We ) -> Self

source§

impl<Key: Eq + Hash, Val: Clone, We: Weighter<Key, (), Val> + Clone, B: BuildHasher + Clone> Cache<Key, Val, We, B>

source

pub fn with( estimated_items_capacity: usize, weight_capacity: u64, weighter: We, hash_builder: B ) -> Self

Creates a new cache that can hold up to weight_capacity in weight. estimated_items_capacity is the estimated number of items the cache is expected to hold, roughly equivalent to weight_capacity / average item weight.

source

pub fn with_options(options: Options, weighter: We, hash_builder: B) -> Self

source

pub fn is_empty(&self) -> bool

Returns whether the cache is empty

source

pub fn len(&self) -> usize

Returns the number of cached items

source

pub fn weight(&self) -> u64

Returns the total weight of cached items

source

pub fn capacity(&self) -> u64

Returns the maximum weight of cached items

source

pub fn misses(&self) -> u64

Returns the number of misses

source

pub fn hits(&self) -> u64

Returns the number of hits

source

pub fn reserve(&mut self, additional: usize)

Reserver additional space for additional entries. Note that this is counted in entries, and is not weighted.

source

pub fn get<Q>(&self, key: &Q) -> Option<Val>where Key: Borrow<Q>, Q: Eq + Hash + ?Sized,

Fetches an item from the cache. Callers should prefer get_mut whenever possible as it’s more efficient.

source

pub fn peek<Q>(&self, key: &Q) -> Option<Val>where Key: Borrow<Q>, Q: Eq + Hash + ?Sized,

Peeks an item from the cache. Contrary to gets, peeks don’t alter the key “hotness”.

source

pub fn remove<Q>(&self, key: &Q) -> boolwhere Key: Borrow<Q>, Q: Eq + Hash + ?Sized,

Peeks an item from the cache whose key is key and qey is <= highest_version. Contrary to gets, peeks don’t alter the key “hotness”.

source

pub fn insert(&self, key: Key, value: Val)

Inserts an item in the cache with key key and qey qey.

Trait Implementations§

source§

impl<Key, Val, We, B> Debug for Cache<Key, Val, We, B>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Key, Val, We = UnitWeighter, B = RandomState> !RefUnwindSafe for Cache<Key, Val, We, B>

§

impl<Key, Val, We, B> Send for Cache<Key, Val, We, B>where B: Send, Key: Send, Val: Send, We: Send,

§

impl<Key, Val, We, B> Sync for Cache<Key, Val, We, B>where B: Send + Sync, Key: Send + Sync, Val: Send + Sync, We: Send + Sync,

§

impl<Key, Val, We, B> Unpin for Cache<Key, Val, We, B>where B: Unpin,

§

impl<Key, Val, We, B> UnwindSafe for Cache<Key, Val, We, B>where B: UnwindSafe, Key: UnwindSafe, Val: UnwindSafe, We: UnwindSafe,

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,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · 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.
const: unstable · source§

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

Performs the conversion.