Skip to main content

RedisBackend

Struct RedisBackend 

Source
pub struct RedisBackend {
    client: Client,
    index_name: String,
    connection: Mutex<Option<Connection>>,
    timeout: Duration,
}
Expand description

Redis-based index backend using RediSearch.

§Connection Management (DB-H6)

Maintains a reusable connection via Mutex<Option<Connection>>. The connection is lazily initialized and reused across operations to avoid the overhead of establishing new connections for each command.

§Command Timeout (CHAOS-HIGH-005)

Connections are configured with a configurable response timeout (default 5s) to prevent indefinite blocking on unresponsive servers. Set SUBCOG_TIMEOUT_REDIS_MS to override.

Fields§

§client: Client

Redis client.

§index_name: String

Index name in Redis.

§connection: Mutex<Option<Connection>>

Cached connection for reuse (DB-H6).

§timeout: Duration

Operation timeout (CHAOS-HIGH-005).

Implementations§

Source§

impl RedisBackend

Source

pub fn new(connection_url: &str, index_name: impl Into<String>) -> Result<Self>

Creates a new Redis backend.

Configures the connection with a 5-second timeout (CHAOS-H2) to prevent indefinite blocking on slow or unresponsive Redis servers.

§Errors

Returns an error if the Redis connection fails.

Source

pub fn with_defaults() -> Result<Self>

Creates a backend with default settings.

§Errors

Returns an error if the Redis connection fails.

Source

pub fn health_check(&self) -> Result<bool>

Checks if the Redis connection is healthy (DB-HIGH-001).

Performs a PING command to verify connectivity and responsiveness.

§Returns

Ok(true) if the connection is healthy, Ok(false) if unhealthy, or Err if the health check itself fails unexpectedly.

§Example
ⓘ
let backend = RedisBackend::new("redis://localhost:6379", "subcog")?;
if backend.health_check()? {
    println!("Redis is healthy");
} else {
    eprintln!("Redis is not responding");
}
§Errors

Returns an error if the connection cannot be established.

Source

fn get_connection(&self) -> Result<Connection>

Gets a connection, reusing the cached one if available (DB-H6).

This method reuses an existing connection when possible, falling back to creating a new connection if the cache is empty or the connection is broken. The connection is stored in a Mutex for thread-safety.

§Timeout (CHAOS-H2)

New connections are configured with a 5-second response timeout.

§Connection Retry (CHAOS-HIGH-003)

New connection attempts use exponential backoff with jitter for transient failures (Redis starting up, network issues, etc.).

Source

fn return_connection(&self, conn: Connection)

Returns a connection to the cache for reuse (DB-H6).

Source

fn ensure_index(&self) -> Result<()>

Ensures the RediSearch index exists.

Source

fn index_exists(&self, conn: &mut Connection) -> bool

Checks if the index already exists.

Source

fn create_index(&self, conn: &mut Connection) -> Result<()>

Creates the RediSearch index.

Source

fn build_filter_clause(filter: &SearchFilter) -> String

Builds a filter clause for RediSearch queries.

Trait Implementations§

Source§

impl IndexBackend for RedisBackend

Source§

fn index(&self, memory: &Memory) -> Result<()>

Indexes a memory for full-text search. Read more
Source§

fn remove(&self, id: &MemoryId) -> Result<bool>

Removes a memory from the index. Read more
Source§

fn search( &self, query: &str, filter: &SearchFilter, limit: usize, ) -> Result<Vec<(MemoryId, f32)>>

Searches for memories matching a text query. Read more
Source§

fn list_all( &self, filter: &SearchFilter, limit: usize, ) -> Result<Vec<(MemoryId, f32)>>

Lists all indexed memories, optionally filtered. Read more
Source§

fn get_memory(&self, id: &MemoryId) -> Result<Option<Memory>>

Retrieves a memory by ID. Read more
Source§

fn clear(&self) -> Result<()>

Clears the entire index. Read more
Source§

fn reindex(&self, memories: &[Memory]) -> Result<()>

Re-indexes all memories. Read more
Source§

fn get_memories_batch(&self, ids: &[MemoryId]) -> Result<Vec<Option<Memory>>>

Retrieves multiple memories by their IDs in a single batch query. 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.

§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> Instrument for T

§

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

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,