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

时间:2023-03-09 10:04:48
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

/*
* 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_ */

redisUtils.cpp

 /*
* redis.cpp
*
* Created on: 2018年6月7日
* Author: oftenlin
*/ #include "redis.h" Redis::Redis(){ } Redis::~Redis()
{
this->_connect = NULL;
this->_reply = NULL;
} bool Redis::connect(std::string host, int port)
{
this->_connect = redisConnect(host.c_str(), port);
if(this->_connect != NULL && this->_connect->err)
{
printf("connect error: %s\n", this->_connect->errstr);
return ;
}
return ;
} std::string Redis::get(std::string key)
{
std::string str ="";
this->_reply = (redisReply*)redisCommand(this->_connect, "GET %s", key.c_str());
if(this->_reply==NULL){
return str;
}
str = this->_reply->str;
freeReplyObject(this->_reply);
return str; } void Redis::set(std::string key, std::string value)
{
redisCommand(this->_connect, "SET %s %s", key.c_str(), value.c_str());
} void Redis::lpush(std::string key, std::string value)
{
redisCommand(this->_connect, "LPUSH %s %s", key.c_str(), value.c_str());
} int Redis::lget(std::string key,int begin_index,int end_index){
/* Let's check what we have inside the list */
redisReply* reply = (redisReply*) redisCommand(this->_connect,"LRANGE %s %d %d",key.c_str(),begin_index,end_index);
if (reply->type == REDIS_REPLY_ARRAY) {
for (int j = ; j < reply->elements; j++) {
printf("%u) %s\n", j, reply->element[j]->str);
// data.push_back(_reply->element[j]->str); }
}
freeReplyObject(reply);
return ;
} std::string Redis::hget(const char* key,const char* hkey){
const char* argv[];
size_t argvlen[];
argv[] = "HGET";
argvlen[] = ;
argv[] = key;
argvlen[] = strlen(key);
argv[] = hkey;
argvlen[] = strlen(hkey);
redisReply* reply =(redisReply*) redisCommandArgv(this->_connect, , argv, argvlen);
std::string value;
if(reply->type != REDIS_REPLY_NIL){
value = std::string(reply->str,reply->str + reply->len);
}
freeReplyObject(reply);
return value;
}
int Redis::hset(const char* key, const char* hkey, const char* value){
redisReply* reply =(redisReply*) redisCommand(this->_connect, "HSET %s %s %s",key,hkey, value);
freeReplyObject(reply);
return ;
}
int Redis::hset(const char* key,const char* hkey,const char* hvalue, size_t hvaluelen){
const char* argv[];
size_t argvlen[];
argv[] = "HSET";
argvlen[] = ;
argv[] = key;
argvlen[] = strlen(key);
argv[] = hkey;
argvlen[] = strlen(hkey);
argv[] = hvalue;
argvlen[] = hvaluelen;
redisReply * reply =(redisReply*) redisCommandArgv(this->_connect, , argv, argvlen);
freeReplyObject(reply);
return ;
} int Redis::del(const char* key){
int res = ;
redisReply* reply = (redisReply*)redisCommand(this->_connect, "DEL %s", key);
if(reply->type == REDIS_REPLY_INTEGER){
if(reply->integer == 1L)
res = ;
}
freeReplyObject(reply);
return res;
} /*if Key ID exists*/
int Redis::existsKey(const char* ID){
redisReply * reply = (redisReply*)redisCommand(this->_connect,"exists %s",ID);
int res = ;
if(reply->type == REDIS_REPLY_INTEGER){
if(reply->integer == 1L)
res = ;
}
freeReplyObject(reply);
return res;
}

三、使用示例

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