Struct cached::stores::ExpiringSizedCache

source ·
pub struct ExpiringSizedCache<K, V> {
    pub ttl_millis: u64,
    pub size_limit: Option<usize>,
    /* private fields */
}
Expand description

A cache enforcing time expiration and an optional maximum size. When a maximum size is specified, the values are dropped in the order of expiration date, e.g. the next value to expire is dropped. This cache is intended for high read scenarios to allow for concurrent reads while still enforcing expiration and an optional maximum cache size.

To accomplish this, there are a few trade-offs:

  • Maximum cache size logic cannot support “LRU”, instead dropping the next value to expire
  • Cache keys must implement Ord
  • The cache’s size, reported by .len is only guaranteed to be accurate immediately after a call to either .evict or .retain_latest
  • Eviction must be explicitly requested, either on its own or while inserting

Fields§

§ttl_millis: u64§size_limit: Option<usize>

Implementations§

source§

impl<K: Hash + Eq + Ord, V> ExpiringSizedCache<K, V>

source

pub fn new(ttl_millis: u64) -> Self

source

pub fn with_capacity(ttl_millis: u64, size: usize) -> Self

source

pub fn size_limit(&mut self, size: usize) -> Option<usize>

Set a size limit. When reached, the next entries to expire are evicted. Returns the previous value if one was set.

source

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

Increase backing stores with enough capacity to store more

source

pub fn ttl_millis(&mut self, ttl_millis: u64) -> u64

Set ttl millis, return previous value

source

pub fn evict(&mut self) -> usize

Evict values that have expired. Returns number of dropped items.

source

pub fn retain_latest(&mut self, count: usize, evict: bool) -> usize

Retain only the latest count values, dropping the next values to expire. If evict, then also evict values that have expired. Returns number of dropped items.

source

pub fn remove(&mut self, key: &K) -> Option<V>

Remove an entry, returning an unexpired value if it was present.

source

pub fn insert(&mut self, key: K, value: V) -> Result<Option<V>, Error>

Insert k/v pair without running eviction logic. See .insert_ttl_evict

source

pub fn insert_ttl( &mut self, key: K, value: V, ttl_millis: u64 ) -> Result<Option<V>, Error>

Insert k/v pair with explicit ttl. See .insert_ttl_evict

source

pub fn insert_evict( &mut self, key: K, value: V, evict: bool ) -> Result<Option<V>, Error>

Insert k/v pair and run eviction logic. See .insert_ttl_evict

source

pub fn insert_ttl_evict( &mut self, key: K, value: V, ttl_millis: Option<u64>, evict: bool ) -> Result<Option<V>, Error>

Optionally run eviction logic before inserting a k/v pair with an optional explicit TTL. If a size_limit was specified, the next entry to expire will be evicted to make space. Returns any existing unexpired value.

source

pub fn clear(&mut self)

Clear all cache entries. Does not release underlying containers

source

pub fn len(&self) -> usize

Return cache size. Note, this does not evict so may return a size that includes expired entries. Run evict or retain_latest first to ensure an accurate length.

source

pub fn is_empty(&self) -> bool

source

pub fn get(&self, key: &K) -> Option<&V>

Retrieve unexpired entry

source§

impl<V> ExpiringSizedCache<String, V>

source

pub fn get_borrowed(&self, key: &str) -> Option<&V>

Retrieve unexpired entry, accepting &str to check against String keys

let mut cache = ExpiringSizedCache::<String, &str>::new(2_000);
cache.insert(String::from("a"), "a").unwrap();
assert_eq!(cache.get_borrowed("a").unwrap(), &"a");
source§

impl<T: Hash + Eq + PartialEq, V> ExpiringSizedCache<Vec<T>, V>

source

pub fn get_borrowed(&self, key: &[T]) -> Option<&V>

Retrieve unexpired entry, accepting &[T] to check against Vec<T> keys

let mut cache = ExpiringSizedCache::<Vec<usize>, &str>::new(2_000);
cache.insert(vec![0], "a").unwrap();
assert_eq!(cache.get_borrowed(&[0]).unwrap(), &"a");

Auto Trait Implementations§

§

impl<K, V> Freeze for ExpiringSizedCache<K, V>

§

impl<K, V> RefUnwindSafe for ExpiringSizedCache<K, V>

§

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

§

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

§

impl<K, V> Unpin for ExpiringSizedCache<K, V>
where V: Unpin,

§

impl<K, V> UnwindSafe for ExpiringSizedCache<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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Pointable for T

source§

const ALIGN: usize = _

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 T
where 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 T
where 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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more