arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
Classes | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
RedisBuffer< T > Class Template Reference

Buffer implementation that stores and retrieves data from a Redis server using the Arduino Client. More...

#include <RedisBuffer.h>

Inheritance diagram for RedisBuffer< T >:
BaseBuffer< T >

Classes

struct  RedisResult
 

Public Member Functions

 RedisBuffer (Client &client, const char *key, size_t max_size, size_t local_buf_size=512, int expire_seconds=60 *60)
 Constructs a RedisBuffer.
 
T * address () override
 Returns the address of the start of the physical read buffer (not supported).
 
int available () override
 Returns the number of elements available to read (local + Redis). Flushes any pending writes before checking.
 
int availableForWrite () override
 Returns the number of elements that can be written before reaching max_size. There are are no checks in place that would prevent that the size values is exeeded: This is for information only!
 
void clear ()
 same as reset
 
virtual int clearArray (int len)
 Removes the next len entries.
 
bool isEmpty ()
 
virtual bool isFull ()
 checks if the buffer is full
 
virtual float levelPercent ()
 Returns the level of the buffer in %.
 
bool peek (T &result) override
 Peeks at the next value in Redis directly (no local buffer). Flushes any pending writes before peeking.
 
bool read (T &result) override
 Reads a single value from Redis directly (no local buffer). Flushes any pending writes before reading.
 
int readArray (T data[], int len) override
 Reads multiple values from the buffer in one batch. Flushes any pending writes before reading.
 
void reset () override
 Clears the buffer both locally and on the Redis server. Flushes any pending writes before clearing.
 
bool resize (int size) override
 Resizes the maximum buffer size. This operation is only allowed before any write has occurred.
 
void setExpire (int seconds)
 Sets the expiration time (in seconds) for the Redis key. The expiration will be refreshed on every write/flush.
 
size_t size () override
 Returns the maximum capacity of the buffer.
 
bool write (T data) override
 Buffers a single value for writing to Redis. Data is only sent to Redis when the local buffer is full or writeArray is called.
 
int writeArray (const T data[], int len) override
 Writes multiple values to Redis in one batch. Flushes any pending writes before sending the new data.
 
virtual int writeArrayOverwrite (const T data[], int len)
 Fills the buffer data and overwrites the oldest data if the buffer is full.
 

Protected Member Functions

void clearResponse ()
 
void fillReadBuffer ()
 Fills the local read buffer from Redis using LRANGE. After reading, removes the items from Redis using LTRIM.
 
void flushWrite ()
 Flushes buffered writes to Redis using RPUSH and sets expiration if configured.
 
RedisResult readResponse ()
 Reads a single line response from the Redis server and parses it into a RedisResult.
 
String redisCommand (const String &cmd, const String &arg1="", const String &arg2="", const String &arg3="")
 Constructs a Redis command in RESP format.
 
RedisResult sendCommand (const String &cmd)
 Sends a command to the Redis server and returns the parsed result.
 

Protected Attributes

Clientclient
 Reference to the Arduino Client for Redis communication.
 
int expire_seconds = 0
 Expiration time in seconds (0 = no expiration).
 
bool has_written = false
 True if any write operation has occurred.
 
const char * key
 Redis key for the buffer.
 
size_t local_buf_size
 Local buffer size for batching.
 
size_t max_size
 Maximum number of elements in the buffer.
 
SingleBuffer< T > read_buf
 Local buffer for pending reads.
 
SingleBuffer< T > write_buf
 Local buffer for pending writes.
 

Detailed Description

template<typename T>
class audio_tools::RedisBuffer< T >

Buffer implementation that stores and retrieves data from a Redis server using the Arduino Client.

This buffer uses a Redis list as a circular buffer and batches read/write operations for efficiency. Individual write/read calls are buffered locally using SingleBuffer and only sent to Redis in bulk when writeArray/readArray is called or when the buffer is full/empty. This reduces network overhead and improves performance for streaming scenarios.

Template Parameters
TData type to buffer (e.g., uint8_t, int16_t)

Constructor & Destructor Documentation

◆ RedisBuffer()

template<typename T >
RedisBuffer ( Client client,
const char *  key,
size_t  max_size,
size_t  local_buf_size = 512,
int  expire_seconds = 60 * 60 
)
inline

Constructs a RedisBuffer.

Parameters
clientReference to a connected Arduino Client (e.g., WiFiClient, EthernetClient).
keyRedis key to use for the buffer (list).
max_sizeMaximum number of elements in the buffer.
local_buf_sizeSize of the local buffer for batching (default: 512).
expire_secondsNumber of seconds after which the Redis key should expire (0 = no expiration).

