SuperCache.Internal.Stream (SuperCache v1.7.0)

Copy Markdown View Source

Internal stream processor for SuperCache buffer queues.

This module bridges the internal message-passing queue (SuperCache.Internal.Queue) with the caching layer (SuperCache). It creates a Stream that continuously pulls items from a named queue and flushes each drained batch into the cache via SuperCache.put_batch!/1.

Design

The stream is built using Stream.resource/3:

  1. Accumulator - The queue name/atom passed to create/1.
  2. Next function - Calls Queue.get/1 to fetch a batch of items.
  3. Cleanup function - No-op, as the queue lifecycle is managed externally.

Queue.get/1 returns items in FIFO order, so sequential updates to the same key within one batch preserve the last write. The batch is flushed with a single put_batch!/1 call. If a batch contains a malformed tuple, the flush retries item by item so valid tuples still land and only the bad item is logged.

Warning

This is an internal module. Do not use it directly in application code. Use SuperCache.Buffer or SuperCache.lazy_put/1 instead.

Summary

Types

Queue name or PID used as the stream accumulator.

Functions

Creates a Stream that continuously pulls items from the given queue.

Consumes an enumerable and pushes each batch into the cache.

Pushes a single item into the SuperCache.

Types

queue_ref()

@type queue_ref() :: atom() | pid()

Queue name or PID used as the stream accumulator.

Functions

create(queue_ref)

@spec create(queue_ref()) :: Enumerable.t()

Creates a Stream that continuously pulls items from the given queue.

The stream yields batches of items returned by Queue.get/1. When the queue process is no longer running, the stream halts cleanly (the buffer runner decides whether to restart it).

Examples

stream = SuperCache.Internal.Stream.create(:my_buffer)
Stream.take(stream, 10) |> Enum.to_list()

make_stream_pipe(enumable)

@spec make_stream_pipe(Enumerable.t()) :: :ok

Consumes an enumerable and pushes each batch into the cache.

Runs the stream to completion. Drained batches are written with SuperCache.put_batch!/1; if a batch contains a malformed tuple, the batch is retried item by item so valid tuples still land and only the bad item is logged.

push(data)

@spec push(tuple()) :: :ok

Pushes a single item into the SuperCache.

Wraps SuperCache.put/1 with error handling. If the put fails, the error is logged but not raised, ensuring the stream continues processing.

Examples

SuperCache.Internal.Stream.push({:user, 1, "Alice"})
# => :ok