-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[redis][refactoring] Adding Redis in server and some code refactoring
- Loading branch information
Nikolay Ryzhov
committed
May 8, 2024
1 parent
4a2a9b0
commit d84d53c
Showing
7 changed files
with
106 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#include "wrapper.h" | ||
#include "postgres-wrapper.h" | ||
|
||
#include <stdexcept> | ||
|
||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include "redis-wrapper.h" | ||
|
||
#include <stdexcept> | ||
|
||
#include "utils/logger.h" | ||
|
||
namespace fm { | ||
|
||
Redis::Redis(const std::string& host, int32_t port) { | ||
context = redisConnect(host.c_str(), port); | ||
if (context != nullptr && context->err) { | ||
throw std::runtime_error(context->errstr); | ||
} else { | ||
Logger::Get().Write("Success Redis connection."); | ||
} | ||
} | ||
|
||
Redis::~Redis() { redisFree(context); } | ||
|
||
redisResponse set(Redis& redis, const std::string& key, const std::string& value) { | ||
redisReply* reply; | ||
reply = static_cast<redisReply*>(redisCommand(redis.context, "SETEX %s 10 %s", key.c_str(), value.c_str())); | ||
|
||
std::string replyStr(reply->str, reply->len); | ||
int8_t status = reply->type; | ||
|
||
freeReplyObject(reply); | ||
return {status, replyStr}; | ||
} | ||
|
||
redisResponse get(Redis& redis, const std::string& key) { | ||
redisReply* reply; | ||
reply = static_cast<redisReply*>(redisCommand(redis.context, "GET %s", key.c_str())); | ||
|
||
std::string replyStr(reply->str, reply->len); | ||
int8_t status = reply->type; | ||
|
||
freeReplyObject(reply); | ||
return {status, replyStr}; | ||
} | ||
|
||
} // namespace fm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <string> | ||
|
||
#include "json.hpp" | ||
|
||
using json = nlohmann::json; | ||
|
||
#include <hiredis/hiredis.h> | ||
|
||
namespace fm { | ||
|
||
using redisResponse = std::pair<int8_t, std::string>; // status code, reply string | ||
|
||
class Redis { | ||
public: | ||
Redis(const std::string& host, int32_t port); | ||
~Redis(); | ||
Redis(const Redis& other) = delete; | ||
Redis(Redis&& other) = delete; | ||
|
||
private: | ||
redisContext* context; | ||
|
||
friend redisResponse set(Redis& redis, const std::string& key, const std::string& value); | ||
friend redisResponse get(Redis& redis, const std::string& key); | ||
}; | ||
|
||
} // namespace fm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters