pub struct FifoCache<K, V> { /* private fields */ }Expand description
A First In, First Out (FIFO) cache for reusing expensive results. Keeps up to your chosen limit and removes the oldest added result when full.
§Example
use tiny_cache::FifoCache;
let mut cache = FifoCache::new(2);
cache.insert("first", 10);
cache.insert("second", 20);
assert_eq!(cache.get(&"first"), Some(&10));
cache.insert("third", 30);
assert!(cache.get(&"first").is_none());
assert_eq!(cache.get(&"third"), Some(&30));
assert_eq!(cache.len(), 2);Implementations§
Source§impl<K, V> FifoCache<K, V>
impl<K, V> FifoCache<K, V>
Sourcepub fn set_capacity(&mut self, capacity: usize)
pub fn set_capacity(&mut self, capacity: usize)
Changes how many results to keep, removing the oldest added results if the limit shrinks.
Sourcepub fn insert(&mut self, key: K, value: V)
pub fn insert(&mut self, key: K, value: V)
Saves a result to reuse later, removing the oldest added result if there is no room. Replacing an existing result does not move it to the back of the line.
pub fn get(&self, key: &K) -> Option<&V>
Sourcepub fn get_mut(&mut self, key: &K) -> Option<&mut V>
pub fn get_mut(&mut self, key: &K) -> Option<&mut V>
Lets you update a saved result without creating a replacement or changing its place in line.
pub fn capacity(&self) -> usize
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn clear(&mut self)
Trait Implementations§
Auto Trait Implementations§
impl<K, V> Forget for FifoCache<K, V>
impl<K, V> Freeze for FifoCache<K, V>
impl<K, V> RefUnwindSafe for FifoCache<K, V>
impl<K, V> Send for FifoCache<K, V>
impl<K, V> Sync for FifoCache<K, V>
impl<K, V> Unpin for FifoCache<K, V>
impl<K, V> UnsafeUnpin for FifoCache<K, V>
impl<K, V> UnwindSafe for FifoCache<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
Mutably borrows from an owned value. Read more
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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