RedisStreams

Struct RedisStreams 

Source
pub struct RedisStreams { /* private fields */ }
Expand description

Redis Streams client for event-driven architectures

Wraps Redis ConnectionManager to provide high-level streaming operations.

Implementations§

Source§

impl RedisStreams

Source

pub async fn new(redis_url: &str) -> Result<Self>

Create a new RedisStreams instance

§Arguments
  • redis_url - Redis connection URL (e.g., <redis://127.0.0.1:6379>)
§Example
let streams = RedisStreams::new("redis://127.0.0.1:6379").await?;
§Errors

Returns an error if Redis connection fails.

Source

pub async fn stream_add( &self, stream_key: &str, fields: Vec<(String, String)>, maxlen: Option<usize>, ) -> Result<String>

Publish data to Redis Stream using XADD

§Arguments
  • stream_key - Name of the Redis Stream (e.g., “events_stream”)
  • fields - Vec of field-value pairs to add to the stream
  • maxlen - Optional maximum length for stream trimming (uses MAXLEN ~ for approximate trimming)
§Returns

The stream entry ID generated by Redis (e.g., “1234567890123-0”)

§Example
let event_id = streams.stream_add(
    "user_events",
    vec![
        ("user_id".to_string(), "42".to_string()),
        ("action".to_string(), "purchase".to_string()),
        ("amount".to_string(), "99.99".to_string()),
    ],
    Some(10000)  // Keep max 10k events
).await?;
println!("Event ID: {}", event_id);
§Errors

Returns an error if the XADD command fails.

Source

pub async fn stream_read_latest( &self, stream_key: &str, count: usize, ) -> Result<Vec<StreamEntry>>

Read latest entries from Redis Stream using XREVRANGE

§Arguments
  • stream_key - Name of the Redis Stream
  • count - Number of latest entries to retrieve
§Returns

Vector of (entry_id, fields) tuples, ordered from newest to oldest

§Example
let latest_10 = streams.stream_read_latest("events", 10).await?;
for (id, fields) in latest_10 {
    println!("Event {}: {:?}", id, fields);
}
§Errors

Returns an error if the XREVRANGE command fails.

Source

pub async fn stream_read( &self, stream_key: &str, last_id: &str, count: usize, block_ms: Option<usize>, ) -> Result<Vec<StreamEntry>>

Read entries from Redis Stream using XREAD (blocking or non-blocking)

§Arguments
  • stream_key - Name of the Redis Stream
  • last_id - Last entry ID seen (use “0” to read from beginning, “$” for only new entries)
  • count - Maximum number of entries to retrieve
  • block_ms - Optional blocking timeout in milliseconds (None for non-blocking)
§Returns

Vector of (entry_id, fields) tuples

§Example
// Non-blocking: read new entries since last seen ID
let entries = streams.stream_read("events", "1234567890-0", 100, None).await?;

// Blocking: wait up to 5 seconds for new entries
let new_entries = streams.stream_read("events", "$", 100, Some(5000)).await?;
§Errors

Returns an error if the XREAD command fails.

Trait Implementations§

Source§

impl Clone for RedisStreams

Source§

fn clone(&self) -> RedisStreams

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl StreamingBackend for RedisStreams

Implement StreamingBackend trait for RedisStreams

This enables RedisStreams to be used as a pluggable streaming backend.

Source§

fn stream_add<'life0, 'life1, 'async_trait>( &'life0 self, stream_key: &'life1 str, fields: Vec<(String, String)>, maxlen: Option<usize>, ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Add an entry to a stream Read more
Source§

fn stream_read_latest<'life0, 'life1, 'async_trait>( &'life0 self, stream_key: &'life1 str, count: usize, ) -> Pin<Box<dyn Future<Output = Result<Vec<(String, Vec<(String, String)>)>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Read the latest N entries from a stream (newest first) Read more
Source§

fn stream_read<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, stream_key: &'life1 str, last_id: &'life2 str, count: usize, block_ms: Option<usize>, ) -> Pin<Box<dyn Future<Output = Result<Vec<(String, Vec<(String, String)>)>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Read entries from a stream with optional blocking Read more

Auto Trait Implementations§

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

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
§

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

§

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

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.
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