43 lines
1.3 KiB
C++
43 lines
1.3 KiB
C++
|
|
#pragma once
|
|||
|
|
#include <memory>
|
|||
|
|
#include <string>
|
|||
|
|
#include <vector>
|
|||
|
|
#include <utility>
|
|||
|
|
#include <nlohmann/json.hpp>
|
|||
|
|
|
|||
|
|
#include <sw/redis++/redis++.h>
|
|||
|
|
|
|||
|
|
namespace sw { namespace redis { class Redis; } }
|
|||
|
|
|
|||
|
|
class RedisPublisher {
|
|||
|
|
public:
|
|||
|
|
// Ex.: "tcp://127.0.0.1:6379?keep_alive=true&socket_timeout=200ms"
|
|||
|
|
explicit RedisPublisher(const std::string& uri);
|
|||
|
|
|
|||
|
|
// SET key com JSON; ttl_sec <= 0 => sem expire
|
|||
|
|
bool set_json(const std::string& key, const std::string& json, int ttl_sec = 0);
|
|||
|
|
|
|||
|
|
// XADD hist<73>rico (retorna id gerado). Se maxlen>0, usa aprox MAXLEN~.
|
|||
|
|
std::string xadd(const std::string& stream,
|
|||
|
|
const std::vector<std::pair<std::string, std::string>>& fields,
|
|||
|
|
size_t maxlen = 0);
|
|||
|
|
|
|||
|
|
bool hset_fields(const std::string& key,
|
|||
|
|
const std::vector<std::pair<std::string, std::string>>& fields,
|
|||
|
|
int ttl_sec = 0);
|
|||
|
|
|
|||
|
|
// 1) Merge com dicion<6F>rio <20>flat<61> em estilo a.b.c = "valor"
|
|||
|
|
bool set_json_merge_paths(const std::string& key,
|
|||
|
|
const std::vector<std::pair<std::string, std::string>>& flat_fields,
|
|||
|
|
int ttl_sec = 0,
|
|||
|
|
const std::string& sep = ".");
|
|||
|
|
|
|||
|
|
// 2) Merge com JSON aninhado (deep-merge)
|
|||
|
|
bool set_json_merge_object(const std::string& key,
|
|||
|
|
const nlohmann::json& patch,
|
|||
|
|
int ttl_sec = 0);
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
std::unique_ptr<sw::redis::Redis> r_;
|
|||
|
|
};
|