Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
RedisClient.hpp
Go to the documentation of this file.
1#pragma once
3#include <string>
4#include <vector>
5#include <optional>
6#include <mutex>
8
9namespace orbit::database {
10
15public:
22 RedisClient(const std::string& host, int port);
24
30 bool connect();
31
35 void disconnect();
36
42 std::string ping();
43
52 bool set(const std::string& key, const std::string& value, int expire_seconds = 0);
53
60 std::optional<std::string> get(const std::string& key);
61
68 long long incr(const std::string& key);
69
76 long long incr_with_expiry(const std::string& key, int seconds);
77
85 bool expire(const std::string& key, int seconds);
86
91 bool del(const std::string& key);
92
93private:
94 // Callers must hold mutex_.
97
98 // The reply as text; "" on failure or an error reply. *no_value, when
99 // given, is set unless the reply carried a value: a nil reply, an error
100 // reply or a failure. "" alone cannot tell those from an empty string.
101 std::string send_command(const std::vector<std::string>& args, bool* no_value = nullptr);
102 std::string read_response(bool& ok, bool& no_value);
103
104 std::string host_;
105 int port_;
106 int fd_{-1};
107 std::mutex mutex_; // Thread-safe for shared use across worker threads
108};
109
110} // namespace database
A synchronous Redis client providing core operations.
Definition RedisClient.hpp:14
void disconnect()
Disconnects from the Redis server.
bool connect()
Connects to the Redis server.
long long incr_with_expiry(const std::string &key, int seconds)
Increments a counter and, when this creates it, sets its expiry, atomically (one Lua script)....
std::string read_response(bool &ok, bool &no_value)
bool expire(const std::string &key, int seconds)
Sets a timeout on a key.
int fd_
Definition RedisClient.hpp:106
std::string send_command(const std::vector< std::string > &args, bool *no_value=nullptr)
std::optional< std::string > get(const std::string &key)
Gets the value of a key.
bool set(const std::string &key, const std::string &value, int expire_seconds=0)
Sets a key to hold a string value with an optional expiration time.
std::string ping()
Sends a PING command to the Redis server.
std::mutex mutex_
Definition RedisClient.hpp:107
std::string host_
Definition RedisClient.hpp:104
int port_
Definition RedisClient.hpp:105
long long incr(const std::string &key)
Increments the integer value of a key by one.
bool del(const std::string &key)
Deletes a key.
RedisClient(const std::string &host, int port)
Constructs a new RedisClient.
Definition ConnectionPool.hpp:14