| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- #include "CRedis.h"
- #include <string.h>
- #define LOG_WARN printf
- #define LOG_ERROR printf
- #define LOG_INFO printf
- #define LOG_DEBUG printf
- #ifdef WIN32
- #define NO_QFORKIMPL //这一行必须加才能正常使用
- #include "hiredis\msvs\win32_interop\win32_fixes.h"
- #pragma comment(lib,"hiredis.lib")
- #pragma comment(lib,"win32_interop.lib")
- #endif // WIN32
- CRedis::CRedis()
- {
- this->_connect = nullptr;
- this->_reply = nullptr;
- m_pHost = nullptr;
- m_port = 6379;
- m_pPwd = nullptr;
- m_isConnect = false;
- }
- CRedis::~CRedis()
- {
- if (this->_reply != nullptr)
- freeReplyObject(this->_reply);
- if (this->_connect != nullptr)
- redisFree(this->_connect);
- this->_connect = nullptr;
- this->_reply = nullptr;
- }
- bool CRedis::connect(const char * host, int port, const char * password)
- {
- LOG_INFO("Redis连接[%s][%d][%s]\n", host, port, password);
- if (this->_connect != nullptr)
- redisFree(this->_connect);
- this->_connect = nullptr;
- struct timeval timeout = { 1, 500000 }; // 1.5 seconds
- this->_connect = redisConnectWithTimeout(host, port, timeout);
- if (this->_connect == nullptr || this->_connect->err) {
- if (this->_connect) {
- LOG_ERROR("Connection error: %s\n", this->_connect->errstr);
- redisFree(this->_connect);
- }
- else {
- LOG_ERROR("Connection error: can't allocate redis context\n");
- }
- return false;
- }
- if (password == "")
- {
- m_isConnect = true;
- return true;
- }
- this->_reply = (redisReply *)redisCommand(this->_connect, "AUTH %s", password);
- if (this->_reply->type == REDIS_REPLY_ERROR) {
- LOG_ERROR("Redis认证失败!\n");
- return false;
- }
- else
- {
- LOG_INFO("Redis认证成功!\n");
- m_isConnect = true;
- if (this->m_pHost == nullptr)
- {
- std::uint32_t len = strlen(host);
- this->m_pHost = std::make_unique<char[]>(len + 1);
- memcpy(this->m_pHost.get(), host, len);
- }
- else
- {
- this->m_pHost.reset();
- std::uint32_t len = strlen(host);
- this->m_pHost = std::make_unique<char[]>(len + 1);
- memcpy(this->m_pHost.get(), host, len);
- }
- if (this->m_pPwd == nullptr)
- {
- std::uint32_t len = strlen(password);
- this->m_pPwd = std::make_unique<char[]>(len + 1);
- memcpy(this->m_pPwd.get(), password, len);
- }
- else
- {
- }
- this->m_port = port;
- /* PING server */
- static bool isFirst = true;
- if (isFirst)
- {
- isFirst = false;
- __testCon();
- }
- }
- return true;
- }
- bool CRedis::set(const char * key, const char * value)
- {
- if (!m_isConnect) return false;
- std::unique_lock<std::mutex>lock(mut);
- this->_reply = (redisReply*)redisCommand(this->_connect, "SET %s %s", key, value);
- int n = strcmp(this->_reply->str, "OK");
- freeReplyObject(this->_reply);
- LOG_DEBUG("SET %s %s\n", key, value);
- if (n == 0) return true;
- return false;
- }
- bool CRedis::set(const char * key, const char * value, int second)
- {
- if (!m_isConnect) return false;
- std::unique_lock<std::mutex>lock(mut);
- this->_reply = (redisReply*)redisCommand(this->_connect, "SET %s %s EX %d", key, value, second);
- int n = strcmp(this->_reply->str, "OK");
- freeReplyObject(this->_reply);
- LOG_DEBUG("SET %s %s EX %d\n", key, value, second);
- if (n == 0) return true;
- return false;
- }
- void CRedis::push(const char * key, const char * values)
- {
- if (!m_isConnect) return;
- std::unique_lock<std::mutex>lock(mut);
- this->_reply = (redisReply*)redisCommand(this->_connect, "PUBLISH %s %s", key, values);
- int n = 0;
- if (this->_reply->str != nullptr)
- n = strcmp(this->_reply->str, "OK");
- if (n == 0)
- {
- LOG_DEBUG("推送成功");
- }
- else
- {
- //LOG_WARN("推送失败[%s]", this->_reply->str);
- // "MOVED 6368 192.168.0.75:6381"
- std::string str = this->_reply->str;
- if (str.find("MOVED") == std::string::npos)
- {
- LOG_WARN("推送失败[%s]", this->_reply->str);
- return;
- }
- auto index = str.find_last_of(' ');
- auto spli = str.find(':');
- std::string ip = str.substr(index + 1, spli - index - 1);
- std::string port = str.substr(spli + 1, str.length() - spli);
- LOG_INFO("推送[%s][%s]", ip.c_str(), port.c_str());
- freeReplyObject(this->_reply);
- connect(ip.c_str(), atoi(port.c_str()), this->m_pPwd.get());
- this->_reply = (redisReply*)redisCommand(this->_connect, "PUBLISH %s %s", key, values);
- LOG_INFO("推送结果[%s]", this->_reply->str);
- }
- freeReplyObject(this->_reply);
- }
- void CRedis::push(const char * key, std::list<const char*> &values)
- {
- if (!m_isConnect) return;
- std::unique_lock<std::mutex>lock(mut);
- while (!values.empty())
- {
- this->_reply = (redisReply*)redisCommand(this->_connect, "LPUSH %s %s", key, values.front());
- freeReplyObject(this->_reply);
- values.pop_front();
- }
- }
- void CRedis::pull(const char * key, std::list<const char*>& values)
- {
- if (!m_isConnect) return;
- std::unique_lock<std::mutex>lock(mut);
- this->_reply = (redisReply*)redisCommand(this->_connect, "LRANGE %s 0 -1", key);
- if (this->_reply->type == REDIS_REPLY_ARRAY) {
- for (size_t j = 0; j < this->_reply->elements; j++) {
- values.emplace_back(this->_reply->element[j]->str);
- }
- }
- freeReplyObject(this->_reply);
- }
- const char * CRedis::get(const char * key)
- {
- if (!m_isConnect) return nullptr;
- std::unique_lock<std::mutex>lock(mut);
- this->_reply = (redisReply*)redisCommand(this->_connect, "GET %s", key);
- if (this->_reply->type == REDIS_REPLY_STRING)
- {
- char* str = this->_reply->str;
- freeReplyObject(this->_reply);
- return str;
- }
- else if (this->_reply->type == REDIS_REPLY_NIL)
- {
- LOG_DEBUG("GET %s: 空\n", key);
- freeReplyObject(this->_reply);
- return nullptr;
- }
- else
- {
- LOG_DEBUG("GET key:%s value:%s type:%d\n", key, this->_reply->str, this->_reply->type);
- freeReplyObject(this->_reply);
- return nullptr;
- }
- }
- void CRedis::__delKey(const char * key)
- {
- if (!m_isConnect) return;
- //std::unique_lock<std::mutex>lock(mut);
- this->_reply = (redisReply*)redisCommand(this->_connect, "DEL %s", key);
- freeReplyObject(this->_reply);
- }
- bool CRedis::__testCon()
- {
- std::unique_lock<std::mutex>lock(mut);
- this->_reply = (redisReply*)redisCommand(this->_connect, "SET %s %s", "test", "test");
- int n = strcmp(this->_reply->str, "OK");
- std::string str = this->_reply->str;
- freeReplyObject(this->_reply);
- __delKey("test");
- if (n == 0) return true;
- auto index = str.find_last_of(' ');
- auto spli = str.find(':');
- std::string ip = str.substr(index + 1, spli - index - 1);
- std::string port = str.substr(spli + 1, str.length() - spli);
- return connect(ip.c_str(), atoi(port.c_str()), this->m_pPwd.get());
- }
- std::shared_ptr<CRedis> CRedis::pInstance(new CRedis);
|