redis++的编译 安装 使用方案

ID:981 / 打印

小伙伴们对数据库编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《redis++的编译 安装 使用方案》,就很适合你,本篇文章讲解的知识点主要包括redis++。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

常见的是:hiredis 和hirredisvip

hiredis 和hirredisvip 都是最基础的。也没封装什么连接池啊,自动重连啊,那些东西。适合简单的场景。或者你自己手艺好,能自己封装一层好的接口。

后来尝试:cloredis

最后发现:redisplus plus

直到有一天我问同事,他们给我看redis官网推荐的C++的连接库,有好多库。好几页。而平时看的redis中文网推荐的才几个。艾玛。耽误事儿啊。

然后我接触了redisplus plus (redis++)。感觉蛮给力的玩意。

redis++地址

https://github.com/sewenew/redis-plus-plus

详细的信息可以看他们网站里的介绍。我这里只贴一段代码。

连接单机模式的

#include  #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  //using namespace std; using namespace sw::redis; using namespace std::chrono; int main() {     ConnectionOptions connection_options;     connection_options.host = "192.168.11.85";  // Required.     connection_options.port = 16379; // Optional. The default port is 6379.     //connection_options.password = "auth";   // Optional. No password by default.     connection_options.db = 5;  // Optional. Use the 0th database by default.       ConnectionPoolOptions pool_options;     pool_options.size = 3;  // Pool size, i.e. max number of connections.     pool_options.wait_timeout = std::chrono::milliseconds(100);       ConnectionOptions connection_options2;     connection_options2.host = "192.168.11.85";      connection_options2.port = 16379;     connection_options2.db = 7;       ConnectionPoolOptions pool_options7;     pool_options7.size = 3;      pool_options7.wait_timeout = std::chrono::milliseconds(100);       Redis * redisofDB1 = NULL;     Redis * redisofDB7 = NULL;     // 开始连接     try{         redisofDB1 = new Redis(connection_options, pool_options);              redisofDB7 = new Redis(connection_options2, pool_options7);     }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 ;     }       /*     std::map<:string std::string> hashTerm;     redisofDB7->hgetall("FORWARD.PLAT.DETAIL",std::inserter(hashTerm, hashTerm.end()));               for(auto it1 = hashTerm.begin() ;it1 != hashTerm.end(); it1++)     {         std::cout first second  keys;     std::map<:string std::string> hashs;     while (true) {         cursor = redisofDB7->hscan("FORWARD.PLAT.DETAIL",cursor, pattern, count, std::inserter(hashs, hashs.begin()));           if (cursor == 0) {             break;         }     }     if(hashs.size() first second hget("XNY.CARINFO","CRC01211711100232");     std::cout vv;     std::vector<:string> vlist;     while (true) {         cursor2 = redisofDB7->hscan("FORWARD.LIST.002",cursor2, pattern2, count2, std::inserter(vv, vv.begin()));           if (cursor2 == 0) {             break;         }     }       for(auto it1 = vv.begin() ;it1 != vv.end(); it1++)     {         vlist.push_back(it1->first);     }       for(auto uu = vlist.begin(); uu !=vlist.end(); uu ++ )     {         std::cout  

连接哨兵模式的

#include  #include  #include  #include  #include  #include  #include  #include  #include  #include  //using namespace std; using namespace sw::redis; int main() {     SentinelOptions sentinel_opts;     // sentinel_opts.nodes = {{"127.0.0.1", 9000},     //                     {"127.0.0.1", 9001},     //                     {"127.0.0.1", 9002}};   // Required. List of Redis Sentinel nodes.     sentinel_opts.nodes = {{"192.168.127.134", 26379}};// Required. List of Redis Sentinel nodes.     // Optional. Timeout before we successfully connect to Redis Sentinel.     // By default, the timeout is 100ms.     sentinel_opts.connect_timeout = std::chrono::milliseconds(200);       // Optional. Timeout before we successfully send request to or receive response from Redis Sentinel.     // By default, the timeout is 100ms.     sentinel_opts.socket_timeout = std::chrono::milliseconds(200);       auto sentinel = std::make_shared(sentinel_opts);         ConnectionOptions connection_opts;     //connection_opts.password = "auth";  // Optional. No password by default.     connection_opts.db = 1;      connection_opts.connect_timeout = std::chrono::milliseconds(100);   // Required.     connection_opts.socket_timeout = std::chrono::milliseconds(100);    // Required.       ConnectionPoolOptions pool_opts;     pool_opts.size = 3; // Optional. The default size is 1.       auto redis = Redis(sentinel, "mymaster", Role::MASTER, connection_opts, pool_opts);     Redis* p = &redis;     std::map<:string std::string> hash;     p->hgetall("PLATINFO",std::inserter(hash, hash.end()));       for(auto it = hash.begin() ;it != hash.end(); it++)     {         std::cout first second  hashTerm;     pp->hgetall("TERMINAL:LIST:test123456789012",std::inserter(hashTerm, hashTerm.end()));               for(auto it1 = hashTerm.begin() ;it1 != hashTerm.end(); it1++)     {         std::cout first second hexists("PLATINFO","test123456789012");     std::cout hget("PLATINFO","test1234567890123");       std::cout vPaltIDs;     p->hkeys("PLATINFO",std::inserter(vPaltIDs, vPaltIDs.end()));       for(auto vIter = vPaltIDs.begin();vIter != vPaltIDs.end(); vIter ++)     {         std::cout  

连接集群模式的

#include  #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  //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<:string> keys;     std::map<: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() first second  hsetnx("farm:command:TES21129BH2000001", std::to_string(1597739778), strValue);          std::cout hdel("farm:command:TES21129BH2000001", strfeild);          std::cout del("farm:command:NJTEST0000000009");          std::cout hlen("farm:status:TST1010191210110_20200701114501");     std::couthset("farm:clientnum","WUZ11010BC100009","009");     std::cout expire("farm:status:NJTEST0000000005_20200624135512",60); #endif       return 0; }

终于介绍完啦!小伙伴们,这篇关于《redis++的编译 安装 使用方案》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~the24.cn也会发布数据库相关知识,快来关注吧!

上一篇: Redis妙用之存储用户token问题
下一篇: Redisson分布式锁之加解锁详解

作者:admin @ 24资源网   2024-09-02

本站所有软件、源码、文章均有网友提供,如有侵权联系308410122@qq.com

与本文相关文章

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。