Skip to main content

MemoryStore

Struct MemoryStore 

Source
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

Source

pub fn new() -> MemoryStore

Create new in-memory store

Create store with vector search enabled

§Arguments
  • config - Index configuration
Source

pub const fn with_ttl_config(self, config: TTLConfig) -> MemoryStore

Configure TTL behavior for item expiration.

§Arguments
  • config - TTL configuration
Source

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);
Source

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

Trait Implementations§

Source§

impl Debug for MemoryStore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for MemoryStore

Source§

fn default() -> MemoryStore

Returns the “default value” for a type. Read more
Source§

impl Store for MemoryStore

Source§

fn get<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, namespace: &'life1 str, key: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<Option<Item>, StoreError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, MemoryStore: 'async_trait,

Get item from store Read more
Source§

fn put<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, namespace: &'life1 str, key: &'life2 str, value: Value, index: Option<Vec<String>>, ) -> Pin<Box<dyn Future<Output = Result<(), StoreError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, MemoryStore: 'async_trait,

Put item into store Read more
Source§

fn delete<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, namespace: &'life1 str, key: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<(), StoreError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, MemoryStore: 'async_trait,

Delete item from store Read more
Source§

fn search<'life0, 'async_trait>( &'life0 self, query: SearchQuery, ) -> Pin<Box<dyn Future<Output = Result<SearchResult, StoreError>> + Send + 'async_trait>>
where 'life0: 'async_trait, MemoryStore: 'async_trait,

Search items with filtering and optional vector search Read more
Source§

fn list_namespaces<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, prefix: Option<&'life1 str>, suffix: Option<&'life2 str>, max_depth: Option<usize>, limit: Option<usize>, offset: Option<usize>, ) -> Pin<Box<dyn Future<Output = Result<Vec<String>, StoreError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, MemoryStore: 'async_trait,

List namespaces Read more
Source§

fn batch<'life0, 'async_trait>( &'life0 self, ops: Vec<StoreOp>, ) -> Pin<Box<dyn Future<Output = Result<Vec<StoreResult>, StoreError>> + Send + 'async_trait>>
where 'life0: 'async_trait, MemoryStore: 'async_trait,

Execute batch operations Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more