Struct LruCache

Source
pub struct LruCache<K, V> { /* private fields */ }
Expand description

Cache with LRU eviction strategy

Implementations§

Source§

impl<K: Hash + Eq, V> Cache<K, V>

Source

pub fn new(multiply_cap: usize, timeout_secs: u64) -> Self

Create new Cache, which will expiring its entry after timeout_secs and allocating new slab with capacity multiply_cap when no space is ready and no entry expires

Source

pub fn get<Q>(&mut self, key: &Q) -> Option<&V>
where Arc<K>: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a reference to the value of the key in the cache or None if it is not present in the cache. Moves the key to the head of the LRU list if it exists.

§Example
use aba_cache as cache;
use cache::LruCache;

let mut cache = LruCache::new(2, 60);

cache.put(1, "a");
cache.put(2, "b");
cache.put(2, "c");
cache.put(3, "d");

assert_eq!(cache.get(&1), Some(&"a"));
assert_eq!(cache.get(&2), Some(&"c"));
assert_eq!(cache.get(&3), Some(&"d"));
Source

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

Puts a key-value pair into cache. If the key already exists in the cache, then it updates the key’s value and returns the old value. Otherwise, None is returned.

§Example
use aba_cache as cache;
use cache::LruCache;

let mut cache = LruCache::new(2, 60);

assert_eq!(None, cache.put(1, "a"));
assert_eq!(None, cache.put(2, "b"));
assert_eq!(Some("b"), cache.put(2, "beta"));

assert_eq!(cache.get(&1), Some(&"a"));
assert_eq!(cache.get(&2), Some(&"beta"));
Source

pub fn evict(&mut self)

Removes expired entry. This operation will deallocate empty slab caused by entry removal if any.

§Example
use aba_cache as cache;
use cache::LruCache;
use std::{thread, time::Duration};

let mut cache = LruCache::new(2, 1);

cache.put(String::from("1"), "one");
cache.put(String::from("2"), "two");
cache.put(String::from("3"), "three");

assert_eq!(cache.len(), 3);
assert_eq!(cache.capacity(), 4);

thread::sleep(Duration::from_secs(1));
cache.evict();

assert_eq!(cache.len(), 0);
assert_eq!(cache.capacity(), 0);
Source

pub fn capacity(&self) -> usize

Returns the maximum number of key-value pairs the cache can hold. Note that on data insertion, when no space is available and no entry is timeout, then capacity will be added with multiply_cap to accomodate.

§Example
use aba_cache as cache;
use cache::LruCache;

let mut cache: LruCache<usize, &str> = LruCache::new(2, 60);
assert_eq!(cache.capacity(), 2);

cache.put(1, "a");
assert_eq!(cache.capacity(), 2);

cache.put(2, "b");
assert_eq!(cache.capacity(), 2);

cache.put(3, "c");
assert_eq!(cache.capacity(), 4);
Source

pub fn len(&self) -> usize

Returns the number of key-value pairs that are currently in the the cache. Note that len should be less than or equal to capacity

§Example
use aba_cache as cache;
use cache::LruCache;

let mut cache = LruCache::new(2, 60);
assert_eq!(cache.len(), 0);

cache.put(1, "a");
assert_eq!(cache.len(), 1);

cache.put(2, "b");
assert_eq!(cache.len(), 2);
assert_eq!(cache.capacity(), 2);

cache.put(3, "c");
assert_eq!(cache.len(), 3);
assert_eq!(cache.capacity(), 4);
Source

pub fn is_empty(&self) -> bool

Returns a bool indicating whether the cache is empty or not.

§Example
use aba_cache as cache;
use cache::LruCache;

let mut cache = LruCache::new(2, 60);
assert!(cache.is_empty());

cache.put(1, "a");
assert!(!cache.is_empty());

Auto Trait Implementations§

§

impl<K, V> Freeze for Cache<K, V>

§

impl<K, V> RefUnwindSafe for Cache<K, V>

§

impl<K, V> Send for Cache<K, V>
where K: Sync + Send, V: Send,

§

impl<K, V> Sync for Cache<K, V>
where K: Sync + Send, V: Sync,

§

impl<K, V> Unpin for Cache<K, V>
where V: Unpin,

§

impl<K, V> UnwindSafe for Cache<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 = 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.