Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
orbit::database::RedisClient Class Reference

A synchronous Redis client providing core operations. More...

#include <RedisClient.hpp>

Public Member Functions

 RedisClient (const std::string &host, int port)
 Constructs a new RedisClient.
 
 ~RedisClient ()
 
bool connect ()
 Connects to the Redis server.
 
void disconnect ()
 Disconnects from the Redis server.
 
std::string ping ()
 Sends a PING command to the Redis server.
 
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::optional< std::string > get (const std::string &key)
 Gets the value of a key.
 
long long incr (const std::string &key)
 Increments the integer value of a key by one.
 
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). A separate INCR then EXPIRE can leave a counter without an expiry if the second command fails.
 
bool expire (const std::string &key, int seconds)
 Sets a timeout on a key.
 
bool del (const std::string &key)
 Deletes a key.
 

Private Member Functions

bool connect_locked ()
 
void close_locked ()
 
std::string send_command (const std::vector< std::string > &args, bool *no_value=nullptr)
 
std::string read_response (bool &ok, bool &no_value)
 

Private Attributes

std::string host_
 
int port_
 
int fd_ {-1}
 
std::mutex mutex_
 

Detailed Description

A synchronous Redis client providing core operations.

Constructor & Destructor Documentation

◆ RedisClient()

orbit::database::RedisClient::RedisClient ( const std::string &  host,
int  port 
)

Constructs a new RedisClient.

Parameters
hostThe Redis server hostname or IP address.
portThe Redis server port number.

◆ ~RedisClient()

orbit::database::RedisClient::~RedisClient ( )

Member Function Documentation

◆ close_locked()

void orbit::database::RedisClient::close_locked ( )
private

◆ connect()

bool orbit::database::RedisClient::connect ( )

Connects to the Redis server.

Returns
true if the connection was successful, false otherwise.

◆ connect_locked()

bool orbit::database::RedisClient::connect_locked ( )
private

◆ del()

bool orbit::database::RedisClient::del ( const std::string &  key)

Deletes a key.

Returns
true if the key existed.

◆ disconnect()

void orbit::database::RedisClient::disconnect ( )

Disconnects from the Redis server.

◆ expire()

bool orbit::database::RedisClient::expire ( const std::string &  key,
int  seconds 
)

Sets a timeout on a key.

Parameters
keyThe key to set the timeout on.
secondsThe timeout in seconds.
Returns
true if the timeout was set, false otherwise.

◆ get()

std::optional< std::string > orbit::database::RedisClient::get ( const std::string &  key)

Gets the value of a key.

Parameters
keyThe key to query.
Returns
An optional string containing the value, or std::nullopt if the key does not exist.

◆ incr()

long long orbit::database::RedisClient::incr ( const std::string &  key)

Increments the integer value of a key by one.

Parameters
keyThe key to increment.
Returns
The value of the key after the increment.

◆ incr_with_expiry()

long long orbit::database::RedisClient::incr_with_expiry ( const std::string &  key,
int  seconds 
)

Increments a counter and, when this creates it, sets its expiry, atomically (one Lua script). A separate INCR then EXPIRE can leave a counter without an expiry if the second command fails.

Returns
The new value, or 0 on error.

◆ ping()

std::string orbit::database::RedisClient::ping ( )

Sends a PING command to the Redis server.

Returns
The response string from the server (e.g., "PONG").

◆ read_response()

std::string orbit::database::RedisClient::read_response ( bool &  ok,
bool &  no_value 
)
private

◆ send_command()

std::string orbit::database::RedisClient::send_command ( const std::vector< std::string > &  args,
bool *  no_value = nullptr 
)
private

◆ set()

bool orbit::database::RedisClient::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.

Parameters
keyThe key to set.
valueThe value to associate with the key.
expire_secondsOptional expiration time in seconds (0 means no expiration).
Returns
true if the command succeeded, false otherwise.

Member Data Documentation

◆ fd_

int orbit::database::RedisClient::fd_ {-1}
private

◆ host_

std::string orbit::database::RedisClient::host_
private

◆ mutex_

std::mutex orbit::database::RedisClient::mutex_
private

◆ port_

int orbit::database::RedisClient::port_
private

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