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: ClientRedis client.
index_name: StringIndex name in Redis.
connection: Mutex<Option<Connection>>Cached connection for reuse (DB-H6).
timeout: DurationOperation timeout (CHAOS-HIGH-005).
Implementations§
Source§impl RedisBackend
impl RedisBackend
Sourcepub fn new(connection_url: &str, index_name: impl Into<String>) -> Result<Self>
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.
Sourcepub fn with_defaults() -> Result<Self>
pub fn with_defaults() -> Result<Self>
Sourcepub fn health_check(&self) -> Result<bool>
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.
Sourcefn get_connection(&self) -> Result<Connection>
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.).
Sourcefn return_connection(&self, conn: Connection)
fn return_connection(&self, conn: Connection)
Returns a connection to the cache for reuse (DB-H6).
Sourcefn ensure_index(&self) -> Result<()>
fn ensure_index(&self) -> Result<()>
Ensures the RediSearch index exists.
Sourcefn index_exists(&self, conn: &mut Connection) -> bool
fn index_exists(&self, conn: &mut Connection) -> bool
Checks if the index already exists.
Sourcefn create_index(&self, conn: &mut Connection) -> Result<()>
fn create_index(&self, conn: &mut Connection) -> Result<()>
Creates the RediSearch index.
Sourcefn build_filter_clause(filter: &SearchFilter) -> String
fn build_filter_clause(filter: &SearchFilter) -> String
Builds a filter clause for RediSearch queries.
Trait Implementations§
Source§impl IndexBackend for RedisBackend
impl IndexBackend for RedisBackend
Source§fn index(&self, memory: &Memory) -> Result<()>
fn index(&self, memory: &Memory) -> Result<()>
Source§fn search(
&self,
query: &str,
filter: &SearchFilter,
limit: usize,
) -> Result<Vec<(MemoryId, f32)>>
fn search( &self, query: &str, filter: &SearchFilter, limit: usize, ) -> Result<Vec<(MemoryId, f32)>>
Source§fn list_all(
&self,
filter: &SearchFilter,
limit: usize,
) -> Result<Vec<(MemoryId, f32)>>
fn list_all( &self, filter: &SearchFilter, limit: usize, ) -> Result<Vec<(MemoryId, f32)>>
Auto Trait Implementations§
impl !Freeze for RedisBackend
impl RefUnwindSafe for RedisBackend
impl Send for RedisBackend
impl Sync for RedisBackend
impl Unpin for RedisBackend
impl UnwindSafe for RedisBackend
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].