pub struct MemoryStore { /* private fields */ }Expand description
In-memory store implementation
Thread-safe in-memory store using RwLock for concurrent access.
Implementations§
Source§impl MemoryStore
impl MemoryStore
Sourcepub fn new() -> MemoryStore
pub fn new() -> MemoryStore
Create new in-memory store
Sourcepub fn with_vector_search(self, config: IndexConfig) -> MemoryStore
pub fn with_vector_search(self, config: IndexConfig) -> MemoryStore
Sourcepub const fn with_ttl_config(self, config: TTLConfig) -> MemoryStore
pub const fn with_ttl_config(self, config: TTLConfig) -> MemoryStore
Sourcepub async fn sweep_expired_items(&self) -> Result<usize, StoreError>
pub async fn sweep_expired_items(&self) -> Result<usize, StoreError>
Sweep expired items from the store, returning the count of removed items.
This method iterates through all namespaces and their items, collecting
keys where expires_at < now. It removes at most sweep_max_items
expired items to avoid blocking other operations for extended periods.
The sweep is cooperative with the lazy cleanup in get() and search():
expired items are removed during normal reads, while this method performs
bulk cleanup in the background.
§Errors
Returns StoreError::Storage if the sweep operation encounters an
unexpected error (currently unused, reserved for future extensions).
§Examples
use juncture_core::store::{MemoryStore, Store, TTLConfig};
use std::time::Duration;
use serde_json::json;
let store = MemoryStore::new().with_ttl_config(TTLConfig {
default_ttl: Some(Duration::from_millis(50)),
refresh_on_read: false,
..Default::default()
});
store.put("ns", "key1", json!({ "v": 1 }), None).await?;
store.put("ns", "key2", json!({ "v": 2 }), None).await?;
// Wait for items to expire
tokio::time::sleep(Duration::from_millis(80)).await;
// Sweep removes both expired items
let count = store.sweep_expired_items().await?;
assert_eq!(count, 2);Sourcepub fn start_sweep_task(self: Arc<MemoryStore>) -> JoinHandle<()>
pub fn start_sweep_task(self: Arc<MemoryStore>) -> JoinHandle<()>
Start the background sweep task for periodic expired item cleanup.
This method spawns a tokio task that runs periodically according to
sweep_interval in the TTL config. Each sweep cycle removes at most
sweep_max_items expired items. Errors are logged via tracing::warn!
and do not terminate the task.
§Note
The returned JoinHandle can be used to abort the task via
handle.abort() or awaited to ensure the task completes. If dropped
without aborting, the task will continue running in the background.
§Examples
use juncture_core::store::{MemoryStore, TTLConfig};
use std::{time::Duration, sync::Arc};
let store = Arc::new(MemoryStore::new().with_ttl_config(TTLConfig {
default_ttl: Some(Duration::from_secs(300)),
refresh_on_read: true,
..Default::default()
}));
// Start background sweep task
let _sweep_handle = store.start_sweep_task();
// Store continues to work, sweep task runs in background
// The task will run until _sweep_handle is dropped or aborted