SmartSpectra C++ SDK
Measure human vitals from video with SmartSpectra C++ SDK.
Loading...
Searching...
No Matches
redis_client.hpp
1// redis_client.hpp
2// Created by Greg on 1/30/25.
3// Copyright (C) 2025 Presage Security, Inc.
4//
5// SPDX-License-Identifier: LGPL-3.0-or-later
6
7#pragma once
8
9#include <string>
10#include <vector>
11#include <optional>
12#include <chrono>
13
14namespace presage::smartspectra::redis_ipc {
15
22class RedisClient {
23public:
24 RedisClient() = default;
25 ~RedisClient();
26
27 // No copy
28 RedisClient(const RedisClient&) = delete;
29 RedisClient& operator=(const RedisClient&) = delete;
30
38 bool Connect(const std::string& host, int port, std::chrono::milliseconds timeout);
39
43 void Close();
44
49 bool Ping();
50
57 bool Publish(const std::string& channel, const std::string& payload);
58
65 bool Set(const std::string& key, const std::string& value);
66
72 bool Del(const std::string& key);
73
80 std::optional<std::string> LPop(const std::string& key, bool* ok);
81
86 const std::string& last_error() const { return last_error_; }
87
93 void SetResponseLimits(size_t max_bulk_string_bytes, size_t max_array_elements);
94
95private:
96 enum class ReplyType {
97 kSimpleString,
98 kInteger,
99 kBulkString,
100 kNull,
101 kArray,
102 kError
103 };
104
105 struct Reply {
106 ReplyType type = ReplyType::kError;
107 std::string string_value;
108 int64_t integer_value = 0;
109 std::vector<Reply> array_items;
110 };
111
112 bool Execute(const std::vector<std::string>& command, Reply* reply);
113 static std::string Encode(const std::vector<std::string>& command);
114 bool EnsureConnected() const;
115 bool SendAll(const std::string& data);
116 bool ReceiveSome();
117 bool EnsureAvailable(size_t bytes);
118 bool ReadChar(char* ch);
119 bool ReadLine(std::string* line);
120 bool ReadBulk(std::string* value, size_t length);
121 bool ParseReply(Reply* reply);
122 void CompactBuffer();
123
124 int fd_ = -1;
125 std::chrono::milliseconds timeout_{1000};
126 std::string buffer_;
127 size_t offset_ = 0;
128 mutable std::string last_error_;
129
130 size_t max_bulk_string_bytes_ = 134217728; // 128MB default
131 size_t max_array_elements_ = 1000000; // 1M default
132};
133
134} // namespace presage::smartspectra::redis_ipc
bool Connect(const std::string &host, int port, std::chrono::milliseconds timeout)
Definition redis_client.cpp:97
std::optional< std::string > LPop(const std::string &key, bool *ok)
Definition redis_client.cpp:204
bool Set(const std::string &key, const std::string &value)
Definition redis_client.cpp:188
void Close()
Definition redis_client.cpp:163
const std::string & last_error() const
Definition redis_client.hpp:86
void SetResponseLimits(size_t max_bulk_string_bytes, size_t max_array_elements)
Definition redis_client.cpp:92
bool Del(const std::string &key)
Definition redis_client.cpp:196
bool Publish(const std::string &channel, const std::string &payload)
Definition redis_client.cpp:180
bool Ping()
Definition redis_client.cpp:172