溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

redis++怎么編譯、安裝及使用

發(fā)布時(shí)間:2023-03-27 09:50:53 來(lái)源:億速云 閱讀:129 作者:iii 欄目:開發(fā)技術(shù)

這篇“redis++怎么編譯、安裝及使用”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“redis++怎么編譯、安裝及使用”文章吧。

前言

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

常見的是:hiredis 和hirredisvip

hiredis 和hirredisvip 都是最基礎(chǔ)的。也沒封裝什么連接池啊,自動(dòng)重連啊,那些東西。適合簡(jiǎn)單的場(chǎng)景?;蛘吣阕约菏炙嚭?,能自己封裝一層好的接口。

后來(lái)嘗試:cloredis

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

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

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

redis++怎么編譯、安裝及使用

連接單機(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;
    // 開始連接
    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;
}

連接哨兵模式的

#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 ;
    }
 
    // 連接成功- 下面開始干活兒。
    printf("-----連接成功,下面開始干活兒-----\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++怎么編譯、安裝及使用”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI