[][src]Struct dashcache::Cache

pub struct Cache<T: ?Sized, C: Caches<T> = Arc<T>, S: BuildHasher + Clone = RandomState> { /* fields omitted */ }

A cache for values of type T

Implementations

impl<T: Hash + Eq> Cache<T>[src]

pub fn new() -> Cache<T>[src]

Create a new, empty cache

impl<T: Eq + Hash + ?Sized, C: Caches<T>, S: BuildHasher + Clone> Cache<T, C, S>[src]

pub fn cache<Q>(&self, value: Q) -> C where
    C: Borrow<Q>,
    Q: Into<C> + Hash + Eq
[src]

Attempt to cache a value. If already cached, return the corresponding Arc

Example

use dashcache::Cache;
use elysees::Arc;
let int_cache = Cache::<u64>::new();

let cached_32 = int_cache.cache(32);
let arc_32 = Arc::new(32);
// These are different allocations!
assert!(!Arc::ptr_eq(&arc_32, &cached_32));

// We can use the cache to de-duplicate allocations
let dedup_32 = int_cache.cache(arc_32.clone());
assert!(Arc::ptr_eq(&dedup_32, &cached_32));

// Similarly, we'll get the same `Arc` if we insert the value again
let new_32 = int_cache.cache(32);
assert!(Arc::ptr_eq(&new_32, &cached_32));

// We can also insert an `Arc` from the get-go:
let arc_44 = Arc::new(44);
let cached_44 = int_cache.cache(arc_44.clone());
assert!(Arc::ptr_eq(&arc_44, &cached_44));
// New insertions are also deduplicated:
let dedup_44 = int_cache.cache(44);
assert!(Arc::ptr_eq(&arc_44, &dedup_44));

pub fn gc(&self) -> usize[src]

Garbage-collect a given cache. Return how many values were collected.

Example

use dashcache::Cache;
use elysees::Arc;
let int_cache = Cache::<u64>::new();

// Let's stick 2 used values and 3 unused values into the cache:
let used_1 = int_cache.cache(77);
let used_2 = int_cache.cache(88);
int_cache.cache(99);
int_cache.cache(500);
int_cache.cache(81);
// We can see that at this point there are 5 things in the cache:
assert_eq!(int_cache.len(), 5);
// Now, let's garbage collect the cache, which should bring us down 3 things:
assert_eq!(int_cache.gc(), 3);
// And we have 2 things left:
assert_eq!(int_cache.len(), 2);
assert!(Arc::ptr_eq(&used_1, &int_cache.cache(77)));
assert!(Arc::ptr_eq(&used_2, &int_cache.cache(88)));

pub fn len(&self) -> usize[src]

Compute how many items are in a given cache.

Example

use dashcache::Cache;
let int_cache = Cache::<u64>::new();
assert_eq!(int_cache.len(), 0);
int_cache.cache(10);
assert_eq!(int_cache.len(), 1);
int_cache.cache(20);
assert_eq!(int_cache.len(), 2);
// Since 10 is already in the cache, this is a no-op:
int_cache.cache(10);
assert_eq!(int_cache.len(), 2);

pub fn is_empty(&self) -> bool[src]

Check if this value cache is empty.

Example

use dashcache::Cache;
let int_cache = Cache::<u64>::new();
assert!(int_cache.is_empty());
int_cache.cache(10);
assert!(!int_cache.is_empty());

Trait Implementations

impl<T: Debug + ?Sized, C: Debug + Caches<T>, S: Debug + BuildHasher + Clone> Debug for Cache<T, C, S>[src]

impl<T: ?Sized, C: Caches<T>, S: BuildHasher + Clone + Default> Default for Cache<T, C, S>[src]

Auto Trait Implementations

impl<T, C = Arc<T>, S = RandomState> !RefUnwindSafe for Cache<T, C, S>

impl<T: ?Sized, C, S> Send for Cache<T, C, S> where
    C: Send,
    S: Send,
    T: Send

impl<T: ?Sized, C, S> Sync for Cache<T, C, S> where
    C: Send + Sync,
    S: Send + Sync,
    T: Sync

impl<T: ?Sized, C, S> Unpin for Cache<T, C, S> where
    S: Unpin,
    T: Unpin

impl<T: ?Sized, C, S> UnwindSafe for Cache<T, C, S> where
    C: UnwindSafe,
    S: UnwindSafe,
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Erasable for T[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.