Skip to content

Commit

Permalink
Added RedisClient wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kürzeder committed Jul 27, 2019
1 parent cc7384e commit 69aa470
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
12 changes: 6 additions & 6 deletions module/CFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int CFunctions::redis_create_client(lua_State* lua_vm)
{
// redis_client redis_create_client()
try {
const auto client = new redis_client();
const auto client = new RedisClient();
g_Module->AddRedisClient(client);

lua_pushlightuserdata(lua_vm, static_cast<void*>(client));
Expand Down Expand Up @@ -37,7 +37,7 @@ int CFunctions::redis_connect(lua_State* lua_vm)
try
{
// Read arguments
const auto client = static_cast<redis_client*>(lua_touserdata(lua_vm, 1));
const auto client = static_cast<RedisClient*>(lua_touserdata(lua_vm, 1));
const auto host = lua_tostring(lua_vm, 2);
const auto port = lua_tonumber(lua_vm, 3);

Expand Down Expand Up @@ -74,7 +74,7 @@ int CFunctions::redis_disconnect(lua_State* lua_vm)

try {
// Read arguments
const auto client = static_cast<redis_client*>(lua_touserdata(lua_vm, 1));
const auto client = static_cast<RedisClient*>(lua_touserdata(lua_vm, 1));

// verify client
if (!g_Module->HasRedisClient(client))
Expand All @@ -84,7 +84,7 @@ int CFunctions::redis_disconnect(lua_State* lua_vm)
return 1;
}

client->disconnect(true);
client->disconnect();
lua_pushboolean(lua_vm, true);
return 1;
} catch(exception& e)
Expand All @@ -108,7 +108,7 @@ int CFunctions::redis_test(lua_State* lua_vm)
try
{
// Read arguments
const auto client = static_cast<redis_client*>(lua_touserdata(lua_vm, 1));
const auto client = static_cast<RedisClient*>(lua_touserdata(lua_vm, 1));

// verify client
if (!g_Module->HasRedisClient(client))
Expand All @@ -117,7 +117,7 @@ int CFunctions::redis_test(lua_State* lua_vm)
return 1;
}

std::cout << client->is_connected() << std::endl;
std::cout << (client->is_connected() ? "true" : "false") << std::endl;
lua_pushboolean(lua_vm, true);
return 1;
} catch(std::exception& e)
Expand Down
10 changes: 5 additions & 5 deletions module/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#include <unordered_set>
#include <any>
#include <optional>
#include <cpp_redis/core/client.hpp>

#include "JobManager.h"
#include "RedisClient.h"
#include "ILuaModuleManager.h"

struct lua_State;
Expand All @@ -25,17 +25,17 @@ class Module
inline void RemoveLuaVM(lua_State* luaVM) { _luaStates.erase(luaVM); }
inline bool HasLuaVM(lua_State* luaVM) { return _luaStates.find(luaVM) != _luaStates.end(); }

inline void AddRedisClient(redis_client* client) { _redisClients.insert(client); }
inline void RemoveRedisClient(redis_client* client) { _redisClients.erase(client); }
inline bool HasRedisClient(redis_client* client) { return _redisClients.find(client) != _redisClients.end(); }
inline void AddRedisClient(RedisClient* client) { _redisClients.insert(client); }
inline void RemoveRedisClient(RedisClient* client) { _redisClients.erase(client); }
inline bool HasRedisClient(RedisClient* client) { return _redisClients.find(client) != _redisClients.end(); }

inline JobManager<const std::optional<std::any>>& GetJobManager() { return _jobManager; }

private:
ILuaModuleManager* _moduleManager;
JobManager<const std::optional<std::any>> _jobManager;
std::unordered_set<lua_State*> _luaStates;
std::unordered_set<redis_client*> _redisClients;
std::unordered_set<RedisClient*> _redisClients;
};

extern Module* g_Module;
35 changes: 35 additions & 0 deletions module/RedisClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "RedisClient.h"
#include "Module.h"

RedisClient::RedisClient()
{
_client = new redis_client();
_subscriber = new redis_subscriber();

g_Module->AddRedisClient(this);
}

RedisClient::~RedisClient()
{
g_Module->RemoveRedisClient(this);

delete _client;
delete _subscriber;
}

void RedisClient::connect(const std::string& host, const int& port) const
{
_client->connect(host, port);
_subscriber->connect(host, port);
}

void RedisClient::disconnect() const
{
_client->disconnect(true);
_subscriber->disconnect(true);
}

bool RedisClient::is_connected() const
{
return _client->is_connected() && _subscriber->is_connected();
}
21 changes: 21 additions & 0 deletions module/RedisClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include <cpp_redis/core/client.hpp>
#include <cpp_redis/core/subscriber.hpp>

using redis_client = cpp_redis::client;
using redis_subscriber = cpp_redis::subscriber;

class RedisClient
{
public:
RedisClient();
~RedisClient();

void connect(const std::string& host, const int& port) const;
void disconnect() const;
bool is_connected() const;
private:
redis_client* _client;
redis_subscriber* _subscriber;
};

0 comments on commit 69aa470

Please sign in to comment.