欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

redis++的編譯?安裝?使用方案

 更新時(shí)間:2023年03月27日 08:10:35   作者:wx63993a9e4baf6  
這篇文章主要介紹了redis++的編譯?安裝?使用方案的相關(guān)資料,需要的朋友可以參考下

前言

之前給公司作網(wǎng)關(guān),一直想找個(gè)牛逼點(diǎn)的C++ 的 或者 C的 redis連接庫(kù)。 結(jié)果很多都不近人意。

常見(jiàn)的是:hiredis 和hirredisvip

hiredis 和hirredisvip 都是最基礎(chǔ)的。也沒(méi)封裝什么連接池啊,自動(dòng)重連啊,那些東西。適合簡(jiǎn)單的場(chǎng)景。或者你自己手藝好,能自己封裝一層好的接口。

后來(lái)嘗試:cloredis

最后發(fā)現(xiàn):redisplus plus

直到有一天我問(wèn)同事,他們給我看redis官網(wǎng)推薦的C++的連接庫(kù),有好多庫(kù)。好幾頁(yè)。而平時(shí)看的redis中文網(wǎng)推薦的才幾個(gè)。艾瑪。耽誤事兒啊。

然后我接觸了redisplus plus (redis++)。感覺(jué)蠻給力的玩意。

redis++地址

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

詳細(xì)的信息可以看他們網(wǎng)站里的介紹。我這里只貼一段代碼。

連接單機(jī)模式的

#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;
    // 開(kāi)始連接
    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;
    }
    */
 
    // 開(kāi)始干活
    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;
}

連接哨兵模式的

#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 是大部分查詢命令的返回值類型,要想轉(zhuǎn)為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;
 
    // 測(cè)試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
//

連接集群模式的

#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 ;
    }
 
    // 連接成功- 下面開(kāi)始干活兒。
    printf("-----連接成功,下面開(kāi)始干活兒-----\n");
#if 1
    //1.先測(cè)試一個(gè)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.測(cè)試hsetnx 
    std::cout << "------------------------測(cè)試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.測(cè)試hdel 
    bool vBol2 = redisofDB1->hdel("farm:command:TES21129BH2000001", strfeild);
    
    std::cout << "hdel wanbi : " << vBol2 << std::endl;
 
    //4.測(cè)試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.測(cè)試hset
    bool bbb = redisofDB1->hset("farm:clientnum","WUZ11010BC100009","009");
    std::cout << "hset finished is :" << bbb << std::endl;
 
 
    //7.測(cè)試expire
    redisofDB1->expire("farm:status:NJTEST0000000005_20200624135512",60);
#endif
 
    return 0;
}

到此這篇關(guān)于redis++的編譯 安裝 使用方案的文章就介紹到這了,更多相關(guān)redis++的使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 一文詳解如何使用Redis實(shí)現(xiàn)分布式鎖

    一文詳解如何使用Redis實(shí)現(xiàn)分布式鎖

    這篇文章主要介紹了一文詳解如何使用Redis實(shí)現(xiàn)分布式鎖,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • Redis配置外網(wǎng)可訪問(wèn)(redis遠(yuǎn)程連接不上)的方法

    Redis配置外網(wǎng)可訪問(wèn)(redis遠(yuǎn)程連接不上)的方法

    默認(rèn)情況下,當(dāng)我們?cè)诓渴鹆藃edis服務(wù)之后,redis本身默認(rèn)只允許本地訪問(wèn)。Redis服務(wù)端只允許它所在服務(wù)器上的客戶端訪問(wèn),如果Redis服務(wù)端和Redis客戶端不在同一個(gè)機(jī)器上,就要進(jìn)行配置。
    2022-12-12
  • Redis數(shù)據(jù)過(guò)期策略的實(shí)現(xiàn)詳解

    Redis數(shù)據(jù)過(guò)期策略的實(shí)現(xiàn)詳解

    最近項(xiàng)目當(dāng)中遇到一個(gè)需求場(chǎng)景,需要清空一些存放在Redis的數(shù)據(jù),本文對(duì)Redis的過(guò)期機(jī)制簡(jiǎn)單的講解一下,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Redis使用ZSET實(shí)現(xiàn)消息隊(duì)列使用小結(jié)

    Redis使用ZSET實(shí)現(xiàn)消息隊(duì)列使用小結(jié)

    這篇文章主要介紹了Redis使用ZSET實(shí)現(xiàn)消息隊(duì)列使用總結(jié),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-03-03
  • Redis中Scan命令的踩坑實(shí)錄

    Redis中Scan命令的踩坑實(shí)錄

    這篇文章主要給大家介紹了關(guān)于Redis中Scan命令踩坑的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Redis配合SSDB實(shí)現(xiàn)持久化存儲(chǔ)代碼示例

    Redis配合SSDB實(shí)現(xiàn)持久化存儲(chǔ)代碼示例

    這篇文章主要介紹了Redis配合SSDB實(shí)現(xiàn)持久化存儲(chǔ)代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Redis集群搭建(主從模式、哨兵模式、集群模式)

    Redis集群搭建(主從模式、哨兵模式、集群模式)

    本文主要介紹了Redis集群搭建,主要包括主從模式、哨兵模式、集群模式這三種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • 使用攔截器+Redis實(shí)現(xiàn)接口冪思路詳解

    使用攔截器+Redis實(shí)現(xiàn)接口冪思路詳解

    這篇文章主要介紹了使用攔截器+Redis實(shí)現(xiàn)接口冪等,接口冪等有很多種實(shí)現(xiàn)方式,攔截器/AOP+Redis,攔截器/AOP+本地緩存等等,本文講解一下通過(guò)攔截器+Redis實(shí)現(xiàn)冪等的方式,需要的朋友可以參考下
    2023-08-08
  • redis刪除指定key的實(shí)現(xiàn)步驟

    redis刪除指定key的實(shí)現(xiàn)步驟

    本文主要介紹了redis刪除指定key的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • SpringBoot讀寫Redis客戶端并實(shí)現(xiàn)Jedis技術(shù)切換功能

    SpringBoot讀寫Redis客戶端并實(shí)現(xiàn)Jedis技術(shù)切換功能

    這篇文章主要介紹了SpringBoot讀寫Redis客戶端并實(shí)現(xiàn)技術(shù)切換功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-01-01

最新評(píng)論