[][src]Struct memory_cache::MemoryCache

pub struct MemoryCache<A, B> { /* fields omitted */ }

Represents a local in-memory cache.

Methods

impl<A: Hash + Eq, B> MemoryCache<A, B>[src]

pub fn new(full_scan_frequency: Duration) -> MemoryCache<A, B>[src]

Creates an empty MemoryCache.

Example

use memory_cache::MemoryCache;
use std::time::Duration;

let scan_frequency = Duration::from_secs(60);

let mut cache = MemoryCache::new(scan_frequency);

let key: &'static str = "key";
let value: &'static str = "Hello, World!";

cache.set(key, value, None);
let cached_value = cache.get(&key);

assert_eq!(cached_value, Some(&value));

pub fn has_key(&self, key: &A) -> bool[src]

Determines whether the MemoryCache<A, B> contains the specified key.

Example

use memory_cache::MemoryCache;
use std::time::Duration;

let scan_frequency = Duration::from_secs(60);

let mut cache = MemoryCache::new(scan_frequency);

let key: &'static str = "key";
let value: &'static str = "Hello, World!";

cache.set(key, value, None);

assert!(cache.has_key(&key));

pub fn get_last_scan_time(&self) -> Option<&SystemTime>[src]

Gets the last scan time.

  • None If there were no scans.

Example

use memory_cache::MemoryCache;
use std::time::{Duration, SystemTime};

let scan_frequency = Duration::from_secs(60);

let mut cache = MemoryCache::new(scan_frequency);

let key: &'static str = "key";
let value: &'static str = "Hello, World!";

cache.set(key, value, None);

assert_eq!(cache.get_last_scan_time(), None);

pub fn get_full_scan_frequency(&self) -> &Duration[src]

Gets the full scan frequency.

Example

use memory_cache::MemoryCache;
use std::time::{Duration, SystemTime};

let scan_frequency = Duration::from_secs(60);

let mut cache = MemoryCache::new(scan_frequency);

let key: &'static str = "key";
let value: &'static str = "Hello, World!";

cache.set(key, value, None);

assert_eq!(cache.get_full_scan_frequency(), &scan_frequency);

pub fn get(&self, key: &A) -> Option<&B>[src]

Gets the value associated with the specified key.

Example

use memory_cache::MemoryCache;
use std::time::Duration;

let scan_frequency = Duration::from_secs(60);

let mut cache = MemoryCache::new(scan_frequency);

let key: &'static str = "key";
let value: &'static str = "Hello, World!";

cache.set(key, value, None);

assert_eq!(cache.get(&key), Some(&value));

pub fn get_or_set<F>(
    &mut self,
    key: A,
    factory: F,
    duration: Option<Duration>
) -> &B where
    F: Fn() -> B, 
[src]

Gets the value associated with the specified key, or if the key can not be found, creates and insert value using the factory function.

Example

use memory_cache::MemoryCache;
use std::time::Duration;

let scan_frequency = Duration::from_secs(60);

let mut cache = MemoryCache::new(scan_frequency);

let key: &'static str = "key";
let value: &'static str = "Hello, World!";

assert_eq!(cache.get_or_set(key, move || value, None), &value);
assert!(cache.has_key(&key));

pub fn set(&mut self, key: A, value: B, duration: Option<Duration>)[src]

Inserts a cache entry into the cache by using a key and a value.

Example

use memory_cache::MemoryCache;
use std::time::Duration;

let scan_frequency = Duration::from_secs(60);

let mut cache = MemoryCache::new(scan_frequency);

let key: &'static str = "key";
let value: &'static str = "Hello, World!";

cache.set(key, value, None);

assert_eq!(cache.get(&key), Some(&value));

pub fn remove(&mut self, key: &A) -> Option<B>[src]

Removes a cache entry from the cache, returning the value at the key if the key was previously in the cache.

Example

use memory_cache::MemoryCache;
use std::time::Duration;

let scan_frequency = Duration::from_secs(60);

let mut cache = MemoryCache::new(scan_frequency);

let key: &'static str = "key";
let value: &'static str = "Hello, World!";

cache.set(key, value, None);

assert_eq!(cache.remove(&key), Some(value));
assert!(!cache.has_key(&key));

Auto Trait Implementations

impl<A, B> RefUnwindSafe for MemoryCache<A, B> where
    A: RefUnwindSafe,
    B: RefUnwindSafe

impl<A, B> Send for MemoryCache<A, B> where
    A: Send,
    B: Send

impl<A, B> Sync for MemoryCache<A, B> where
    A: Sync,
    B: Sync

impl<A, B> Unpin for MemoryCache<A, B> where
    A: Unpin,
    B: Unpin

impl<A, B> UnwindSafe for MemoryCache<A, B> where
    A: UnwindSafe,
    B: 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> 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.