Skip to main content

FifoCache

Struct FifoCache 

Source
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>

Source

pub fn new(capacity: usize) -> FifoCache<K, V>

Source§

impl<K, V> FifoCache<K, V>
where K: Clone + Eq + Hash,

Source

pub fn set_capacity(&mut self, capacity: usize)

Changes how many results to keep, removing the oldest added results if the limit shrinks.

Source

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.

Source

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

Source

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.

Source

pub fn capacity(&self) -> usize

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn clear(&mut self)

Trait Implementations§

Source§

impl<K, V> Debug for FifoCache<K, V>
where K: Debug, V: Debug,

Source§

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

Formats the value using the given formatter. Read more

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>
where HashMap<K, V, FxBuildHasher>: Send, VecDeque<K>: Send,

§

impl<K, V> Sync for FifoCache<K, V>
where HashMap<K, V, FxBuildHasher>: Sync, VecDeque<K>: Sync,

§

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> 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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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
Source§

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

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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<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