Skip to main content

CuckooCache

Struct CuckooCache 

Source
pub struct CuckooCache { /* private fields */ }
Expand description

A pre-allocated cache that uses cuckoo hashing with D=4 candidate positions per key. Items are stored inline in fixed-size slots within a contiguous array.

Implementations§

Source§

impl CuckooCache

Source

pub fn builder() -> Builder

Returns a new Builder for configuring a CuckooCache.

use cuckoo_cache::CuckooCache;

let cache = CuckooCache::builder()
    .nitem(4096)
    .item_size(64)
    .build();
Source

pub fn get(&mut self, key: &[u8]) -> Option<Item>

Look up an item by key.

use cuckoo_cache::CuckooCache;
use std::time::Duration;

let mut cache = CuckooCache::builder().build();
assert!(cache.get(b"coffee").is_none());

cache.insert(b"coffee", b"strong", Duration::ZERO).unwrap();
let item = cache.get(b"coffee").unwrap();
assert_eq!(item.value(), b"strong");
Source

pub fn insert<'a, T: Into<Value<'a>>>( &mut self, key: &[u8], value: T, ttl: Duration, ) -> Result<(), CuckooCacheError>

Insert an item into the cache.

use cuckoo_cache::CuckooCache;
use std::time::Duration;

let mut cache = CuckooCache::builder().build();
cache.insert(b"drink", b"coffee", Duration::ZERO).unwrap();

let item = cache.get(b"drink").unwrap();
assert_eq!(item.value(), b"coffee");

cache.insert(b"drink", b"whisky", Duration::ZERO).unwrap();
let item = cache.get(b"drink").unwrap();
assert_eq!(item.value(), b"whisky");
Source

pub fn delete(&mut self, key: &[u8]) -> bool

Remove the item with the given key.

use cuckoo_cache::CuckooCache;
use std::time::Duration;

let mut cache = CuckooCache::builder().build();
assert!(!cache.delete(b"coffee"));

cache.insert(b"coffee", b"strong", Duration::ZERO).unwrap();
assert!(cache.delete(b"coffee"));
assert!(cache.get(b"coffee").is_none());
Source

pub fn clear(&mut self)

Clear all items from the cache.

Source

pub fn wrapping_add( &mut self, key: &[u8], rhs: u64, ) -> Result<Item, CuckooCacheError>

Perform a wrapping addition on a numeric value.

Source

pub fn saturating_sub( &mut self, key: &[u8], rhs: u64, ) -> Result<Item, CuckooCacheError>

Perform a saturating subtraction on a numeric value.

Auto Trait Implementations§

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.