redis++的编译 安装 使用方案
来源:脚本之家
2023-05-13 13:22:34
0浏览
收藏
小伙伴们对数据库编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《redis++的编译 安装 使用方案》,就很适合你,本篇文章讲解的知识点主要包括redis++。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!
常见的是:hiredis 和hirredisvip
hiredis 和hirredisvip 都是最基础的。也没封装什么连接池啊,自动重连啊,那些东西。适合简单的场景。或者你自己手艺好,能自己封装一层好的接口。
后来尝试:cloredis
最后发现:redisplus plus
直到有一天我问同事,他们给我看redis官网推荐的C++的连接库,有好多库。好几页。而平时看的redis中文网推荐的才几个。艾玛。耽误事儿啊。
然后我接触了redisplus plus (redis++)。感觉蛮给力的玩意。
redis++地址
https://github.com/sewenew/redis-plus-plus
详细的信息可以看他们网站里的介绍。我这里只贴一段代码。
连接单机模式的
#include <unistd.h> #include <chrono> #include <tuple> #include <iostream> #include <vector> #include <map> #include <unordered_set> #include <sw> #include <sw> #include <sw> #include <sw> //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 <h2>连接哨兵模式的</h2> <pre class="brush:cpp;">#include <unistd.h> #include <chrono> #include <tuple> #include <iostream> #include <vector> #include <map> #include <sw> #include <sw> #include <sw> #include <sw> //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>(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 <h2>连接集群模式的</h2> <pre class="brush:cpp;">#include <unistd.h> #include <chrono> #include <tuple> #include <iostream> #include <vector> #include <map> #include <unordered_set> #include <sw> #include <sw> #include <sw> #include <sw> //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; }</:string></:string></sw></sw></sw></sw></unordered_set></map></vector></iostream></tuple></chrono></unistd.h>
终于介绍完啦!小伙伴们,这篇关于《redis++的编译 安装 使用方案》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布数据库相关知识,快来关注吧!
版本声明
本文转载于:脚本之家 如有侵犯,请联系study_golang@163.com删除

- 上一篇
- Golang中指针的使用详解

- 下一篇
- 宝塔中ThinkPHP框架使用Redis的一系列教程
查看更多
最新文章
-
- 数据库 · Redis | 48秒前 |
- RedisSentinel高可用配置详解
- 468浏览 收藏
-
- 数据库 · Redis | 11小时前 |
- Redis配置加密技巧分享
- 268浏览 收藏
-
- 数据库 · Redis | 4天前 |
- Redis性能优化配置指南
- 182浏览 收藏
-
- 数据库 · Redis | 4天前 |
- RedisHyperLogLog大数据统计技巧
- 305浏览 收藏
-
- 数据库 · Redis | 6天前 |
- Redis安全配置参数设置详解
- 252浏览 收藏
-
- 数据库 · Redis | 6天前 |
- 不同环境Redis安全配置对比与调整方法
- 374浏览 收藏
-
- 数据库 · Redis | 6天前 |
- RedisList队列优化方法分享
- 311浏览 收藏
-
- 数据库 · Redis | 1星期前 |
- Redis主从复制故障排查指南
- 178浏览 收藏
-
- 数据库 · Redis | 1星期前 |
- Redis原子操作详解与实战应用
- 469浏览 收藏
-
- 数据库 · Redis | 1星期前 |
- Redis崩溃后重启与数据恢复方法
- 153浏览 收藏
-
- 数据库 · Redis | 1星期前 |
- Redis安全配置:强密码与访问控制设置教程
- 440浏览 收藏
-
- 数据库 · Redis | 1星期前 |
- Redis单节点迁移集群的实用方法
- 376浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 514次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 499次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
查看更多
AI推荐
-
- AI Mermaid流程图
- SEO AI Mermaid 流程图工具:基于 Mermaid 语法,AI 辅助,自然语言生成流程图,提升可视化创作效率,适用于开发者、产品经理、教育工作者。
- 635次使用
-
- 搜获客【笔记生成器】
- 搜获客笔记生成器,国内首个聚焦小红书医美垂类的AI文案工具。1500万爆款文案库,行业专属算法,助您高效创作合规、引流的医美笔记,提升运营效率,引爆小红书流量!
- 642次使用
-
- iTerms
- iTerms是一款专业的一站式法律AI工作台,提供AI合同审查、AI合同起草及AI法律问答服务。通过智能问答、深度思考与联网检索,助您高效检索法律法规与司法判例,告别传统模板,实现合同一键起草与在线编辑,大幅提升法律事务处理效率。
- 657次使用
-
- TokenPony
- TokenPony是讯盟科技旗下的AI大模型聚合API平台。通过统一接口接入DeepSeek、Kimi、Qwen等主流模型,支持1024K超长上下文,实现零配置、免部署、极速响应与高性价比的AI应用开发,助力专业用户轻松构建智能服务。
- 726次使用
-
- 迅捷AIPPT
- 迅捷AIPPT是一款高效AI智能PPT生成软件,一键智能生成精美演示文稿。内置海量专业模板、多样风格,支持自定义大纲,助您轻松制作高质量PPT,大幅节省时间。
- 621次使用
查看更多
相关文章
-
- redis复制有可能碰到的问题汇总
- 2023-01-01 501浏览
-
- 使用lua+redis解决发多张券的并发问题
- 2023-01-27 501浏览
-
- Redis应用实例分享:社交媒体平台设计
- 2023-06-21 501浏览
-
- 使用Python和Redis构建日志分析系统:如何实时监控系统运行状况
- 2023-08-08 501浏览
-
- 如何利用Redis和Python实现消息队列功能
- 2023-08-16 501浏览