FromRedis.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "FromRedis.h"
  2. #include "fmtlog.h"
  3. FromRedis::FromRedis()
  4. {
  5. }
  6. FromRedis::~FromRedis()
  7. {
  8. }
  9. /* 设置IP和端口 */
  10. void FromRedis::setRedisIPAndPort(const std::string& ip, int port)
  11. {
  12. m_redisIP = ip;
  13. m_redisPort = port;
  14. }
  15. /* 设置密码 */
  16. void FromRedis::setRedisPassword(const std::string& password)
  17. {
  18. m_redisPassword = password;
  19. }
  20. /* 连接Redis */
  21. bool FromRedis::connectRedis()
  22. {
  23. if(m_redisContext != nullptr)
  24. {
  25. redisFree(m_redisContext);
  26. m_redisContext = nullptr;
  27. }
  28. if(m_redisIP.empty())
  29. {
  30. FMTLOG_ERROR( "Redis IP is empty");
  31. return false;
  32. }
  33. /* 连接Redis */
  34. m_redisContext = redisConnect(m_redisIP.c_str(), m_redisPort);
  35. if( (m_redisContext == nullptr) || ( m_redisContext->err != 0) )
  36. {
  37. FMTLOG_ERROR( "Connect redis failed:{}", m_redisContext->errstr);
  38. return false;
  39. }
  40. FMTLOG_INFO("Connect redis success");
  41. /* 登录认证 */
  42. if(!m_redisPassword.empty())
  43. {
  44. if(!authRedis(m_redisPassword))
  45. {
  46. FMTLOG_ERROR( "Auth redis failed");
  47. return false;
  48. }
  49. // FMTLOG_INFO( "Auth redis success");
  50. }
  51. return true;
  52. }
  53. /* 断开Redis连接 */
  54. void FromRedis::disConnectRedis()
  55. {
  56. if(m_redisContext != nullptr)
  57. {
  58. redisFree(m_redisContext);
  59. m_redisContext = nullptr;
  60. }
  61. }
  62. /* 获取redis中的String数据 */
  63. bool FromRedis::getRedisString(const std::string& key, std::string& value)
  64. {
  65. if(m_redisContext == nullptr)
  66. {
  67. FMTLOG_ERROR( "Redis no connect");
  68. return false;
  69. }
  70. value.clear();
  71. /* 获取数据 */
  72. auto reply = (redisReply*)redisCommand(m_redisContext, "GET %s", key.c_str());
  73. // auto reply = (redisReply*)redisCommand(m_redisContext, "GET 113:P100104000");
  74. if(reply == nullptr)
  75. {
  76. FMTLOG_ERROR( "Get redis string data failed");
  77. return false;
  78. }
  79. if(reply->type != REDIS_REPLY_STRING)
  80. {
  81. FMTLOG_ERROR( "Get redis data failed, type is not string, type: {}", reply->type);
  82. freeReplyObject(reply);
  83. return false;
  84. }
  85. value = reply->str;
  86. freeReplyObject(reply);
  87. return true;
  88. }
  89. /* 登录认证Redis,这个需要在连接成功后调用 */
  90. bool FromRedis::authRedis(const std::string& password)
  91. {
  92. if(m_redisContext == nullptr)
  93. {
  94. FMTLOG_ERROR( "Redis no connect");
  95. return false;
  96. }
  97. auto reply = (redisReply*)redisCommand(m_redisContext, "AUTH %s", password.c_str());
  98. if(reply == nullptr)
  99. {
  100. FMTLOG_ERROR( "Redis Auth failed");
  101. return false;
  102. }
  103. /* 判断返回值 */
  104. if( reply->type == REDIS_REPLY_STATUS && strcasecmp(reply->str, "OK") == 0)
  105. {
  106. FMTLOG_INFO( "Redis Auth success");
  107. freeReplyObject(reply);
  108. return true;
  109. }
  110. else
  111. {
  112. FMTLOG_ERROR( "Redis Auth failed:{}", reply->str);
  113. freeReplyObject(reply);
  114. return false;
  115. }
  116. }