pub struct Cache<K, V, E = VoidEvict<K, V>, S = Storage<K, V>, A = TinyLFUCache, H = SipHasherBuilder>where
    K: Eq + Hash,
    E: OnEvict<K, V>,
    S: Store<K, V>,
    A: TinyLFU,
    H: BuildHasher,
{ /* private fields */ }
Expand description

Default implementation of Cache with TinyLFU admit policy.

Implementations

Create new cache with default window_size = 10000 for TinyLFU.

Arguments
  • capacity: max items in cache
Example
use cascara::Cache;

let cache = Cache::<u8,u8>::new(100);
Panic

If capacity is 0.

Create new cache with defined window size fo TinyLFU.

Arguments
  • window_size: window size for TinyLFU (max. 10 000)
  • capacity: max items in cache
Example
use cascara::Cache;

let cache = Cache::<u8,u8>::with_window_size(100,20);
Panic

If window_size or capacity is 0. If window_size > 10 000

Create new cache with TinyLFU window size = 10 000 and callback for evicted items from cache.

Arguments
  • capacity: max items in cache
  • on_evict: will be call for every item evicted from cache.
Example
use cascara::{Cache, OnEvict};

#[derive(Default)]
struct Evict {}

impl OnEvict<u8, u8> for Evict {
    fn evict(&self, k: &u8, v: &u8) {
        println!("Evict item.  k={}, v={}", k, v);
    }
}

let cache = Cache::<u8,u8,Evict>::with_on_evict(100, Evict::default());
Panic

If capacity is 0.

Create new cache with defined window size for TinyLFU and callback for evicted items from cache.

Arguments
  • capacity: max items in cache
  • on_evict: will be call for every item evicted from cache.
  • window_size: window size for TinyLFU (max. 10 000)
Example
use cascara::{Cache, OnEvict};

#[derive(Default)]
struct Evict {}

impl OnEvict<u8, u8> for Evict {
    fn evict(&self, k: &u8, v: &u8) {
        println!("Evict item.  k={}, v={}", k, v);
    }
}

let cache = Cache::<u8,u8,Evict>::with_on_evict_and_window_size(100, Evict::default(), 20);
Panic

If window_size or capacity is 0. If window_size > 10 000

Activate metric collecting in cache

Example
use cascara::Cache;

let cache = Cache::<u8,u8>::new(100).with_metrics();
assert!(cache.metrics().is_some());

Returns how many items can be hold in cache

Example
use cascara::Cache;

let cache = Cache::<u8,u8>::new(100);
assert_eq!(cache.capacity(), 100);

Returns actual number of items in cache

Example
use cascara::Cache;

let mut cache = Cache::new(100);
assert!(cache.insert(1,1).is_ok());
assert_eq!(cache.len(), 1);

Returns true if cache is empty

Example
use cascara::Cache;

let cache = Cache::<u8,u8>::new(100);
assert!(cache.is_empty());

Returns how many room left

Example
use cascara::Cache;

let mut cache = Cache::new(100);
assert_eq!(cache.room_left(), 100);
assert!(cache.insert(1,1).is_ok());
assert_eq!(cache.room_left(), 99);

Return true if item is in storage

Arguments
  • k: item key
Example
use cascara::Cache;

let mut cache = Cache::new(100);
assert!(!cache.contains(&1));
assert!(cache.insert(1,1).is_ok());
assert!(cache.contains(&1));

Return item ref if is in cache

Arguments
  • k: item key
Example
use cascara::Cache;

let mut cache = Cache::new(100);
assert!(cache.insert(1,2).is_ok());
assert_eq!(cache.get(&1), Some(&2));

Return mutable item ref if is in cache

Arguments
  • k: item key
Example
use cascara::Cache;

let mut cache = Cache::new(100);
assert!(cache.insert(1,1).is_ok());
let v = cache.get_mut(&1);
assert!(v.is_some());
if let Some(v) = v {
    *v = 2;
}
assert_eq!(cache.get(&1), Some(&2));

Insert item into cache. Item can be rejected (return Err) if cache is full and estimate of new item is lower than sample item from cache. If item is inserted, than preview item value can be returned. Cache is cleaned and all expired items are removed before new is inserted.

Arguments
  • k: item key
  • v: item value
Example
use cascara::Cache;

let mut cache = Cache::new(100);
assert!(cache.insert(1,1).is_ok());
let v = cache.insert(1,2);
assert!(v.is_ok());
if let Ok(v) = v {
     assert_eq!(v, Some(1));
}

Insert item into cache with defined time to life in seconds. Returns preview item if exists with given key. Cache is cleaned and all expired items are removed before new is inserted.

If expiration time is 0 sec, than item is insert without ttl.

Arguments
  • k: item key
  • v: item value
  • expiration: how many seconds should item lives
Example
use cascara::Cache;
use std::time::Duration;

let mut cache = Cache::new(100);
assert!(cache.insert_with_ttl(1,1, Duration::from_secs(1)).is_ok());
assert!(cache.contains(&1));
std::thread::sleep(Duration::from_secs(2));
assert!(!cache.contains(&1));

Remove and return item from cache.

Arguments
  • k: item key
Example
use cascara::Cache;

let mut cache = Cache::new(100);
assert!(cache.insert(1,2).is_ok());
assert!(cache.contains(&1));
assert_eq!(cache.remove(&1), Some(2));
assert!(!cache.contains(&1));

Remove all items from cache.

Example
use cascara::Cache;

let mut cache = Cache::new(100);
assert!(cache.insert(1,1).is_ok());
assert!(cache.insert(2,2).is_ok());
assert!(cache.contains(&1));
assert!(cache.contains(&2));
cache.clear();
assert!(!cache.contains(&1));
assert!(!cache.contains(&2));

Return cache metrics

Example
use cascara::Cache;

let cache = Cache::<u8,u8>::new(100).with_metrics();
assert!(cache.metrics().is_some());

An iterator visiting all entries in order. The iterator element type is (&’a K, &’a V).

Example
use cascara::Cache;

let mut cache = Cache::<u8, u8>::new(10);
assert!(cache.insert(1, 1).is_ok());
assert!(cache.insert(2, 2).is_ok());
assert!(cache.insert(3, 3).is_ok());
for (key, val) in cache.iter() {
    println!("key: {} val: {}", key, val);
}

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.