Struct PaperCache

Source
pub struct PaperCache<K, V, S = RandomState> { /* private fields */ }

Implementations§

Source§

impl<K, V, S> PaperCache<K, V, S>
where K: 'static + Eq + Hash + TypeSize, V: 'static + TypeSize, S: Default + Clone + BuildHasher,

Source

pub fn new( max_size: CacheSize, policies: &[PaperPolicy], policy: PaperPolicy, ) -> Result<Self, CacheError>

Creates an empty PaperCache with maximum size max_size and eviction policy policy. If the maximum size is zero, a CacheError will be returned.

§Examples
use paper_cache::{PaperCache, PaperPolicy};

let cache = PaperCache::<u32, u32>::new(
    1000,
    &[PaperPolicy::Lfu],
    PaperPolicy::Lfu,
);

assert!(cache.is_ok());

// Supplying a maximum size of zero will return a `CacheError`.
let cache = PaperCache::<u32, u32>::new(
    0,
    &[PaperPolicy::Lfu],
    PaperPolicy::Lfu,
);

assert!(cache.is_err());

// Supplying duplicate policies will return a `CacheError`.
let cache = PaperCache::<u32, u32>::new(
    1000,
    &[PaperPolicy::Lfu, PaperPolicy::Lru, PaperPolicy::Lfu],
    PaperPolicy::Lfu,
);

assert!(cache.is_err());

// Supplying a non-configured policy will return a `CacheError`.
let cache = PaperCache::<u32, u32>::new(
    1000,
    &[PaperPolicy::Lfu],
    PaperPolicy::Lru,
);

assert!(cache.is_err());
Source

pub fn with_hasher( max_size: CacheSize, policies: &[PaperPolicy], policy: PaperPolicy, hasher: S, ) -> Result<Self, CacheError>

Creates an empty PaperCache with the supplied hasher.

§Examples
use std::hash::RandomState;
use paper_cache::{PaperCache, PaperPolicy};

let cache = PaperCache::<u32, u32>::with_hasher(
    1000,
    &[PaperPolicy::Lfu],
    PaperPolicy::Lfu,
    RandomState::default(),
);

assert!(cache.is_ok());
Source

pub fn version(&self) -> String

Returns the current cache version.

§Examples
use paper_cache::{PaperCache, PaperPolicy};

let mut cache = PaperCache::<u32, u32>::new(
    1000,
    &[PaperPolicy::Lfu],
    PaperPolicy::Lfu
).unwrap();

assert_eq!(cache.version(), env!("CARGO_PKG_VERSION"));
Source

pub fn status(&self) -> Result<Status, CacheError>

Returns the current statistics.

§Examples
use paper_cache::{PaperCache, PaperPolicy};

let mut cache = PaperCache::<u32, u32>::new(
    1000,
    &[PaperPolicy::Lfu],
    PaperPolicy::Lfu,
).unwrap();

cache.set(0, 0, None);

let status = cache.status().unwrap();
assert!(status.used_size() > 0);
Source

pub fn get(&self, key: &K) -> Result<Arc<V>, CacheError>

Gets the value associated with the supplied key. If the key was not found in the cache, returns a CacheError.

§Examples
use paper_cache::{PaperCache, PaperPolicy};

let mut cache = PaperCache::<u32, u32>::new(
    1000,
    &[PaperPolicy::Lfu],
    PaperPolicy::Lfu,
).unwrap();

cache.set(0, 0, None);

// Getting a key which exists in the cache will return the associated value.
assert!(cache.get(&0).is_ok());
// Getting a key which does not exist in the cache will return a CacheError.
assert!(cache.get(&1).is_err());
Source

pub fn set(&self, key: K, value: V, ttl: Option<u32>) -> Result<(), CacheError>

Sets the supplied key and value in the cache. Returns a CacheError if the value size is zero or larger than the cache’s maximum size.

If the key already exists in the cache, the associated value is updated to the supplied value.

§Examples
use paper_cache::{PaperCache, PaperPolicy};

let mut cache = PaperCache::<u32, u32>::new(
    1000,
    &[PaperPolicy::Lfu],
    PaperPolicy::Lfu,
).unwrap();

assert!(cache.set(0, 0, None).is_ok());
Source

pub fn del(&self, key: &K) -> Result<(), CacheError>

Deletes the object associated with the supplied key in the cache. Returns a CacheError if the key was not found in the cache.

§Examples
use paper_cache::{PaperCache, PaperPolicy};

let mut cache = PaperCache::<u32, u32>::new(
    1000,
    &[PaperPolicy::Lfu],
    PaperPolicy::Lfu,
).unwrap();

cache.set(0, 0, None);
assert!(cache.del(&0).is_ok());

