正文

redis++的编译?安装?使用方案(redis5编译安装)深度揭秘

文章摘要

这篇文章介绍了在C++环境中使用Redis++库进行Redis数据库操作的实现方法。主要内容包括: 1. 配置Redis连接,设置服务器地址、端口和默认数据库。 2. 使用hscan命令进行数据扫描,测试Redis的分布式键值存储功能。 3. 通过hsetnx、hdel、del、hlen、hset等命令执行一系列Redis操作,并处理相关错误。 4. 文章还提到错误处理机制,包括捕获各种错误类型并打印日志。 总结展示了如何在C++程序中集成Redis++库,并完成基本的Redis操作。

#include <unistd.h> #include <chrono> #include <tuple> #include <iostream> #include <vector> #include <map> #include <unordered_set> #include <sw/redis++/redis++.h> #include <sw/redis++/sentinel.h> #include <sw/redis++/connection.h> #include <sw/redis++/connection_pool.h> //using namespace std; using namespace sw::redis; using namespace std::chrono; int main() { ConnectionOptions connection_options; connection_options.host=”192.168.11.124″; // Required. connection_options.port=7001; // Optional. The default port is 6379. //connection_options.password=”auth”; // Optional. No password by default. connection_options.db=0; // Optional. Use the 0th database by default. ConnectionPoolOptions pool_options; pool_options.size=5; // Pool size, i.e. max number of connections. pool_options.wait_timeout=std::chrono::milliseconds(100); RedisCluster* redisofDB1=NULL; try{ redisofDB1=new RedisCluster(connection_options, pool_options); }catch (const ReplyError &err) { printf(“RedisHandler– ReplyError:%s \n”,err.what()); return false ; }catch (const TimeoutError &err) { printf(“RedisHandler– TimeoutError%s \n”,err.what()); return false ; }catch (const ClosedError &err) { printf(“RedisHandler– ClosedError%s \n”,err.what()); return false ; }catch (const IoError &err) { printf(“RedisHandler– IoError%s \n”,err.what()); return false ; }catch (const Error &err) { printf(“RedisHandler– other%s \n”,err.what()); return false ; } // 连接成功- 下面开始干活儿。 printf(“—–连接成功,下面开始干活儿—–\n”); #if 1 //1.先测试一个gethall 用hscan代替得吧 auto cursor=0LL; auto pattern=”*”; auto count=5; //std::unordered_set<std::string> keys; std::map<std::string, std::string> hashs; while (true) { cursor=redisofDB1->hscan(“farm:status:NJTEST0000000005_20200702132812”,cursor, pattern, count, std::inserter(hashs, hashs.begin())); if (cursor==0) { break; } } if(hashs.size() < 1) { printf(“we get nothing !\n”); } std::cout << “the hashs.size=” << hashs.size() << std::endl; for(auto it1=hashs.begin() ;it1 !=hashs.end(); it1++) { std::cout <<“key:” <<it1->first << “Value:”<<it1->second <<std::endl; } #endif #if 0 // 2.测试hsetnx std::cout << “————————测试hsetnx—————————” << std::endl; std::string strValue=”SUBMIT$2$TES21129BH2000001$UPDATE$TIME:20200818163607,TYPE:3,VALUE:MjIzLjIyMy4xODcuMzUsMTA1MCxmdGFkbWluLGZ0YWRtaW44MTY0NSxINENfVjEuMy4zN18yMDIwMDgxN19hNDdhLmJpbiww,MODEL:2″; //std::string strfeild=”1597739777″; bool vBol=redisofDB1->hsetnx(“farm:command:TES21129BH2000001”, std::to_string(1597739778), strValue); std::cout << “hsetnx wanbi : ” << vBol << std::endl; //3.测试hdel bool vBol2=redisofDB1->hdel(“farm:command:TES21129BH2000001”, strfeild); std::cout << “hdel wanbi : ” << vBol2 << std::endl; //4.测试del bool vBol3=redisofDB1->del(“farm:command:NJTEST0000000009”); std::cout << “del wanbi : ” << vBol3 << std::endl; //5.hlen long long llen=redisofDB1->hlen(“farm:status:TST1010191210110_20200701114501”); std::cout<< “the len is :” << llen << std::endl; //6.测试hset bool bbb=redisofDB1->hset(“farm:clientnum”,”WUZ11010BC100009″,”009″); std::cout << “hset finished is :” << bbb << std::endl; //7.测试expire redisofDB1->expire(“farm:status:NJTEST0000000005_20200624135512”,60); #endif return 0; }