{"id":201831,"date":"2025-05-10T13:34:02","date_gmt":"2025-05-10T05:34:02","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201831\/"},"modified":"2025-05-10T13:34:02","modified_gmt":"2025-05-10T05:34:02","slug":"redis%e7%9a%84%e7%bc%96%e8%af%91-%e5%ae%89%e8%a3%85-%e4%bd%bf%e7%94%a8%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201831\/","title":{"rendered":"redis++\u7684\u7f16\u8bd1\u00a0\u5b89\u88c5\u00a0\u4f7f\u7528\u65b9\u6848"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>redis++\u7684\u7f16\u8bd1&nbsp;\u5b89\u88c5&nbsp;\u4f7f\u7528\u65b9\u6848<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6570\u636e\u5e93\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aredis++\u7684\u7f16\u8bd1&nbsp;\u5b89\u88c5&nbsp;\u4f7f\u7528\u65b9\u6848\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ecredis++\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u5e38\u89c1\u7684\u662f\uff1ahiredis \u548chirredisvip<\/p>\n<p>hiredis \u548chirredisvip \u90fd\u662f\u6700\u57fa\u7840\u7684\u3002\u4e5f\u6ca1\u5c01\u88c5\u4ec0\u4e48\u8fde\u63a5\u6c60\u554a\uff0c\u81ea\u52a8\u91cd\u8fde\u554a\uff0c\u90a3\u4e9b\u4e1c\u897f\u3002\u9002\u5408\u7b80\u5355\u7684\u573a\u666f\u3002\u6216\u8005\u4f60\u81ea\u5df1\u624b\u827a\u597d\uff0c\u80fd\u81ea\u5df1\u5c01\u88c5\u4e00\u5c42\u597d\u7684\u63a5\u53e3\u3002<\/p>\n<p>\u540e\u6765\u5c1d\u8bd5\uff1acloredis<\/p>\n<p>\u6700\u540e\u53d1\u73b0\uff1aredisplus plus<\/p>\n<p>\u76f4\u5230\u6709\u4e00\u5929\u6211\u95ee\u540c\u4e8b\uff0c\u4ed6\u4eec\u7ed9\u6211\u770bredis\u5b98\u7f51\u63a8\u8350\u7684C++\u7684\u8fde\u63a5\u5e93\uff0c\u6709\u597d\u591a\u5e93\u3002\u597d\u51e0\u9875\u3002\u800c\u5e73\u65f6\u770b\u7684redis\u4e2d\u6587\u7f51\u63a8\u8350\u7684\u624d\u51e0\u4e2a\u3002\u827e\u739b\u3002\u803d\u8bef\u4e8b\u513f\u554a\u3002<\/p>\n<p>\u7136\u540e\u6211\u63a5\u89e6\u4e86redisplus plus (redis++\uff09\u3002\u611f\u89c9\u86ee\u7ed9\u529b\u7684\u73a9\u610f\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683956351645f227f69d3e.jpg1683956351645f227f69d53.png\" class=\"aligncenter\"><\/p>\n<h2>redis++\u5730\u5740<\/h2>\n<p>https:\/\/github.com\/sewenew\/redis-plus-plus<\/p>\n<p>\u8be6\u7ec6\u7684\u4fe1\u606f\u53ef\u4ee5\u770b\u4ed6\u4eec\u7f51\u7ad9\u91cc\u7684\u4ecb\u7ecd\u3002\u6211\u8fd9\u91cc\u53ea\u8d34\u4e00\u6bb5\u4ee3\u7801\u3002<\/p>\n<h2>\u8fde\u63a5\u5355\u673a\u6a21\u5f0f\u7684<\/h2>\n<pre>#include \n   \n    \n#include \n    \n      #include \n     \n       #include \n      \n        #include \n       \n         #include \n         #include \n         \n           #include \n          \n            #include \n           \n             #include \n            \n              #include \n             \n               \/\/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; \/\/ \u5f00\u59cb\u8fde\u63a5 try{ redisofDB1 = new Redis(connection_options, pool_options); redisofDB7 = new Redis(connection_options2, pool_options7); }catch (const ReplyError &amp;err) { printf(\"RedisHandler-- ReplyError\uff1a%s \\n\",err.what()); return false ; }catch (const TimeoutError &amp;err) { printf(\"RedisHandler-- TimeoutError%s \\n\",err.what()); return false ; }catch (const ClosedError &amp;err) { printf(\"RedisHandler-- ClosedError%s \\n\",err.what()); return false ; }catch (const IoError &amp;err) { printf(\"RedisHandler-- IoError%s \\n\",err.what()); return false ; }catch (const Error &amp;err) { printf(\"RedisHandler-- other%s \\n\",err.what()); return false ; } \/* std::map&lt;:string std::string&gt; hashTerm; redisofDB7-&gt;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&lt;:string std::string&gt; hashs; while (true) { cursor = redisofDB7-&gt;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&lt;:string&gt; vlist; while (true) { cursor2 = redisofDB7-&gt;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-&gt;first); } for(auto uu = vlist.begin(); uu !=vlist.end(); uu ++ ) { std::cout \n              <h2>\u8fde\u63a5\u54e8\u5175\u6a21\u5f0f\u7684<\/h2> \n              <pre>#include \n               \n                \n#include \n                \n                  #include \n                 \n                   #include \n                  \n                    #include \n                   \n                     #include \n                     #include \n                     \n                       #include \n                      \n                        #include \n                       \n                         #include \n                        \n                          \/\/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\n                         \n                          (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 = &amp;redis; std::map&lt;:string std::string&gt; hash; p-&gt;hgetall(\"PLATINFO\",std::inserter(hash, hash.end())); for(auto it = hash.begin() ;it != hash.end(); it++) { std::cout first second hashTerm; pp-&gt;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-&gt;hkeys(\"PLATINFO\",std::inserter(vPaltIDs, vPaltIDs.end())); for(auto vIter = vPaltIDs.begin();vIter != vPaltIDs.end(); vIter ++) { std::cout \n                          <h2>\u8fde\u63a5\u96c6\u7fa4\u6a21\u5f0f\u7684<\/h2> \n                          <pre>#include \n                           \n                            \n#include \n                            \n                              #include \n                             \n                               #include \n                              \n                                #include \n                               \n                                 #include \n                                 #include \n                                 \n                                   #include \n                                  \n                                    #include \n                                   \n                                     #include \n                                    \n                                      #include \n                                     \n                                       \/\/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 &amp;err) { printf(\"RedisHandler-- ReplyError\uff1a%s \\n\",err.what()); return false ; }catch (const TimeoutError &amp;err) { printf(\"RedisHandler-- TimeoutError%s \\n\",err.what()); return false ; }catch (const ClosedError &amp;err) { printf(\"RedisHandler-- ClosedError%s \\n\",err.what()); return false ; }catch (const IoError &amp;err) { printf(\"RedisHandler-- IoError%s \\n\",err.what()); return false ; }catch (const Error &amp;err) { printf(\"RedisHandler-- other%s \\n\",err.what()); return false ; } \/\/ \u8fde\u63a5\u6210\u529f- \u4e0b\u9762\u5f00\u59cb\u5e72\u6d3b\u513f\u3002 printf(\"-----\u8fde\u63a5\u6210\u529f\uff0c\u4e0b\u9762\u5f00\u59cb\u5e72\u6d3b\u513f-----\\n\"); #if 1 \/\/1.\u5148\u6d4b\u8bd5\u4e00\u4e2agethall \u7528hscan\u4ee3\u66ff\u5f97\u5427 auto cursor = 0LL; auto pattern = \"*\"; auto count = 5; \/\/std::unordered_set&lt;:string&gt; keys; std::map&lt;:string std::string&gt; hashs; while (true) { cursor = redisofDB1-&gt;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; }\n                                      \n                                      \n                                     \n                                    \n                                   \n                                  \n                                 \n                               \n                              \n                             \n                            \n                           <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>redis++\u7684\u7f16\u8bd1&nbsp;&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201831","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201831"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201831\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}