pub struct MemoryCache<K, V>{ /* private fields */ }cache only.Expand description
Thread-safe in-memory cache with TTL support
§Examples
use yimi_rutool::cache::MemoryCache;
use std::time::Duration;
let cache = MemoryCache::new();
// Store value without TTL
cache.put("key1".to_string(), "value1".to_string()).unwrap();
// Store value with TTL
cache.put_with_ttl("key2".to_string(), "value2".to_string(), Duration::from_secs(60)).unwrap();
// Retrieve values
assert_eq!(cache.get(&"key1".to_string()).unwrap(), Some("value1".to_string()));
assert_eq!(cache.get(&"key2".to_string()).unwrap(), Some("value2".to_string()));Implementations§
Source§impl<K, V> MemoryCache<K, V>
impl<K, V> MemoryCache<K, V>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new cache with no size limit and no default TTL
§Examples
use yimi_rutool::cache::MemoryCache;
let cache: MemoryCache<String, i32> = MemoryCache::new();Sourcepub fn with_ttl(default_ttl: Duration) -> Self
pub fn with_ttl(default_ttl: Duration) -> Self
Create a new cache with default TTL
§Examples
use yimi_rutool::cache::MemoryCache;
use std::time::Duration;
let cache: MemoryCache<String, i32> = MemoryCache::with_ttl(Duration::from_secs(300));Sourcepub fn with_max_size(max_size: usize) -> Self
pub fn with_max_size(max_size: usize) -> Self
Create a new cache with maximum size limit
§Examples
use yimi_rutool::cache::MemoryCache;
let cache: MemoryCache<String, i32> = MemoryCache::with_max_size(1000);Sourcepub fn with_ttl_and_size(default_ttl: Duration, max_size: usize) -> Self
pub fn with_ttl_and_size(default_ttl: Duration, max_size: usize) -> Self
Create a new cache with both default TTL and maximum size
§Examples
use yimi_rutool::cache::MemoryCache;
use std::time::Duration;
let cache: MemoryCache<String, i32> = MemoryCache::with_ttl_and_size(
Duration::from_secs(300),
1000
);Sourcepub fn put(&self, key: K, value: V) -> Result<()>
pub fn put(&self, key: K, value: V) -> Result<()>
Store a value in the cache with default TTL
§Examples
use yimi_rutool::cache::MemoryCache;
let cache = MemoryCache::new();
cache.put("key".to_string(), "value".to_string()).unwrap();Sourcepub fn put_with_ttl(&self, key: K, value: V, ttl: Duration) -> Result<()>
pub fn put_with_ttl(&self, key: K, value: V, ttl: Duration) -> Result<()>
Store a value in the cache with specific TTL
§Examples
use yimi_rutool::cache::MemoryCache;
use std::time::Duration;
let cache = MemoryCache::new();
cache.put_with_ttl("key", "value", Duration::from_secs(60)).unwrap();Sourcepub fn put_permanent(&self, key: K, value: V) -> Result<()>
pub fn put_permanent(&self, key: K, value: V) -> Result<()>
Store a value in the cache without TTL (never expires)
§Examples
use yimi_rutool::cache::MemoryCache;
let cache = MemoryCache::new();
cache.put_permanent("key".to_string(), "value".to_string()).unwrap();Sourcepub fn get(&self, key: &K) -> Result<Option<V>>
pub fn get(&self, key: &K) -> Result<Option<V>>
Retrieve a value from the cache
§Examples
use yimi_rutool::cache::MemoryCache;
let cache = MemoryCache::new();
cache.put("key".to_string(), "value".to_string()).unwrap();
let result = cache.get(&"key".to_string()).unwrap();
assert_eq!(result, Some("value".to_string()));Sourcepub fn contains_key(&self, key: &K) -> Result<bool>
pub fn contains_key(&self, key: &K) -> Result<bool>
Check if a key exists in the cache (without updating access time)
§Examples
use yimi_rutool::cache::MemoryCache;
let cache = MemoryCache::new();
cache.put("key".to_string(), "value".to_string()).unwrap();
assert!(cache.contains_key(&"key".to_string()).unwrap());
assert!(!cache.contains_key(&"nonexistent".to_string()).unwrap());Sourcepub fn remove(&self, key: &K) -> Result<Option<V>>
pub fn remove(&self, key: &K) -> Result<Option<V>>
Remove a value from the cache
§Examples
use yimi_rutool::cache::MemoryCache;
let cache = MemoryCache::new();
cache.put("key".to_string(), "value".to_string()).unwrap();
let removed = cache.remove(&"key".to_string()).unwrap();
assert_eq!(removed, Some("value".to_string()));Sourcepub fn clear(&self) -> Result<()>
pub fn clear(&self) -> Result<()>
Clear all entries from the cache
§Examples
use yimi_rutool::cache::MemoryCache;
let cache = MemoryCache::new();
cache.put("key1".to_string(), "value1".to_string()).unwrap();
cache.put("key2".to_string(), "value2".to_string()).unwrap();
cache.clear().unwrap();
assert_eq!(cache.size().unwrap(), 0);Sourcepub fn size(&self) -> Result<usize>
pub fn size(&self) -> Result<usize>
Get the number of entries in the cache
§Examples
use yimi_rutool::cache::MemoryCache;
let cache = MemoryCache::new();
cache.put("key1".to_string(), "value1".to_string()).unwrap();
cache.put("key2".to_string(), "value2".to_string()).unwrap();
assert_eq!(cache.size().unwrap(), 2);Sourcepub fn is_empty(&self) -> Result<bool>
pub fn is_empty(&self) -> Result<bool>
Check if the cache is empty
§Examples
use yimi_rutool::cache::MemoryCache;
let cache = MemoryCache::new();
assert!(cache.is_empty().unwrap());
cache.put("key".to_string(), "value".to_string()).unwrap();
assert!(!cache.is_empty().unwrap());Sourcepub fn cleanup_expired(&self) -> Result<usize>
pub fn cleanup_expired(&self) -> Result<usize>
Remove all expired entries from the cache
§Examples
use yimi_rutool::cache::MemoryCache;
use std::time::Duration;
let cache = MemoryCache::new();
cache.put_with_ttl("key", "value", Duration::from_millis(1)).unwrap();
std::thread::sleep(Duration::from_millis(10));
let removed = cache.cleanup_expired().unwrap();
assert_eq!(removed, 1);Sourcepub fn keys(&self) -> Result<Vec<K>>
pub fn keys(&self) -> Result<Vec<K>>
Get all keys in the cache (excluding expired ones)
§Examples
use yimi_rutool::cache::MemoryCache;
let cache = MemoryCache::new();
cache.put("key1".to_string(), "value1".to_string()).unwrap();
cache.put("key2".to_string(), "value2".to_string()).unwrap();
let keys = cache.keys().unwrap();
assert_eq!(keys.len(), 2);Sourcepub fn stats(&self) -> Result<CacheStats>
pub fn stats(&self) -> Result<CacheStats>
Get cache statistics
§Examples
use yimi_rutool::cache::MemoryCache;
let cache = MemoryCache::new();
cache.put("key1".to_string(), "value1".to_string()).unwrap();
cache.put("key2".to_string(), "value2".to_string()).unwrap();
let stats = cache.stats().unwrap();
assert_eq!(stats.total_entries, 2);Sourcepub fn get_or_compute<F>(&self, key: K, compute_fn: F) -> Result<V>where
F: FnOnce() -> V,
pub fn get_or_compute<F>(&self, key: K, compute_fn: F) -> Result<V>where
F: FnOnce() -> V,
Get or compute a value for the given key
§Examples
use yimi_rutool::cache::MemoryCache;
let cache = MemoryCache::new();
let value = cache.get_or_compute("key".to_string(), || "computed_value".to_string()).unwrap();
assert_eq!(value, "computed_value");
// Second call should return cached value
let cached_value = cache.get_or_compute("key".to_string(), || "new_value".to_string()).unwrap();
assert_eq!(cached_value, "computed_value");Sourcepub fn get_or_compute_with_ttl<F>(
&self,
key: K,
compute_fn: F,
ttl: Duration,
) -> Result<V>where
F: FnOnce() -> V,
pub fn get_or_compute_with_ttl<F>(
&self,
key: K,
compute_fn: F,
ttl: Duration,
) -> Result<V>where
F: FnOnce() -> V,
Get or compute a value with TTL for the given key
§Examples
use yimi_rutool::cache::MemoryCache;
use std::time::Duration;
let cache = MemoryCache::new();
let value = cache.get_or_compute_with_ttl(
"key",
|| "computed_value".to_string(),
Duration::from_secs(60)
).unwrap();
assert_eq!(value, "computed_value");Trait Implementations§
Source§impl<K, V> Clone for MemoryCache<K, V>
impl<K, V> Clone for MemoryCache<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for MemoryCache<K, V>
impl<K, V> RefUnwindSafe for MemoryCache<K, V>
impl<K, V> Send for MemoryCache<K, V>
impl<K, V> Sync for MemoryCache<K, V>
impl<K, V> Unpin for MemoryCache<K, V>
impl<K, V> UnsafeUnpin for MemoryCache<K, V>
impl<K, V> UnwindSafe for MemoryCache<K, V>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().