Skip to main content

FifoCacheMap

Struct FifoCacheMap 

Source
pub struct FifoCacheMap<K, V, const N: usize>
where K: Clone + Debug + Eq + Hash,
{ /* private fields */ }
Expand description

A bounded cache that maintains key-value pairs with O(1) lookups.

Uses an ArrayDeque for FIFO ordering and an AHashMap for fast key-value access. When capacity is exceeded, the oldest entry is automatically evicted.

§Examples

use nautilus_common::cache::fifo::FifoCacheMap;

let mut cache: FifoCacheMap<u32, String, 3> = FifoCacheMap::new();
cache.insert(1, "one".to_string());
cache.insert(2, "two".to_string());
cache.insert(3, "three".to_string());
assert_eq!(cache.get(&1), Some(&"one".to_string()));

// Adding beyond capacity evicts the oldest
cache.insert(4, "four".to_string());
assert_eq!(cache.get(&1), None);
assert_eq!(cache.get(&4), Some(&"four".to_string()));

Zero capacity is a compile-time error:

use nautilus_common::cache::fifo::FifoCacheMap;

// This fails to compile: capacity must be > 0
let cache: FifoCacheMap<u32, String, 0> = FifoCacheMap::new();

Implementations§

Source§

impl<K, V, const N: usize> FifoCacheMap<K, V, N>
where K: Clone + Debug + Eq + Hash,

Source

pub fn new() -> Self

Creates a new empty FifoCacheMap with capacity N.

§Panics

Compile-time panic if N == 0.

Source

pub const fn capacity(&self) -> usize

Returns the capacity of the cache.

Source

pub fn len(&self) -> usize

Returns the number of entries in the cache.

Source

pub fn is_empty(&self) -> bool

Returns whether the cache is empty.

Source

pub fn contains_key(&self, key: &K) -> bool

Returns whether the cache contains the given key (O(1) lookup).

Source

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

Returns a reference to the value for the given key (O(1) lookup).

Source

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

Returns a mutable reference to the value for the given key (O(1) lookup).

Source

pub fn insert(&mut self, key: K, value: V)

Inserts a key-value pair into the cache.

If the key already exists, the value is updated (no eviction occurs). If the cache is at capacity and the key is new, the oldest entry is evicted.

Source

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

Removes a key from the cache, returning the value if present.

Source

pub fn clear(&mut self)

Clears all entries from the cache.

Trait Implementations§

Source§

impl<K, V: Debug, const N: usize> Debug for FifoCacheMap<K, V, N>
where K: Clone + Debug + Eq + Hash + Debug,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<K, V, const N: usize> Default for FifoCacheMap<K, V, N>
where K: Clone + Debug + Eq + Hash,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<K, V, const N: usize> Freeze for FifoCacheMap<K, V, N>
where K: Freeze,

§

impl<K, V, const N: usize> RefUnwindSafe for FifoCacheMap<K, V, N>

§

impl<K, V, const N: usize> Send for FifoCacheMap<K, V, N>
where K: Send, V: Send,

§

impl<K, V, const N: usize> Sync for FifoCacheMap<K, V, N>
where K: Sync, V: Sync,

§

impl<K, V, const N: usize> Unpin for FifoCacheMap<K, V, N>
where K: Unpin, V: Unpin,

§

impl<K, V, const N: usize> UnsafeUnpin for FifoCacheMap<K, V, N>
where K: UnsafeUnpin,

§

impl<K, V, const N: usize> UnwindSafe for FifoCacheMap<K, V, N>
where K: UnwindSafe, V: UnwindSafe,

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.

§

impl<T> Instrument for T

§

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

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

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

Source§

type Output = T

Should always be Self
Source§

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

Source§

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

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

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

§

impl<T> Ungil for T
where T: Send,