C++ 使用 hiredis 封装redis 的数据获取接口

整合自互联网

一、hiredis 类库的安装

tar -zxvf hiredis-v0.13.3.tar.gz

make
make install

mkdir /usr/lib/hiredis
cp libhiredis.so /usr/lib/
//将动态连接库libhiredis.so至/usr/lib/
mkdir /usr/include/hiredis
cp hiredis.h /usr/include/hiredis   //头文件包含#include<hiredis/hiredis.h>

 

二、封装 redisUtil.h 访问 实现 redis 的连接,按 key 来获取各个类型的数据

redisUtils.h

C++ 使用 hiredis 封装redis 的数据获取接口

/*
 * redis.h
 *
 *  Created on: 2018年6月7日
 *      Author: oftenlin
 */

#ifndef REDIS_H_
#define REDIS_H_

#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>
#include <hiredis/hiredis.h>

class Redis
{
    public:
        Redis();
        ~Redis();
        bool connect(std::string host, int port);
        std::string get(std::string key);
        void set(std::string key, std::string value);
        std::string hget(const char* key,const  char* hkey);
        int existsKey(const char* ID);
        int del(const char* key);
        int hset(const char* key,const char* hkey,const char* hvalue, size_t hvaluelen);
        int hset(const char* key, const char* hkey, const char* value);
        void lpush(std::string key, std::string value);
        int lget(std::string key,int begin_index,int end_index);
    private:
        redisContext* _connect;
        redisReply* _reply;

};

#endif /* REDIS_H_ */

View Code

redisUtils.cpp

C++ 使用 hiredis 封装redis 的数据获取接口

  1 /*
  2  * redis.cpp
  3  *
  4  *  Created on: 2018年6月7日
  5  *      Author: oftenlin
  6  */
  7 
  8 #include "redis.h"
  9 
 10 Redis::Redis(){
 11 
 12 }
 13 
 14 Redis::~Redis()
 15 {
 16     this->_connect = NULL;
 17     this->_reply = NULL;
 18 }
 19 
 20 bool Redis::connect(std::string host, int port)
 21 {
 22     this->_connect = redisConnect(host.c_str(), port);
 23     if(this->_connect != NULL && this->_connect->err)
 24     {
 25         printf("connect error: %sn", this->_connect->errstr);
 26         return 0;
 27     }
 28     return 1;
 29 }
 30 
 31 std::string Redis::get(std::string key)
 32 {
 33     std::string str ="";
 34     this->_reply = (redisReply*)redisCommand(this->_connect, "GET %s", key.c_str());
 35     if(this->_reply==NULL){
 36         return str;
 37     }
 38     str = this->_reply->str;
 39     freeReplyObject(this->_reply);
 40     return str;
 41 
 42 }
 43 
 44 void Redis::set(std::string key, std::string value)
 45 {
 46     redisCommand(this->_connect, "SET %s %s", key.c_str(), value.c_str());
 47 }
 48 
 49 void Redis::lpush(std::string key, std::string value)
 50 {
 51     redisCommand(this->_connect, "LPUSH %s %s", key.c_str(), value.c_str());
 52 }
 53 
 54 int Redis::lget(std::string key,int begin_index,int end_index){
 55     /* Let's check what we have inside the list */
 56     redisReply* reply = (redisReply*) redisCommand(this->_connect,"LRANGE %s %d %d",key.c_str(),begin_index,end_index);
 57     if (reply->type == REDIS_REPLY_ARRAY) {
 58         for (int j = 0; j < reply->elements; j++) {
 59             printf("%u) %sn", j, reply->element[j]->str);
 60 //            data.push_back(_reply->element[j]->str);
 61 
 62         }
 63     }
 64     freeReplyObject(reply);
 65     return 1;
 66 }
 67 
 68 std::string Redis::hget(const char* key,const  char* hkey){
 69     const char* argv[3];
 70     size_t argvlen[3];
 71     argv[0] = "HGET";
 72     argvlen[0] = 4;
 73     argv[1] = key;
 74     argvlen[1] = strlen(key);
 75     argv[2] = hkey;
 76     argvlen[2] = strlen(hkey);
 77     redisReply* reply =(redisReply*) redisCommandArgv(this->_connect, 3, argv, argvlen);
 78     std::string value;
 79     if(reply->type != REDIS_REPLY_NIL){
 80         value = std::string(reply->str,reply->str + reply->len);
 81     }
 82     freeReplyObject(reply);
 83     return value;
 84 }
 85 int Redis::hset(const char* key, const char* hkey, const char* value){
 86     redisReply* reply =(redisReply*) redisCommand(this->_connect, "HSET %s %s %s",key,hkey, value);
 87     freeReplyObject(reply);
 88     return 1;
 89 }
 90 int Redis::hset(const char* key,const char* hkey,const char* hvalue, size_t hvaluelen){
 91     const char* argv[4];
 92     size_t argvlen[4];
 93     argv[0] = "HSET";
 94     argvlen[0] = 4;
 95     argv[1] = key;
 96     argvlen[1] = strlen(key);
 97     argv[2] = hkey;
 98     argvlen[2] = strlen(hkey);
 99     argv[3] = hvalue;
100     argvlen[3] = hvaluelen;
101     redisReply * reply =(redisReply*) redisCommandArgv(this->_connect, 4, argv, argvlen);
102     freeReplyObject(reply);
103     return 1;
104 }
105 
106 int Redis::del(const char* key){
107     int res = 0;
108     redisReply* reply = (redisReply*)redisCommand(this->_connect, "DEL %s", key);
109     if(reply->type == REDIS_REPLY_INTEGER){
110         if(reply->integer == 1L)
111             res = 1;
112     }
113     freeReplyObject(reply);
114     return res;
115 }
116 
117 /*if Key ID exists*/
118 int Redis::existsKey(const char* ID){
119     redisReply * reply = (redisReply*)redisCommand(this->_connect,"exists %s",ID);
120     int res = 0;
121     if(reply->type == REDIS_REPLY_INTEGER){
122         if(reply->integer == 1L)
123             res  = 1;
124     }
125     freeReplyObject(reply);
126     return res;
127 }

View Code

 

三、使用示例

#include "write2db/redis.h"
int main(int argc, char **argv) {
    Redis *r = new Redis();
    if(!r->connect("localhost", 6379)){
        printf("redis connect error!n");
    return 0;
    }
    
    std::string time_str = r->get("mykey");

}    

 

原文链接: https://www.cnblogs.com/oftenlin/p/9487076.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++ 使用 hiredis 封装redis 的数据获取接口

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/279493

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 上午4:12
下一篇 2023年2月15日 上午4:13

相关推荐