服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Redis - redis++的编译 安装 使用方案

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

2023-03-30 15:48wx63993a9e4baf6 Redis

这篇文章主要介绍了redis++的编译 安装 使用方案的相关资料,需要的朋友可以参考下

前言

之前给公司作网关,一直想找个牛逼点的C++ 的 或者 C的 redis连接库。 结果很多都不近人意。

常见的是:hiredis 和hirredisvip

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

后来尝试:cloredis

最后发现:redisplus plus

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

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

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

redis++地址

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

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

连接单机模式的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#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.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<std::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 <<"Plat ID:"  <<it1->first <<std::endl;
        std::cout <<  "Plat UserName & Password"<<it1->second <<std::endl;
    }
    */
 
    // 开始干活
    auto cursor = 0LL;
    auto pattern = "*";
    auto count = 5;
    //std::unordered_set<std::string> keys;
    std::map<std::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() < 1)
    {
        printf("we get nothing !\n");
    }
    for(auto it1 = hashs.begin() ;it1 != hashs.end(); it1++)
    {
        std::cout <<"Plat ID:"  <<it1->first <<std::endl;
        std::cout <<  "Plat UserName & Password"<<it1->second <<std::endl;
    }
 
    OptionalString strValue = redisofDB1->hget("XNY.CARINFO","CRC01211711100232");
    std::cout<< " CRC01211711100232  的 vin :" << *strValue << std::endl;
    std::string straa = *strValue;
    if(straa.empty())
    {
           std::cout << "we gete nothing " << std::endl ;
    }
    std::cout<< " ---- CRC01211711100232  的 details :" << straa << std::endl;
 
    std::cout<< " ---- 下面试试hincrby ---- " << std::endl;
 
 
 
    auto cursor2 = 0LL;
    auto pattern2 ="*";
    auto count2 = 20;
    std::map<std::string, std::string> vv;
    std::vector<std::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 << *uu << std::endl;
    }
 
    return 0;
}

连接哨兵模式的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <unistd.h>
#include <chrono>
#include <tuple>
#include <iostream>
#include <vector>
#include <map>
#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;
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<std::string, std::string> hash;
    p->hgetall("PLATINFO",std::inserter(hash, hash.end()));
 
    for(auto it = hash.begin() ;it != hash.end(); it++)
    {
        std::cout <<"Plat ID:"  <<it->first <<std::endl;
        std::cout <<  "Plat UserName & Password"<<it->second <<std::endl;
    }
 
    ConnectionOptions connection_opts2;
    //connection_opts.password = "auth";  // Optional. No password by default.
    connection_opts2.db = 2;
    connection_opts2.connect_timeout = std::chrono::milliseconds(100);   // Required.
    connection_opts2.socket_timeout = std::chrono::milliseconds(100);    // Required.
 
    auto redisDB2 = Redis(sentinel, "mymaster", Role::MASTER, connection_opts2, pool_opts);
    Redis*pp  = &redisDB2;
    std::map<std::string, std::string> hashTerm;
    pp->hgetall("TERMINAL:LIST:test123456789012",std::inserter(hashTerm, hashTerm.end()));
    
    
    for(auto it1 = hashTerm.begin() ;it1 != hashTerm.end(); it1++)
    {
        std::cout <<"Plat ID:"  <<it1->first <<std::endl;
        std::cout <<  "Plat UserName & Password"<<it1->second <<std::endl;
    }
 
    // 是否存在
    bool bb = p->hexists("PLATINFO","test123456789012");
    std::cout << "PLATINFO 里 存在 test123456789012:" << bb << std::endl;
 
 
    // hget - 注意这里,OptionalString 是大部分查询命令的返回值类型,要想转为string 需要加*
    OptionalString strValue = p->hget("PLATINFO","test1234567890123");
 
    std::cout<< " test123456789012  的 details :" << *strValue << std::endl;
    std::string straa = *strValue;
    if(straa.empty())
        {
           std::cout << "we gete nothing " << std::endl ;
        }
    std::cout<< " ---- test123456789012  的 details :" << straa << std::endl;
 
    // 测试hkeys
    std::vector<std::string> vPaltIDs;
    p->hkeys("PLATINFO",std::inserter(vPaltIDs, vPaltIDs.end()));
 
    for(auto vIter = vPaltIDs.begin();vIter != vPaltIDs.end(); vIter ++)
    {
        std::cout << *vIter << std::endl;
    }
    return 0;
}
 
 
 
 
//g++ -std=c++11 -I/usr/local/include -L/usr/local/lib -Wl,-rpath=../libc++ -o app testRedisSentinel.cpp -lredis++ -lhiredis -pthread
//

连接集群模式的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#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;
}

到此这篇关于redis++的编译 安装 使用方案的文章就介绍到这了,更多相关redis++的使用内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.51cto.com/u_15912066/6148762

延伸 · 阅读

精彩推荐
  • Redis关于Redis未授权访问漏洞利用的介绍与修复建议

    关于Redis未授权访问漏洞利用的介绍与修复建议

    Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API,下面这篇文章主要给大家介绍了...

    burlin5032019-11-06
  • Redis如何高效使用Redis作为LRU缓存

    如何高效使用Redis作为LRU缓存

    这篇文章主要介绍了如何高效使用Redis作为LRU缓存,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考...

    程序零世界3702020-09-12
  • RedisRedis删除策略的三种方法及逐出算法

    Redis删除策略的三种方法及逐出算法

    这篇文章主要介绍了Redis删除策略的三种方法及逐出算法,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下...

    我是一棵卷心菜6152022-08-01
  • RedisRedis做数据持久化的解决方案及底层原理

    Redis做数据持久化的解决方案及底层原理

    Redis有两种方式来实现数据的持久化,分别是RDB(Redis Database)和AOF(Append Only File),今天通过本文给大家聊一聊Redis做数据持久化的解决方案及底层原理,...

    S.H5032021-08-17
  • RedisRedis教程(十三):管线详解

    Redis教程(十三):管线详解

    这篇文章主要介绍了Redis教程(十三):管线详解,本文讲解了请求应答协议和RTT、管线(pipelining)、Benchmark等内容,需要的朋友可以参考下 ...

    Redis教程网3722019-10-24
  • RedisCentOS系统下Redis安装和自启动配置的步骤

    CentOS系统下Redis安装和自启动配置的步骤

    相信大家都知道Redis是一个C实现的基于内存、可持久化的键值对数据库,在分布式服务中常作为缓存服务。所以这篇文章将详细介绍在CentOS系统下如何从零...

    daisy2852019-10-30
  • Redis解决linux下redis数据库overcommit_memory问题

    解决linux下redis数据库overcommit_memory问题

    这篇文章介绍了解决linux下redis数据库overcommit_memory问题的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学...

    whycold9692022-02-24
  • Redis浅谈Redis存储数据类型及存取值方法

    浅谈Redis存储数据类型及存取值方法

    这篇文章主要介绍了浅谈Redis存储数据类型及存取值方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋...

    心寒丶11222021-08-02