redisoperation.h
#pragma once
#include "hiredis.h"
//对redis进行操作的类
class CRedisOperation
{
public:
CRedisOperation();
~CRedisOperation();
public:
void doTest();
};
redisoperation.cpp
#include "RedisOperation.h" #include#include #include #include #include #include #include "hiredis.h" #include #include using namespace std; //构造函数 CRedisOperation::CRedisOperation() { } //析构函数 CRedisOperation::~CRedisOperation() { } //测试函数 void CRedisOperation::doTest() { //连接redis 需要启动才可以进行连接 //redis默认监听端口为6387 可以再配置文件中修改 redisContext* c = redisConnect("127.0.0.1", 6379); if (c->err) { printf("Connect to redisServer faile:%sn", c->errstr); redisFree(c); return; } printf("Connect to redisServer Successn"); //为redis设置key const char* command1 = "set stest1 value1"; //执行redis命令行 redisReply * r = (redisReply *) redisCommand(c, command1); //如果返回NULL则说明执行失败 if (NULL == r) { printf("Execut command1 failuren"); redisFree(c); return; } //如果执行失败则释放连接 if (!(r->type == REDIS_REPLY_STATUS && (strcmp(r->str, "OK") == 0 || strcmp(r->str, "ok") == 0))) { printf("Failed to execute command[%s]n", command1); freeReplyObject(r); redisFree(c); return; } //执行成功 释放redisCommand执行后返回的redisReply所占用的内存 freeReplyObject(r); printf("Succeed to execute command[%s]n", command1); const char * command2 = "strlen stest1"; r = (redisReply *) redisCommand(c, command2); //如果返回类型不是整形 则释放连接 if (r->type != REDIS_REPLY_INTEGER) { printf("Failed to execute command[%s]n", command2); freeReplyObject(r); redisFree(c); return; } //获取字符串长度 int length = r->integer; freeReplyObject(r); printf("The length of 'stest1' is %d.n", length); printf("Succeed to execute command[%s]n", command2); //获取redis键值对信息 const char * command3 = "get stest1"; r = (redisReply *) redisCommand(c, command3); if (r->type != REDIS_REPLY_STRING) { printf("Failed to execute command[%s]n", command3); freeReplyObject(r); redisFree(c); return; } printf("The value of 'stest1' is %sn", r->str); freeReplyObject(r); printf("Succeed to execute command[%s]n", command3); const char * command4 = "get stest2"; r = (redisReply *) redisCommand(c, command4); if (r->type != REDIS_REPLY_NIL) { printf("Failed to execute command[%s]n", command4); freeReplyObject(r); redisFree(c); return; } freeReplyObject(r); printf("Succeed to execute command[%s]n", command4); //为hash设置键值对值 const char * hset = "hset hushtest 001 leida1 002 leida2"; r = (redisReply *)redisCommand(c, hset); if (!r && !(strcmp(r->str, "OK") == 0 || strcmp(r->str, "ok") == 0)) { printf("执行失败"); freeReplyObject(r); redisFree(c); return; } else { freeReplyObject(r); } //获取指定雷达的键值对 const char * hmget = "hmget hushtest 001 003"; r = (redisReply *)redisCommand(c, hmget); for (int i = 0; i < r->elements; i = i + 1) { if (!r->element[i]->str) { printf("不存在改键值对,从数据库中获取后存入redis中 执行hset函数"); } else { //获取字符串的值 string value = r->element[i]->str; } } freeReplyObject(r); //获取hmap的键值对 根据查询的返回 第一个参数为fileds 第二个参数为value const char * command5 = "hgetall hushtest"; r = (redisReply *)redisCommand(c, command5); for (int i = 0; i < r->elements; i = i+2) { string id = r->element[i]->str; string value = r->element[i + 1]->str; } freeReplyObject(r); //释放连接资源 redisFree(c); }



