StreamingBackend

Trait StreamingBackend 

Source
pub trait StreamingBackend: Send + Sync {
    // Required methods
    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;
    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;
    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;
}
Expand description

Optional trait for cache backends that support event streaming

This trait defines operations for event-driven architectures using streaming data structures like Redis Streams.

§Capabilities

  • Publish events to streams with automatic trimming
  • Read latest entries (newest first)
  • Read entries with blocking support

§Backend Requirements

Not all cache backends support streaming. This trait is optional and should only be implemented by backends with native streaming support (e.g., Redis Streams, Kafka, Pulsar).

§Example

use multi_tier_cache::{StreamingBackend, async_trait};

#[async_trait]
impl StreamingBackend for MyStreamingCache {
    async fn stream_add(
        &self,
        stream_key: &str,
        fields: Vec<(String, String)>,
        maxlen: Option<usize>,
    ) -> Result<String> {
        // Add entry to stream, return entry ID
    }

    // ... implement other methods
}

Required Methods§

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

§Arguments
  • stream_key - Name of the stream (e.g., “events_stream”)
  • fields - Vector of field-value pairs to add
  • maxlen - Optional maximum stream length (older entries are trimmed)
§Returns
  • Ok(entry_id) - The generated entry ID (e.g., “1234567890-0”)
  • Err(e) - Stream operation failed
§Trimming Behavior

If maxlen is specified, the stream is automatically trimmed to keep approximately that many entries (oldest entries are removed).

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)

§Arguments
  • stream_key - Name of the stream
  • count - Maximum number of entries to retrieve
§Returns
  • Ok(entries) - Vector of (entry_id, fields) tuples (newest first)
  • Err(e) - Stream operation failed
§Ordering

Entries are returned in reverse chronological order (newest first).

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

§Arguments
  • stream_key - Name of the stream
  • last_id - Last entry ID seen (“0” for beginning, “$” for new only)
  • count - Maximum number of entries to retrieve
  • block_ms - Optional blocking timeout in milliseconds (None = non-blocking)
§Returns
  • Ok(entries) - Vector of (entry_id, fields) tuples
  • Err(e) - Stream operation failed
§Blocking Behavior
  • None: Non-blocking, returns immediately
  • Some(ms): Blocks up to ms milliseconds waiting for new entries
§Use Cases
  • Non-blocking: Poll for new events
  • Blocking: Long-polling for real-time event consumption

Implementors§

Source§

impl StreamingBackend for RedisStreams

Implement StreamingBackend trait for RedisStreams

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