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

Examples found in repository?
examples/caches.rs (line 9)
8fn main() {
9    let mut fifo = FifoCache::new(2);
10    for key in 0..2 {
11        fifo.insert(key, format!("Value {key}"));
12    }
13    fifo.insert(2, "Newest value".to_owned());
14    assert!(fifo.get(&0).is_none());
15    assert_eq!(fifo.len(), 2);
16    fifo.set_capacity(1);
17    assert_eq!(fifo.get(&1), None);
18    assert_eq!(fifo.len(), 1);
19
20    let mut shared = LRUCache::<String, u64>::default();
21    let first = shared
22        .utilize(1, &"greeting")
23        .unwrap_or_else(|| shared.insert(1, &"greeting", "Hello".to_owned()));
24    let second = shared
25        .utilize(2, &"greeting")
26        .unwrap_or_else(|| shared.insert(2, &"greeting", "Hello".to_owned()));
27    assert!(Rc::ptr_eq(&first, &second));
28
29    shared.remove(&1);
30    assert_eq!(shared.len(), 1);
31    shared.remove(&2);
32    assert!(shared.is_empty());
33    assert_eq!(first.as_str(), "Hello");
34}
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.

Examples found in repository?
examples/caches.rs (line 16)
8fn main() {
9    let mut fifo = FifoCache::new(2);
10    for key in 0..2 {
11        fifo.insert(key, format!("Value {key}"));
12    }
13    fifo.insert(2, "Newest value".to_owned());
14    assert!(fifo.get(&0).is_none());
15    assert_eq!(fifo.len(), 2);
16    fifo.set_capacity(1);
17    assert_eq!(fifo.get(&1), None);
18    assert_eq!(fifo.len(), 1);
19
20    let mut shared = LRUCache::<String, u64>::default();
21    let first = shared
22        .utilize(1, &"greeting")
23        .unwrap_or_else(|| shared.insert(1, &"greeting", "Hello".to_owned()));
24    let second = shared
25        .utilize(2, &"greeting")
26        .unwrap_or_else(|| shared.insert(2, &"greeting", "Hello".to_owned()));
27    assert!(Rc::ptr_eq(&first, &second));
28
29    shared.remove(&1);
30    assert_eq!(shared.len(), 1);
31    shared.remove(&2);
32    assert!(shared.is_empty());
33    assert_eq!(first.as_str(), "Hello");
34}
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.

Examples found in repository?
examples/caches.rs (line 11)
8fn main() {
9    let mut fifo = FifoCache::new(2);
10    for key in 0..2 {
11        fifo.insert(key, format!("Value {key}"));
12    }
13    fifo.insert(2, "Newest value".to_owned());
14    assert!(fifo.get(&0).is_none());
15    assert_eq!(fifo.len(), 2);
16    fifo.set_capacity(1);
17    assert_eq!(fifo.get(&1), None);
18    assert_eq!(fifo.len(), 1);
19
20    let mut shared = LRUCache::<String, u64>::default();
21    let first = shared
22        .utilize(1, &"greeting")
23        .unwrap_or_else(|| shared.insert(1, &"greeting", "Hello".to_owned()));
24    let second = shared
25        .utilize(2, &"greeting")
26        .unwrap_or_else(|| shared.insert(2, &"greeting", "Hello".to_owned()));
27    assert!(Rc::ptr_eq(&first, &second));
28
29    shared.remove(&1);
30    assert_eq!(shared.len(), 1);
31    shared.remove(&2);
32    assert!(shared.is_empty());
33    assert_eq!(first.as_str(), "Hello");
34}
Source

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

Examples found in repository?
examples/caches.rs (line 14)
8fn main() {
9    let mut fifo = FifoCache::new(2);
10    for key in 0..2 {
11        fifo.insert(key, format!("Value {key}"));
12    }
13    fifo.insert(2, "Newest value".to_owned());
14    assert!(fifo.get(&0).is_none());
15    assert_eq!(fifo.len(), 2);
16    fifo.set_capacity(1);
17    assert_eq!(fifo.get(&1), None);
18    assert_eq!(fifo.len(), 1);
19
20    let mut shared = LRUCache::<String, u64>::default();
21    let first = shared
22        .utilize(1, &"greeting")
23        .unwrap_or_else(|| shared.insert(1, &"greeting", "Hello".to_owned()));
24    let second = shared
25        .utilize(2, &"greeting")
26        .unwrap_or_else(|| shared.insert(2, &"greeting", "Hello".to_owned()));
27    assert!(Rc::ptr_eq(&first, &second));
28
29    shared.remove(&1);
30    assert_eq!(shared.len(), 1);
31    shared.remove(&2);
32    assert!(shared.is_empty());
33    assert_eq!(first.as_str(), "Hello");
34}
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

Examples found in repository?
examples/caches.rs (line 15)
8fn main() {
9    let mut fifo = FifoCache::new(2);
10    for key in 0..2 {
11        fifo.insert(key, format!("Value {key}"));
12    }
13    fifo.insert(2, "Newest value".to_owned());
14    assert!(fifo.get(&0).is_none());
15    assert_eq!(fifo.len(), 2);
16    fifo.set_capacity(1);
17    assert_eq!(fifo.get(&1), None);
18    assert_eq!(fifo.len(), 1);
19
20    let mut shared = LRUCache::<String, u64>::default();
21    let first = shared
22        .utilize(1, &"greeting")
23        .unwrap_or_else(|| shared.insert(1, &"greeting", "Hello".to_owned()));
24    let second = shared
25        .utilize(2, &"greeting")
26        .unwrap_or_else(|| shared.insert(2, &"greeting", "Hello".to_owned()));
27    assert!(Rc::ptr_eq(&first, &second));
28
29    shared.remove(&1);
30    assert_eq!(shared.len(), 1);
31    shared.remove(&2);
32    assert!(shared.is_empty());
33    assert_eq!(first.as_str(), "Hello");
34}
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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

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