FromRedis.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /* 获取数据 */
  71. auto reply = (redisReply*)redisCommand(m_redisContext, "GET %s", key.c_str());
  72. // auto reply = (redisReply*)redisCommand(m_redisContext, "GET 113:P100104000");
  73. if(reply == nullptr)
  74. {
  75. FMTLOG_ERROR( "Get redis string data failed");
  76. return false;
  77. }
  78. if(reply->type != REDIS_REPLY_STRING)
  79. {
  80. FMTLOG_ERROR( "Get redis data failed, type is not string, type: {}, error str: {}", reply->type, reply->str);
  81. freeReplyObject(reply);
  82. return false;
  83. }
  84. value = reply->str;
  85. freeReplyObject(reply);
  86. return true;
  87. }
  88. /* 登录认证Redis,这个需要在连接成功后调用 */
  89. bool FromRedis::authRedis(const std::string& password)
  90. {
  91. if(m_redisContext == nullptr)
  92. {
  93. FMTLOG_ERROR( "Redis no connect");
  94. return false;
  95. }
  96. auto reply = (redisReply*)redisCommand(m_redisContext, "AUTH %s", password.c_str());
  97. if(reply == nullptr)
  98. {
  99. FMTLOG_ERROR( "Redis Auth failed");
  100. return false;
  101. }
  102. /* 判断返回值 */
  103. if( reply->type == REDIS_REPLY_STATUS && strcasecmp(reply->str, "OK") == 0)
  104. {
  105. FMTLOG_INFO( "Redis Auth success");
  106. freeReplyObject(reply);
  107. return true;
  108. }
  109. else
  110. {
  111. FMTLOG_ERROR( "Redis Auth failed:{}", reply->str);
  112. freeReplyObject(reply);
  113. return false;
  114. }
  115. }