pub struct MemoryCache<A, B> { /* private fields */ }
Expand description
Represents a local in-memory cache.
Implementations§
Source§impl<A: Hash + Eq, B> MemoryCache<A, B>
impl<A: Hash + Eq, B> MemoryCache<A, B>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty MemoryCache
.
§Example
use memory_cache::MemoryCache;
use std::time::Duration;
let mut cache = MemoryCache::new();
let key: &'static str = "key";
let value: &'static str = "Hello, World!";
cache.insert(key, value, None);
assert_eq!(cache.get(&key), Some(&value));
Sourcepub fn with_full_scan(full_scan_frequency: Duration) -> Self
pub fn with_full_scan(full_scan_frequency: Duration) -> Self
Creates an empty MemoryCache
with periodic full scan to identify expired keys.
§Example
use memory_cache::MemoryCache;
use std::time::Duration;
let scan_frequency = Duration::from_secs(60);
let mut cache = MemoryCache::with_full_scan(scan_frequency);
let key: &'static str = "key";
let value: &'static str = "Hello, World!";
cache.insert(key, value, None);
assert_eq!(cache.get(&key), Some(&value));
Sourcepub fn contains_key(&self, key: &A) -> bool
pub fn contains_key(&self, key: &A) -> bool
Determines whether the MemoryCache<A, B>
contains the specified key.
§Example
use memory_cache::MemoryCache;
use std::time::Duration;
let mut cache = MemoryCache::new();
let key: &'static str = "key";
let value: &'static str = "Hello, World!";
cache.insert(key, value, None);
assert!(cache.contains_key(&key));
Sourcepub fn get_last_scan_time(&self) -> Option<SystemTime>
pub fn get_last_scan_time(&self) -> Option<SystemTime>
Gets the last scan time.
None
If there were no scans.
§Example
use memory_cache::MemoryCache;
use std::time::{Duration, SystemTime};
let mut cache = MemoryCache::new();
let key: &'static str = "key";
let value: &'static str = "Hello, World!";
cache.insert(key, value, None);
assert_eq!(cache.get_last_scan_time(), None);
Sourcepub fn get_full_scan_frequency(&self) -> Option<Duration>
pub fn get_full_scan_frequency(&self) -> Option<Duration>
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::with_full_scan(scan_frequency);
let key: &'static str = "key";
let value: &'static str = "Hello, World!";
cache.insert(key, value, None);
assert_eq!(cache.get_full_scan_frequency(), Some(scan_frequency));
Sourcepub fn get(&self, key: &A) -> Option<&B>
pub fn get(&self, key: &A) -> Option<&B>
Gets the value associated with the specified key.
§Example
use memory_cache::MemoryCache;
use std::time::Duration;
let mut cache = MemoryCache::new();
let key: &'static str = "key";
let value: &'static str = "Hello, World!";
cache.insert(key, value, None);
assert_eq!(cache.get(&key), Some(&value));
Sourcepub fn get_or_insert<F>(
&mut self,
key: A,
factory: F,
lifetime: Option<Duration>,
) -> &Bwhere
F: Fn() -> B,
pub fn get_or_insert<F>(
&mut self,
key: A,
factory: F,
lifetime: Option<Duration>,
) -> &Bwhere
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 mut cache = MemoryCache::new();
let key: &'static str = "key";
let value: &'static str = "Hello, World!";
assert_eq!(cache.get_or_insert(key, move || value, None), &value);
assert!(cache.contains_key(&key));
Sourcepub fn insert(
&mut self,
key: A,
value: B,
lifetime: Option<Duration>,
) -> Option<B>
pub fn insert( &mut self, key: A, value: B, lifetime: Option<Duration>, ) -> Option<B>
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 mut cache = MemoryCache::new();
let key: &'static str = "key";
let value: &'static str = "Hello, World!";
cache.insert(key, value, None);
assert_eq!(cache.get(&key), Some(&value));
Sourcepub fn remove(&mut self, key: &A) -> Option<B>
pub fn remove(&mut self, key: &A) -> Option<B>
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 mut cache = MemoryCache::new();
let key: &'static str = "key";
let value: &'static str = "Hello, World!";
cache.insert(key, value, None);
assert_eq!(cache.remove(&key), Some(value));
assert!(!cache.contains_key(&key));
Auto Trait Implementations§
impl<A, B> Freeze for MemoryCache<A, B>
impl<A, B> RefUnwindSafe for MemoryCache<A, B>where
A: RefUnwindSafe,
B: RefUnwindSafe,
impl<A, B> Send for MemoryCache<A, B>
impl<A, B> Sync for MemoryCache<A, B>
impl<A, B> Unpin for MemoryCache<A, B>
impl<A, B> UnwindSafe for MemoryCache<A, B>where
A: UnwindSafe,
B: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more