// Deleting a key which does not exist in the cache will return a CacheError.
assert!(cache.del(&1).is_err());
Source

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

Checks if an object with the supplied key exists in the cache without altering any of the cache’s internal queues.

§Examples
use paper_cache::{PaperCache, PaperPolicy};

let mut cache = PaperCache::<u32, u32>::new(
    1000,
    &[PaperPolicy::Lfu],
    PaperPolicy::Lfu,
).unwrap();

cache.set(0, 0, None);

assert!(cache.has(&0));
assert!(!cache.has(&1));
Source

pub fn peek(&self, key: &K) -> Result<Arc<V>, CacheError>

Gets (peeks) the value associated with the supplied key without altering any of the cache’s internal queues. If the key was not found in the cache, returns a CacheError.

§Examples
use paper_cache::{PaperCache, PaperPolicy};

let mut cache = PaperCache::<u32, u32>::new(
    1000,
    &[PaperPolicy::Lfu],
    PaperPolicy::Lfu,
).unwrap();

cache.set(0, 0, None);
cache.set(1, 0, None);

// Peeking a key which exists in the cache will return the associated value.
assert!(cache.peek(&0).is_ok());
// Peeking a key which does not exist in the cache will return a CacheError.
assert!(cache.peek(&2).is_err());

cache.set(2, 0, None);

// Peeking a key will not alter the eviction order of the objects.
assert!(cache.peek(&1).is_ok());
assert!(cache.peek(&2).is_ok());
Source

pub fn ttl(&self, key: &K, ttl: Option<u32>) -> Result<(), CacheError>

Sets the TTL associated with the supplied key. If the key was not found in the cache, returns a CacheError.

§Examples
use paper_cache::{PaperCache, PaperPolicy};

let mut cache = PaperCache::<u32, u32>::new(
    1000,
    &[PaperPolicy::Lfu],
    PaperPolicy::Lfu,
).unwrap();

cache.set(0, 0, None); // value will not expire
cache.ttl(&0, Some(5)); // value will expire in 5 seconds
Source

pub fn size(&self, key: &K) -> Result<u32, CacheError>

Gets the size of the value associated with the supplied key in bytes. If the key was not found in the cache, returns a CacheError.

§Examples
use paper_cache::{PaperCache, PaperPolicy};

let mut cache = PaperCache::<u32, u32>::new(
    1000,
    &[PaperPolicy::Lfu],
    PaperPolicy::Lfu,
).unwrap();

cache.set(0, 0, None);

// Sizing a key which exists in the cache will return the size of the associated value.
assert!(cache.size(&0).is_ok());
// Sizing a key which does not exist in the cache will return a CacheError.
assert!(cache.size(&1).is_err());
Source

pub fn wipe(&self) -> Result<(), CacheError>

Deletes all objects in the cache and sets the cache’s used size to zero. Returns a CacheError if the objects could not be wiped.

§Examples
use paper_cache::{PaperCache, PaperPolicy};

let mut cache = PaperCache::<u32, u32>::new(
    1000,
    &[PaperPolicy::Lfu],
    PaperPolicy::Lfu,
).unwrap();

cache.wipe();
Source

pub fn resize(&self, max_size: CacheSize) -> Result<(), CacheError>

Resizes the cache to the supplied maximum size. If the supplied size is zero, returns a CacheError.

§Examples
use paper_cache::{PaperCache, PaperPolicy};

let mut cache = PaperCache::<u32, u32>::new(
    1000,
    &[PaperPolicy::Lfu],
    PaperPolicy::Lfu,
).unwrap();

assert!(cache.resize(1).is_ok());

// Resizing to a size of zero will return a CacheError.
assert!(cache.resize(0).is_err());
Source

pub fn policy(&self, policy: PaperPolicy) -> Result<(), CacheError>

Sets the eviction policy of the cache to the supplied policy.

§Examples
use paper_cache::{PaperCache, PaperPolicy};

let mut cache = PaperCache::<u32, u32>::new(
    1000,
    &[PaperPolicy::Lfu],
    PaperPolicy::Lfu,
).unwrap();

assert!(cache.policy(PaperPolicy::Lfu).is_ok());
assert!(cache.policy(PaperPolicy::Lru).is_err());

Trait Implementations§

Source§

impl<K, V, S> Send for PaperCache<K, V, S>

Auto Trait Implementations§

§

impl<K, V, S> Freeze for PaperCache<K, V, S>
where S: Freeze,

§

impl<K, V, S = RandomState> !RefUnwindSafe for PaperCache<K, V, S>

§

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

§

impl<K, V, S> Unpin for PaperCache<K, V, S>
where S: Unpin,

§

impl<K, V, S = RandomState> !UnwindSafe for PaperCache<K, V, S>

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

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V