博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 使用 hiredis 封装redis 的数据获取接口
阅读量:6690 次
发布时间:2019-06-25

本文共 5236 字,大约阅读时间需要 17 分钟。

整合自互联网

一、hiredis 类库的安装

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

makemake installmkdir /usr/lib/hirediscp libhiredis.so /usr/lib///将动态连接库libhiredis.so至/usr/lib/mkdir /usr/include/hirediscp hiredis.h /usr/include/hiredis   //头文件包含#include

 

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

redisUtils.h

/* * redis.h * *  Created on: 2018年6月7日 *      Author: oftenlin */#ifndef REDIS_H_#define REDIS_H_#include 
#include
#include
#include
#include
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

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: %s\n", 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) %s\n", 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");}

 

转载地址:http://ljkoo.baihongyu.com/

你可能感兴趣的文章
LaTeX特殊字符
查看>>
Golang 错误和异常处理的正确姿势
查看>>
JXL的api的简单介绍
查看>>
好久不写文章了
查看>>
声明和定义的区别(深入理解)
查看>>
java连接oracle小小demo
查看>>
分布式系统测试那些事儿——错误注入
查看>>
android-------非常好的图片加载框架和缓存库(Picasso)
查看>>
Titanium, PhoneGap, Sencha Touch, jQuery Mobile – Clearing up confusion
查看>>
eclipse如何部署Web工程到tomcat中
查看>>
在CentOS7上安装JDK1.8
查看>>
搜索和网页排名的数学原理
查看>>
Xcode项目中同一个名称不同位置 简单修改
查看>>
java设计模式-建造者模式
查看>>
Android Shell 快捷键
查看>>
iOS泛型
查看>>
Maven--pom.xml 配置详解之一
查看>>
oracle笔记
查看>>
ContentProvider数据更新
查看>>
Java 关于Ajax的实例--验证用户名(四)
查看>>