[−][src]Struct memory_cache::MemoryCache
Represents a local in-memory cache.
Methods
impl<A: Hash + Eq, B> MemoryCache<A, B>
[src]
pub fn new(full_scan_frequency: Duration) -> Self
[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]
&mut self,
key: A,
factory: F,
duration: Option<Duration>
) -> &B where
F: Fn() -> B,
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>) -> Option<B>
[src]
Inserts a key-value pair into the cache.
If the cache did not have this key present, None
is returned.
If the cache did have this key present, the value is updated, and the old value is returned.
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 key 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,
A: RefUnwindSafe,
B: RefUnwindSafe,
impl<A, B> Send for MemoryCache<A, B> where
A: Send,
B: Send,
A: Send,
B: Send,
impl<A, B> Sync for MemoryCache<A, B> where
A: Sync,
B: Sync,
A: Sync,
B: Sync,
impl<A, B> Unpin for MemoryCache<A, B> where
A: Unpin,
B: Unpin,
A: Unpin,
B: Unpin,
impl<A, B> UnwindSafe for MemoryCache<A, B> where
A: UnwindSafe,
B: UnwindSafe,
A: UnwindSafe,
B: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,