pub struct BoundedLruCache<K, V>{ /* private fields */ }Expand description
Thread-safe bounded LRU cache.
Implements a least-recently-used eviction policy with configurable size limits and optional TTL expiration.
§Type Parameters
K- Cache key type (must implement Hash + Eq)V- Cache value type
§Performance
- Insert: O(1) amortized
- Get: O(1) amortized
- Eviction: O(1) amortized
Implementations§
Source§impl<K, V> BoundedLruCache<K, V>
impl<K, V> BoundedLruCache<K, V>
Sourcepub fn new(config: CacheConfig) -> BoundedLruCache<K, V>
pub fn new(config: CacheConfig) -> BoundedLruCache<K, V>
Create a new bounded LRU cache with the given configuration.
§Arguments
config- Cache configuration (size limits, TTL, etc.)
§Returns
A new bounded LRU cache instance.
§Examples
use perl_workspace_index::workspace::cache::{BoundedLruCache, CacheConfig};
let config = CacheConfig {
max_items: 1000,
max_bytes: 10 * 1024 * 1024, // 10MB
ttl: None,
};
let cache: BoundedLruCache<String, String> = BoundedLruCache::new(config);Sourcepub fn default() -> BoundedLruCache<K, V>
pub fn default() -> BoundedLruCache<K, V>
Sourcepub fn insert_with_size(&self, key: K, value: V, size_bytes: usize) -> bool
pub fn insert_with_size(&self, key: K, value: V, size_bytes: usize) -> bool
Insert a value into the cache.
If the cache is at capacity, the least recently used entry will be evicted.
§Arguments
key- Cache keyvalue- Value to cachesize_bytes- Size of the value in bytes (for memory tracking)
§Returns
true if the value was inserted, false if evicted due to size limits.
§Examples
use perl_workspace_index::workspace::cache::BoundedLruCache;
let mut cache = BoundedLruCache::default();
cache.insert_with_size("key", "value", 5);Sourcepub fn insert(&self, key: K, value: V)where
V: EstimateSize,
pub fn insert(&self, key: K, value: V)where
V: EstimateSize,
Insert a value into the cache with estimated size.
Uses a simple size estimation based on the value’s memory representation.
§Arguments
key- Cache keyvalue- Value to cache
§Returns
true if the value was inserted, false if evicted.
§Examples
use perl_workspace_index::workspace::cache::BoundedLruCache;
let mut cache = BoundedLruCache::default();
cache.insert("key", "value");Sourcepub fn get(&self, key: &K) -> Option<V>where
V: Clone,
pub fn get(&self, key: &K) -> Option<V>where
V: Clone,
Get a value from the cache.
Returns None if the key is not found or the entry has expired.
§Arguments
key- Cache key to look up
§Returns
Some(&V) if found and not expired, None otherwise.
§Examples
use perl_workspace_index::workspace::cache::BoundedLruCache;
let mut cache = BoundedLruCache::default();
cache.insert("key", "value");
assert_eq!(cache.get("key"), Some(&"value"));Sourcepub fn peek(&self, key: &K) -> Option<V>where
V: Clone,
pub fn peek(&self, key: &K) -> Option<V>where
V: Clone,
Peek a value from the cache without changing hit/miss counters.
Expired entries are still removed so stale data does not linger.
Sourcepub fn remove(&self, key: &K) -> Option<V>where
V: Clone,
pub fn remove(&self, key: &K) -> Option<V>where
V: Clone,
Remove a value from the cache.
§Arguments
key- Cache key to remove
§Returns
Some(V) if the entry was removed, None if not found.
§Examples
use perl_workspace_index::workspace::cache::BoundedLruCache;
let mut cache = BoundedLruCache::default();
cache.insert("key", "value");
assert_eq!(cache.remove("key"), Some("value"));Sourcepub fn clear(&self)
pub fn clear(&self)
Clear all entries from the cache.
§Examples
use perl_workspace_index::workspace::cache::BoundedLruCache;
let mut cache = BoundedLruCache::default();
cache.insert("key", "value");
cache.clear();
assert!(cache.is_empty());