FromRedis.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "FromRedis.h"
  2. #include "spdlog/spdlog.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. SPDLOG_DEBUG("Redis IP: {}, Port: {}", m_redisIP, m_redisPort);
  24. SPDLOG_DEBUG("Redis Password: {}", m_redisPassword);
  25. if(m_redisContext != nullptr)
  26. {
  27. redisFree(m_redisContext);
  28. m_redisContext = nullptr;
  29. }
  30. if(m_redisIP.empty())
  31. {
  32. SPDLOG_ERROR( "Redis IP is empty");
  33. return false;
  34. }
  35. /* 连接Redis */
  36. m_redisContext = redisConnect(m_redisIP.c_str(), m_redisPort);
  37. if( (m_redisContext == nullptr) || ( m_redisContext->err != 0) )
  38. {
  39. SPDLOG_ERROR( "Connect redis failed:{}", m_redisContext->errstr);
  40. return false;
  41. }
  42. SPDLOG_INFO("Connect redis success");
  43. /* 登录认证 */
  44. if(!m_redisPassword.empty())
  45. {
  46. if(!authRedis(m_redisPassword))
  47. {
  48. SPDLOG_ERROR( "Auth redis failed");
  49. return false;
  50. }
  51. // SPDLOG_INFO( "Auth redis success");
  52. }
  53. return true;
  54. }
  55. /* 断开Redis连接 */
  56. void FromRedis::disConnectRedis()
  57. {
  58. if(m_redisContext != nullptr)
  59. {
  60. redisFree(m_redisContext);
  61. m_redisContext = nullptr;
  62. }
  63. }
  64. /* 获取redis中的String数据 */
  65. bool FromRedis::getRedisString(const std::string& key, std::string& value)
  66. {
  67. if(m_redisContext == nullptr)
  68. {
  69. SPDLOG_ERROR( "Redis no connect");
  70. return false;
  71. }
  72. value.clear();
  73. /* 获取数据 */
  74. auto reply = (redisReply*)redisCommand(m_redisContext, "GET %s", key.c_str());
  75. // auto reply = (redisReply*)redisCommand(m_redisContext, "GET 113:P100104000");
  76. if(reply == nullptr)
  77. {
  78. SPDLOG_ERROR( "Get redis value data failed");
  79. return false;
  80. }
  81. if(reply->type != REDIS_REPLY_STRING)
  82. {
  83. if(reply->type == REDIS_REPLY_NIL)
  84. {
  85. SPDLOG_WARN( "获取Redis数据失败,Key对应的值不存在,Key: {}", key);
  86. }else {
  87. /* 其他类型的返回值 */
  88. SPDLOG_ERROR( "Get redis data failed, type is not string, type: {}", reply->type);
  89. }
  90. freeReplyObject(reply);
  91. return false;
  92. }
  93. value = reply->str;
  94. freeReplyObject(reply);
  95. return true;
  96. }
  97. /* 登录认证Redis,这个需要在连接成功后调用 */
  98. bool FromRedis::authRedis(const std::string& password)
  99. {
  100. if(m_redisContext == nullptr)
  101. {
  102. SPDLOG_ERROR( "Redis no connect");
  103. return false;
  104. }
  105. auto reply = (redisReply*)redisCommand(m_redisContext, "AUTH %s", password.c_str());
  106. if(reply == nullptr)
  107. {
  108. SPDLOG_ERROR( "Redis Auth failed");
  109. return false;
  110. }
  111. /* 判断返回值 */
  112. if( reply->type == REDIS_REPLY_STATUS && strcasecmp(reply->str, "OK") == 0)
  113. {
  114. SPDLOG_INFO( "Redis Auth success");
  115. freeReplyObject(reply);
  116. return true;
  117. }
  118. else
  119. {
  120. SPDLOG_ERROR( "Redis Auth failed:{}", reply->str);
  121. freeReplyObject(reply);
  122. return false;
  123. }
  124. }