pub struct MemoryStore<V, K = String>{ /* private fields */ }Expand description
A thread-safe in-memory key-value store.
This store is designed to be WASM-compatible and can be used to cache
any type of data that implements Clone. It uses interior mutability
via RwLock to allow concurrent read access.
§Type Parameters
K: The key type, must implementEq + Hash + CloneV: The value type, must implementClone
Implementations§
Source§impl<V, K> MemoryStore<V, K>
impl<V, K> MemoryStore<V, K>
Sourcepub fn new(config: MemoryStoreConfig) -> Self
pub fn new(config: MemoryStoreConfig) -> Self
Create a new memory store with the given configuration.
Sourcepub fn insert(&self, key: K, value: V)
pub fn insert(&self, key: K, value: V)
Insert a value into the store.
If the store has a maximum entry limit and is at capacity, an arbitrary entry will be evicted to make room.
Sourcepub fn get(&self, key: &K) -> Option<V>
pub fn get(&self, key: &K) -> Option<V>
Get a value from the store.
Returns Some(value) if the key exists, None otherwise.
Sourcepub fn remove(&self, key: &K) -> Option<V>
pub fn remove(&self, key: &K) -> Option<V>
Remove a value from the store.
Returns the removed value if the key existed.
Sourcepub fn stats(&self) -> MemoryStoreStats
pub fn stats(&self) -> MemoryStoreStats
Get statistics for the store.
Only meaningful if track_stats was enabled in the config.
Sourcepub fn reset_stats(&self)
pub fn reset_stats(&self)
Reset statistics.
Sourcepub fn get_or_insert_with<F>(&self, key: K, factory: F) -> Vwhere
F: FnOnce() -> V,
pub fn get_or_insert_with<F>(&self, key: K, factory: F) -> Vwhere
F: FnOnce() -> V,
Get or insert a value using a factory function.
If the key exists, returns the existing value. If the key doesn’t exist, calls the factory function to create a value, inserts it, and returns it.