Member Function Documentation

◆ address()

template<typename T >
T * address ( )
inlineoverridevirtual

Returns the address of the start of the physical read buffer (not supported).

Returns
nullptr.

Implements BaseBuffer< T >.

◆ available()

template<typename T >
int available ( )
inlineoverridevirtual

Returns the number of elements available to read (local + Redis). Flushes any pending writes before checking.

Returns
Number of available elements.

Implements BaseBuffer< T >.

◆ availableForWrite()

template<typename T >
int availableForWrite ( )
inlineoverridevirtual

Returns the number of elements that can be written before reaching max_size. There are are no checks in place that would prevent that the size values is exeeded: This is for information only!

Returns
Number of available slots for writing.

Implements BaseBuffer< T >.

◆ clearArray()

template<typename T >
virtual int clearArray ( int  len)
inlinevirtualinherited

◆ isFull()

template<typename T >
virtual bool isFull ( )
inlinevirtualinherited

◆ peek()

template<typename T >
bool peek ( T &  result)
inlineoverridevirtual

Peeks at the next value in Redis directly (no local buffer). Flushes any pending writes before peeking.

Parameters
resultReference to store the peeked value.
Returns
true if a value was available, false otherwise.

Implements BaseBuffer< T >.

◆ read()

template<typename T >
bool read ( T &  result)
inlineoverridevirtual

Reads a single value from Redis directly (no local buffer). Flushes any pending writes before reading.

Parameters
resultReference to store the read value.
Returns
true if a value was read, false otherwise.

Implements BaseBuffer< T >.

◆ readArray()

template<typename T >
int readArray ( data[],
int  len 
)
inlineoverridevirtual

Reads multiple values from the buffer in one batch. Flushes any pending writes before reading.

Parameters
dataArray to store the read values.
lenMaximum number of values to read.
Returns
Number of values actually read.

Reimplemented from BaseBuffer< T >.

◆ readResponse()

template<typename T >
RedisResult readResponse ( )
inlineprotected

Reads a single line response from the Redis server and parses it into a RedisResult.

Returns
RedisResult structure with parsed response.

this is a length prefix line

Remove any leading or trailing whitespace

◆ redisCommand()

template<typename T >
String redisCommand ( const String &  cmd,
const String &  arg1 = "",
const String &  arg2 = "",
const String &  arg3 = "" 
)
inlineprotected

Constructs a Redis command in RESP format.

Parameters
cmdRedis command (e.g., "RPUSH").
arg1First argument.
arg2Second argument.
arg3Third argument.
Returns
Command string in RESP format.

◆ reset()

template<typename T >
void reset ( )
inlineoverridevirtual

Clears the buffer both locally and on the Redis server. Flushes any pending writes before clearing.

Implements BaseBuffer< T >.

◆ resize()

template<typename T >
bool resize ( int  size)
inlineoverridevirtual

Resizes the maximum buffer size. This operation is only allowed before any write has occurred.

Parameters
sizeNew maximum size.
Returns
true if resized, false if any write has already occurred.

Reimplemented from BaseBuffer< T >.

◆ sendCommand()

template<typename T >
RedisResult sendCommand ( const String &  cmd)
inlineprotected

Sends a command to the Redis server and returns the parsed result.

Parameters
cmdCommand string in RESP format.
Returns
RedisResult structure with parsed response.

◆ setExpire()

template<typename T >
void setExpire ( int  seconds)
inline

Sets the expiration time (in seconds) for the Redis key. The expiration will be refreshed on every write/flush.

Parameters
secondsExpiration time in seconds (0 = no expiration).

◆ size()

template<typename T >
size_t size ( )
inlineoverridevirtual

Returns the maximum capacity of the buffer.

Returns
Maximum number of elements.

Implements BaseBuffer< T >.

◆ write()

template<typename T >
bool write ( data)
inlineoverridevirtual

Buffers a single value for writing to Redis. Data is only sent to Redis when the local buffer is full or writeArray is called.

Parameters
dataValue to write.
Returns
true if buffered successfully.

Implements BaseBuffer< T >.

◆ writeArray()

template<typename T >
int writeArray ( const T  data[],
int  len 
)
inlineoverridevirtual

Writes multiple values to Redis in one batch. Flushes any pending writes before sending the new data.

Parameters
dataArray of values to write.
lenNumber of values to write.
Returns
Number of values written.

Reimplemented from BaseBuffer< T >.


The documentation for this class was generated from the following file: