pub struct Lru<K, V> {
evict_fn: Box<dyn Fn(K, V, usize) + Send>,
capacity: Weight,
next_time: Time,
entries: HashMap<K, (V, Weight, Time)>,
by_time: BTreeMap<Time, K>,
total_weight: Weight,
}
Expand description
A weighted cache, evicting the least recently used entries.
This is reimplemented here, instead of using an existing crate, because existing options seem to either not support weights or they use unsafe.
Fields§
§evict_fn: Box<dyn Fn(K, V, usize) + Send>
§capacity: Weight
§next_time: Time
§entries: HashMap<K, (V, Weight, Time)>
§by_time: BTreeMap<Time, K>
§total_weight: Weight
Implementations§
source§impl<K: Hash + Eq + Clone, V> Lru<K, V>
impl<K: Hash + Eq + Clone, V> Lru<K, V>
sourcepub fn new<F>(capacity: usize, evict_fn: F) -> Self
pub fn new<F>(capacity: usize, evict_fn: F) -> Self
Returns a new Lru with the requested configuration.
evict_fn
is called for every entry evicted by the least recently
used policy. It is not called for entries replaced by the same key
in insert
nor entries explictly removed by remove
.
sourcepub fn entry_count(&self) -> usize
pub fn entry_count(&self) -> usize
Returns the total number of entries in the cache.
sourcepub fn entry_weight(&self) -> usize
pub fn entry_weight(&self) -> usize
Returns the sum of weights of entries in the cache.
sourcepub fn update_capacity(&mut self, capacity: usize)
pub fn update_capacity(&mut self, capacity: usize)
Changes the weighted capacity of the cache, evicting as necessary if the new value is smaller.
sourcepub fn get<Q>(&mut self, key: &Q) -> Option<(&K, &V)>
pub fn get<Q>(&mut self, key: &Q) -> Option<(&K, &V)>
Returns a reference to entry with the given key, if present, marking it as most recently used.
sourcepub fn insert(&mut self, key: K, val: V, weight: usize)
pub fn insert(&mut self, key: K, val: V, weight: usize)
Inserts the given key and value into the cache, marking it as most recently used.
If the key already exists in the cache, the existing value and weight are first removed.
sourcepub fn remove<Q>(&mut self, k: &Q) -> Option<(K, V, usize)>
pub fn remove<Q>(&mut self, k: &Q) -> Option<(K, V, usize)>
Removes the entry with the given key from the cache, if present.
Returns None if the entry was not in the cache.
sourcepub(crate) fn iter(&self) -> impl Iterator<Item = (&K, &V, usize)>
pub(crate) fn iter(&self) -> impl Iterator<Item = (&K, &V, usize)>
Returns an iterator over the entries in the cache in order from most recently used to least.
fn insert_not_exists(&mut self, key: K, val: V, weight: Weight)
fn remove_exists(&mut self, time: Time) -> (K, V, Weight)
fn resize(&mut self)
Trait Implementations§
Auto Trait Implementations§
impl<K, V> Freeze for Lru<K, V>
impl<K, V> !RefUnwindSafe for Lru<K, V>
impl<K, V> Send for Lru<K, V>
impl<K, V> !Sync for Lru<K, V>
impl<K, V> Unpin for Lru<K, V>
impl<K, V> !UnwindSafe for Lru<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
source§impl<T> FutureExt for T
impl<T> FutureExt for T
source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
source§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<P, R> ProtoType<R> for Pwhere
R: RustType<P>,
impl<P, R> ProtoType<R> for Pwhere
R: RustType<P>,
source§fn into_rust(self) -> Result<R, TryFromProtoError>
fn into_rust(self) -> Result<R, TryFromProtoError>
RustType::from_proto
.source§fn from_rust(rust: &R) -> P
fn from_rust(rust: &R) -> P
RustType::into_proto
.source§impl<'a, S, T> Semigroup<&'a S> for Twhere
T: Semigroup<S>,
impl<'a, S, T> Semigroup<&'a S> for Twhere
T: Semigroup<S>,
source§fn plus_equals(&mut self, rhs: &&'a S)
fn plus_equals(&mut self, rhs: &&'a S)
std::ops::AddAssign
, for types that do not implement AddAssign
.