2#include "AudioTools/CoreAudio/Buffers.h"
4#define REDIS_RESULT_BUFFER_SIZE (10 * 1024)
84 LOGI(
"RedisBuffer:writeArray: %d", len);
87 for (
int i = 0; i < len; ++i) {
100 bool read(T& result)
override {
106 LOGI(
"RedisBuffer:read: no data available");
112 LOGI(
"Redis LPOP: %d", val);
127 while (read_count < len) {
143 bool peek(T& result)
override {
149 if (!resp.
ok)
return false;
162 LOGI(
"Redis DEL: %d", rc.intValue);
176 LOGI(
"LLEN: %d (ok=%d)", resp.
intValue, resp.
ok);
209 LOGI(
"RedisBuffer::resize: %d",
size);
220 operator bool()
const {
return ok; }
233 void clearResponse() {
234 while (
client.available()) {
248 const String& arg2 =
"",
const String& arg3 =
"") {
250 String(1 + (arg1.length() > 0) + (arg2.length() > 0) +
251 (arg3.length() > 0)) +
253 out +=
"$" + String(cmd.length()) +
"\r\n" + cmd +
"\r\n";
255 out +=
"$" + String(arg1.length()) +
"\r\n" + arg1 +
"\r\n";
257 out +=
"$" + String(arg2.length()) +
"\r\n" + arg2 +
"\r\n";
259 out +=
"$" + String(arg3.length()) +
"\r\n" + arg3 +
"\r\n";
270 if (!
client.connected()) {
271 LOGE(
"Redis not connected");
289 uint8_t buffer[REDIS_RESULT_BUFFER_SIZE] = {};
292 n =
client.read(buffer,
sizeof(buffer));
298 String tail = (
char*)buffer;
299 int nl_pos = tail.indexOf(
"\r\n");
300 while (nl_pos >= 0) {
301 String head = tail.substring(0, nl_pos);
302 if (!head.startsWith(
"$"))
304 tail = tail.substring(nl_pos + 2);
306 nl_pos = tail.indexOf(
"\r\n");
309 if (!tail.startsWith(
"$") && tail.length() > 0) {
318 StrView line((
char*)buffer,
sizeof(buffer), n);
355 String cmd =
"*" + String(2 + write_size) +
"\r\n";
356 cmd +=
"$5\r\nRPUSH\r\n";
357 cmd +=
"$" + String(strlen(
key)) +
"\r\n" +
key +
"\r\n";
361 String sval = String(value);
362 cmd +=
"$" + String(sval.length()) +
"\r\n" + sval +
"\r\n";
366 LOGI(
"Redis RPUSH %d entries: %d (ok=%d)", write_size, resp.
intValue,
372 LOGI(
"Redis EXPIRE: %d (ok=%d)", resp.
intValue, resp.
ok);
383 LOGI(
"Redis LPOP: %d",
size)
388 LOGE(
"Redis LPOP failed: %s", cmd.c_str());
391 for (
auto& str : rc.strValues) {
392 if (str.startsWith(
"*"))
continue;
393 if (str.startsWith(
"$"))
continue;
394 if (str.length() == 0)
continue;
395 LOGI(
"- %s", str.c_str());
396 T value = (T)str.toInt();
403 LOGW(
"RedisBuffer:fillReadBuffer: not enough data read from Redis");