{"id":71089,"date":"2026-02-03T07:05:10","date_gmt":"2026-02-02T23:05:10","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/71089.html"},"modified":"2026-02-03T07:05:10","modified_gmt":"2026-02-02T23:05:10","slug":"redis-set%e4%b8%8ec%e5%ae%9e%e6%88%98%ef%bc%9a%e9%ab%98%e6%95%88%e9%9b%86%e5%90%88%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/71089.html","title":{"rendered":"Redis SET\u4e0eC++\u5b9e\u6218\uff1a\u9ad8\u6548\u96c6\u5408\u64cd\u4f5c"},"content":{"rendered":"<p>\u597d\u7684&#xff0c;\u6211\u4eec\u6765\u6df1\u5165\u63a2\u8ba8 Redis \u4e2d\u7684 SET \u6570\u636e\u7c7b\u578b\u53ca\u5176\u5728 C&#043;&#043; \u4e2d\u7684\u5b9e\u6218\u5e94\u7528\u3002Redis \u7684 SET \u662f\u4e00\u4e2a\u65e0\u5e8f\u7684\u3001\u5143\u7d20\u552f\u4e00\u7684\u96c6\u5408&#xff0c;\u975e\u5e38\u9002\u5408\u5904\u7406\u9700\u8981\u53bb\u91cd\u3001\u6210\u5458\u5173\u7cfb\u5224\u65ad\u6216\u96c6\u5408\u8fd0\u7b97\u7684\u573a\u666f\u3002<\/p>\n<h4>1. Redis SET \u6838\u5fc3\u7279\u6027\u56de\u987e<\/h4>\n<ul>\n<li>\u65e0\u5e8f\u6027 (Unordered): \u5143\u7d20\u5b58\u50a8\u987a\u5e8f\u4e0d\u56fa\u5b9a\u3002<\/li>\n<li>\u552f\u4e00\u6027 (Unique): \u6bcf\u4e2a\u5143\u7d20\u5728\u96c6\u5408\u4e2d\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21\u3002<\/li>\n<li>\u9ad8\u6548\u64cd\u4f5c: \u652f\u6301\u6dfb\u52a0\u3001\u5220\u9664\u3001\u5224\u65ad\u6210\u5458\u5b58\u5728\u6027\u3001\u96c6\u5408\u8fd0\u7b97&#xff08;\u4ea4\u96c6\u3001\u5e76\u96c6\u3001\u5dee\u96c6&#xff09;\u7b49\u64cd\u4f5c&#xff0c;\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a $O(1)$ \u6216 $O(N)$&#xff08;N \u4e3a\u96c6\u5408\u5927\u5c0f\u6216\u53c2\u4e0e\u8fd0\u7b97\u7684\u96c6\u5408\u5927\u5c0f&#xff09;\u3002<\/li>\n<li>\u4e8c\u8fdb\u5236\u5b89\u5168: \u5143\u7d20\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32&#xff08;\u5305\u62ec\u4e8c\u8fdb\u5236\u6570\u636e&#xff09;\u3002<\/li>\n<li>\u6700\u5927\u6210\u5458\u6570: $2^{32} &#8211; 1$&#xff08;\u7ea6 40 \u4ebf\u4e2a&#xff09;\u3002<\/li>\n<\/ul>\n<h4>2. C&#043;&#043; \u5ba2\u6237\u7aef\u5e93\u9009\u62e9 (\u4ee5 hiredis \u4e3a\u4f8b)<\/h4>\n<p>hiredis \u662f Redis \u5b98\u65b9\u63a8\u8350\u7684\u8f7b\u91cf\u7ea7 C \u5ba2\u6237\u7aef\u5e93\u3002C&#043;&#043; \u5e94\u7528\u4e2d\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528&#xff0c;\u6216\u8005\u5c01\u88c5\u6210\u66f4 C&#043;&#043; \u98ce\u683c\u7684\u63a5\u53e3\u3002\u8fd9\u91cc\u6211\u4eec\u4e3b\u8981\u4f7f\u7528 hiredis \u7684\u540c\u6b65 API\u3002<\/p>\n<p>#include &lt;hiredis\/hiredis.h&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>\/\/ \u7b80\u5355\u5c01\u88c5\u4e00\u4e2a\u8fde\u63a5\u7c7b (\u7b80\u5316\u7248&#xff0c;\u5b9e\u9645\u5e94\u7528\u4e2d\u9700\u5904\u7406\u8fde\u63a5\u6c60\u3001\u9519\u8bef\u7b49)<br \/>\nclass RedisClient {<br \/>\npublic:<br \/>\n    RedisClient(const char* host &#061; &#034;127.0.0.1&#034;, int port &#061; 6379) {<br \/>\n        context_ &#061; redisConnect(host, port);<br \/>\n        if (context_ &#061;&#061; nullptr || context_-&gt;err) {<br \/>\n            throw std::runtime_error(&#034;Redis connection error&#034;);<br \/>\n        }<br \/>\n    }<br \/>\n    ~RedisClient() {<br \/>\n        redisFree(context_);<br \/>\n    }<br \/>\n    redisContext* context() { return context_; }<br \/>\nprivate:<br \/>\n    redisContext* context_;<br \/>\n};<\/p>\n<h4>3. C&#043;&#043; \u5b9e\u6218&#xff1a;SET \u64cd\u4f5c\u6df1\u5ea6\u89e3\u6790<\/h4>\n<h5>3.1 \u57fa\u7840\u64cd\u4f5c (SADD, SREM, SMEMBERS, SISMEMBER)<\/h5>\n<p>void basicSetOperations(RedisClient&amp; client) {<br \/>\n    redisContext* ctx &#061; client.context();<\/p>\n<p>    \/\/ SADD: \u5411\u96c6\u5408\u6dfb\u52a0\u4e00\u4e2a\u6216\u591a\u4e2a\u6210\u5458<br \/>\n    redisReply* reply &#061; (redisReply*)redisCommand(ctx, &#034;SADD myset member1 member2 member3&#034;);<br \/>\n    if (reply &#061;&#061; nullptr || reply-&gt;type &#061;&#061; REDIS_REPLY_ERROR) {<br \/>\n        std::cerr &lt;&lt; &#034;SADD error&#034; &lt;&lt; std::endl;<br \/>\n    } else {<br \/>\n        std::cout &lt;&lt; &#034;SADD added &#034; &lt;&lt; reply-&gt;integer &lt;&lt; &#034; members.&#034; &lt;&lt; std::endl;<br \/>\n    }<br \/>\n    freeReplyObject(reply);<\/p>\n<p>    \/\/ SMEMBERS: \u83b7\u53d6\u96c6\u5408\u6240\u6709\u6210\u5458 (\u6ce8\u610f&#xff1a;\u5927\u6570\u636e\u96c6\u614e\u7528&#xff0c;\u53ef\u80fd\u963b\u585e)<br \/>\n    reply &#061; (redisReply*)redisCommand(ctx, &#034;SMEMBERS myset&#034;);<br \/>\n    if (reply &amp;&amp; reply-&gt;type &#061;&#061; REDIS_REPLY_ARRAY) {<br \/>\n        std::cout &lt;&lt; &#034;Members of myset: &#034;;<br \/>\n        for (size_t i &#061; 0; i &lt; reply-&gt;elements; &#043;&#043;i) {<br \/>\n            std::cout &lt;&lt; reply-&gt;element[i]-&gt;str &lt;&lt; &#034; &#034;;<br \/>\n        }<br \/>\n        std::cout &lt;&lt; std::endl;<br \/>\n    }<br \/>\n    freeReplyObject(reply);<\/p>\n<p>    \/\/ SISMEMBER: \u68c0\u67e5\u6210\u5458\u662f\u5426\u5b58\u5728<br \/>\n    reply &#061; (redisReply*)redisCommand(ctx, &#034;SISMEMBER myset member2&#034;);<br \/>\n    if (reply &amp;&amp; reply-&gt;type &#061;&#061; REDIS_REPLY_INTEGER) {<br \/>\n        std::cout &lt;&lt; &#034;member2 exists? &#034; &lt;&lt; (reply-&gt;integer &#061;&#061; 1 ? &#034;Yes&#034; : &#034;No&#034;) &lt;&lt; std::endl;<br \/>\n    }<br \/>\n    freeReplyObject(reply);<\/p>\n<p>    \/\/ SREM: \u79fb\u9664\u4e00\u4e2a\u6216\u591a\u4e2a\u6210\u5458<br \/>\n    reply &#061; (redisReply*)redisCommand(ctx, &#034;SREM myset member1&#034;);<br \/>\n    if (reply) {<br \/>\n        std::cout &lt;&lt; &#034;Removed &#034; &lt;&lt; reply-&gt;integer &lt;&lt; &#034; members.&#034; &lt;&lt; std::endl;<br \/>\n    }<br \/>\n    freeReplyObject(reply);<br \/>\n}<\/p>\n<h5>3.2 \u96c6\u5408\u8fd0\u7b97 (SINTER, SUNION, SDIFF)<\/h5>\n<p>void setAlgebra(RedisClient&amp; client) {<br \/>\n    redisContext* ctx &#061; client.context();<\/p>\n<p>    \/\/ \u51c6\u5907\u4e24\u4e2a\u96c6\u5408<br \/>\n    redisCommand(ctx, &#034;SADD setA a b c d&#034;);<br \/>\n    redisCommand(ctx, &#034;SADD setB c d e f&#034;);<\/p>\n<p>    \/\/ SINTER: \u4ea4\u96c6 (\u540c\u65f6\u5b58\u5728\u4e8e setA \u548c setB \u7684\u5143\u7d20)<br \/>\n    redisReply* reply &#061; (redisReply*)redisCommand(ctx, &#034;SINTER setA setB&#034;);<br \/>\n    if (reply &amp;&amp; reply-&gt;type &#061;&#061; REDIS_REPLY_ARRAY) {<br \/>\n        std::cout &lt;&lt; &#034;Intersection (SINTER): &#034;;<br \/>\n        for (size_t i &#061; 0; i &lt; reply-&gt;elements; &#043;&#043;i) {<br \/>\n            std::cout &lt;&lt; reply-&gt;element[i]-&gt;str &lt;&lt; &#034; &#034;;<br \/>\n        }<br \/>\n        std::cout &lt;&lt; std::endl;<br \/>\n    }<br \/>\n    freeReplyObject(reply);<\/p>\n<p>    \/\/ SUNION: \u5e76\u96c6 (\u5b58\u5728\u4e8e setA \u6216 setB \u6216\u4e24\u8005\u4e2d\u7684\u5143\u7d20)<br \/>\n    reply &#061; (redisReply*)redisCommand(ctx, &#034;SUNION setA setB&#034;);<br \/>\n    \/\/ &#8230; \u5904\u7406\u5e76\u96c6\u7ed3\u679c<br \/>\n    freeReplyObject(reply);<\/p>\n<p>    \/\/ SDIFF: \u5dee\u96c6 (\u5b58\u5728\u4e8e setA \u4f46\u4e0d\u5b58\u5728\u4e8e setB \u7684\u5143\u7d20)<br \/>\n    reply &#061; (redisReply*)redisCommand(ctx, &#034;SDIFF setA setB&#034;);<br \/>\n    \/\/ &#8230; \u5904\u7406\u5dee\u96c6\u7ed3\u679c<br \/>\n    freeReplyObject(reply);<br \/>\n}<\/p>\n<p>http:\/\/my.tv.sohu.com\/us\/442092364\/698249292.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTI5Mi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249079.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTA3OS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249343.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTM0My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249502.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTUwMi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249356.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTM1Ni5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249358.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTM1OC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249516.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTUxNi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249365.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTM2NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249432.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTQzMi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249436.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTQzNi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249446.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTQ0Ni5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249388.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTM4OC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249541.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTU0MS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249544.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTU0NC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249638.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTYzOC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249704.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTcwNC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249647.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTY0Ny5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249708.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTcwOC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249712.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTcxMi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249484.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTQ4NC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249723.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTcyMy5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249574.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTU3NC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249577.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTU3Ny5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249580.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTU4MC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249784.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTc4NC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250017.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDAxNy5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250110.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDExMC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249878.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTg3OC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250043.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDA0My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250044.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDA0NC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250136.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDEzNi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250055.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDA1NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250143.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDE0My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250062.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDA2Mi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250067.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDA2Ny5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250204.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDIwNC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249998.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTk5OC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250081.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDA4MS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250160.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDE2MC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250091.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDA5MS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250321.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDMyMS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250100.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDEwMC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250182.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDE4Mi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250236.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDIzNi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250337.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDMzNy5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250243.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDI0My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250348.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDM0OC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250506.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDUwNi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250354.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDM1NC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250256.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDI1Ni5zaHRtbA&#061;&#061;.html \u00a0<\/p>\n<h5>3.3 \u9ad8\u7ea7\u6280\u5de7\u4e0e\u4f18\u5316<\/h5>\n<ul>\n<li>SSCAN \u4ee3\u66ff SMEMBERS: \u5bf9\u4e8e\u5927\u578b\u96c6\u5408&#xff0c;\u4f7f\u7528 SMEMBERS \u4e00\u6b21\u6027\u83b7\u53d6\u6240\u6709\u6210\u5458\u53ef\u80fd\u5bfc\u81f4\u670d\u52a1\u5668\u963b\u585e\u548c\u5ba2\u6237\u7aef\u5185\u5b58\u6ea2\u51fa\u3002\u5e94\u4f7f\u7528 SSCAN \u8fdb\u884c\u589e\u91cf\u8fed\u4ee3\u3002 void safeScanSet(RedisClient&amp; client, const std::string&amp; key) {<br \/>\n    redisContext* ctx &#061; client.context();<br \/>\n    size_t cursor &#061; 0;<br \/>\n    do {<br \/>\n        redisReply* reply &#061; (redisReply*)redisCommand(ctx, &#034;SSCAN %s %zu&#034;, key.c_str(), cursor);<br \/>\n        if (reply &#061;&#061; nullptr || reply-&gt;type !&#061; REDIS_REPLY_ARRAY || reply-&gt;elements !&#061; 2) {<br \/>\n            \/\/ \u9519\u8bef\u5904\u7406<br \/>\n            break;<br \/>\n        }<br \/>\n        \/\/ \u89e3\u6790\u65b0\u7684\u6e38\u6807<br \/>\n        redisReply* cursor_reply &#061; reply-&gt;element[0];<br \/>\n        cursor &#061; std::stoul(cursor_reply-&gt;str);<br \/>\n        \/\/ \u89e3\u6790\u5143\u7d20\u6570\u7ec4<br \/>\n        redisReply* elements_reply &#061; reply-&gt;element[1];<br \/>\n        for (size_t i &#061; 0; i &lt; elements_reply-&gt;elements; &#043;&#043;i) {<br \/>\n            std::cout &lt;&lt; elements_reply-&gt;element[i]-&gt;str &lt;&lt; &#034; &#034;;<br \/>\n        }<br \/>\n        freeReplyObject(reply);<br \/>\n    } while (cursor !&#061; 0); \/\/ \u6e38\u6807\u4e3a 0 \u8868\u793a\u8fed\u4ee3\u7ed3\u675f<br \/>\n}<\/p>\n<\/li>\n<li>\u539f\u5b50\u6027\u4e0e\u4e8b\u52a1 (MULTI\/EXEC): \u5982\u679c\u9700\u8981\u786e\u4fdd\u4e00\u7cfb\u5217 SET \u64cd\u4f5c\u7684\u539f\u5b50\u6027&#xff08;\u5982\u6dfb\u52a0\u6210\u5458\u540e\u7acb\u5373\u68c0\u67e5\u5927\u5c0f&#xff09;&#xff0c;\u53ef\u4ee5\u4f7f\u7528 MULTI \u5f00\u542f\u4e8b\u52a1&#xff0c;\u53d1\u9001\u547d\u4ee4\u961f\u5217&#xff0c;\u7136\u540e\u7528 EXEC \u6267\u884c\u3002<\/li>\n<li>\u7ba1\u9053 (Pipelining): \u5c06\u591a\u4e2a\u547d\u4ee4\u4e00\u6b21\u6027\u53d1\u9001\u5230\u670d\u52a1\u5668&#xff0c;\u51cf\u5c11\u7f51\u7edc\u5f80\u8fd4\u6b21\u6570&#xff0c;\u663e\u8457\u63d0\u5347\u6279\u91cf\u64cd\u4f5c\u7684\u6027\u80fd\u3002 void pipelineSetAdd(RedisClient&amp; client, const std::string&amp; key, const std::vector&lt;std::string&gt;&amp; members) {<br \/>\n    redisContext* ctx &#061; client.context();<br \/>\n    \/\/ \u5f00\u542f\u7ba1\u9053\u6a21\u5f0f<br \/>\n    redisAppendCommand(ctx, &#034;MULTI&#034;);<br \/>\n    for (const auto&amp; member : members) {<br \/>\n        redisAppendCommand(ctx, &#034;SADD %s %s&#034;, key.c_str(), member.c_str());<br \/>\n    }<br \/>\n    redisAppendCommand(ctx, &#034;EXEC&#034;);<\/p>\n<p>    \/\/ \u53d1\u9001\u5e76\u83b7\u53d6\u6240\u6709\u56de\u590d (\u5ffd\u7565\u4e2d\u95f4\u56de\u590d&#xff0c;\u53ea\u5173\u6ce8 EXEC \u7684\u56de\u590d)<br \/>\n    redisReply* multiReply, *execReply;<br \/>\n    redisGetReply(ctx, (void**)&amp;multiReply); \/\/ MULTI \u7684 OK<br \/>\n    freeReplyObject(multiReply);<br \/>\n    for (size_t i &#061; 0; i &lt; members.size(); &#043;&#043;i) {<br \/>\n        redisGetReply(ctx, (void**)&amp;multiReply); \/\/ \u6bcf\u4e2a SADD \u7684 QUEUED<br \/>\n        freeReplyObject(multiReply);<br \/>\n    }<br \/>\n    redisGetReply(ctx, (void**)&amp;execReply); \/\/ EXEC \u7684\u7ed3\u679c\u6570\u7ec4<br \/>\n    if (execReply &amp;&amp; execReply-&gt;type &#061;&#061; REDIS_REPLY_ARRAY) {<br \/>\n        \/\/ \u5904\u7406\u6bcf\u4e2a SADD \u7684\u7ed3\u679c (execReply-&gt;elements &#061;&#061; members.size())<br \/>\n    }<br \/>\n    freeReplyObject(execReply);<br \/>\n}<\/p>\n<\/li>\n<li>SET \u5b9e\u73b0\u5206\u5e03\u5f0f\u9501 (\u7b80\u6613\u7248): \u5229\u7528 SET \u7684 NX (\u4e0d\u5b58\u5728\u624d\u8bbe\u7f6e) \u548c PX (\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4) \u9009\u9879\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u57fa\u7840\u7684\u5206\u5e03\u5f0f\u9501\u3002 bool tryAcquireLock(RedisClient&amp; client, const std::string&amp; lockKey, const std::string&amp; lockValue, int expireMs) {<br \/>\n    redisContext* ctx &#061; client.context();<br \/>\n    \/\/ SET lockKey lockValue NX PX expireMs<br \/>\n    redisReply* reply &#061; (redisReply*)redisCommand(ctx, &#034;SET %s %s NX PX %d&#034;, lockKey.c_str(), lockValue.c_str(), expireMs);<br \/>\n    if (reply &#061;&#061; nullptr || reply-&gt;type &#061;&#061; REDIS_REPLY_ERROR) {<br \/>\n        freeReplyObject(reply);<br \/>\n        return false;<br \/>\n    }<br \/>\n    bool acquired &#061; (reply-&gt;type &#061;&#061; REDIS_REPLY_STATUS &amp;&amp; strcmp(reply-&gt;str, &#034;OK&#034;) &#061;&#061; 0);<br \/>\n    freeReplyObject(reply);<br \/>\n    return acquired;<br \/>\n}<br \/>\n\/\/ \u91ca\u653e\u9501 (\u9700\u786e\u4fdd\u91ca\u653e\u7684\u662f\u81ea\u5df1\u6301\u6709\u7684\u9501&#xff0c;\u901a\u5e38\u7528 Lua \u811a\u672c\u4fdd\u8bc1\u539f\u5b50\u6027)<\/p>\n<p>http:\/\/my.tv.sohu.com\/us\/442092364\/698249292.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTI5Mi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249079.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTA3OS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249343.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTM0My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249502.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTUwMi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249356.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTM1Ni5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249358.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTM1OC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249516.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTUxNi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249365.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTM2NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249432.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTQzMi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249436.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTQzNi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249446.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTQ0Ni5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249388.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTM4OC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249541.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTU0MS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249544.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTU0NC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249638.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTYzOC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249704.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTcwNC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249647.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTY0Ny5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249708.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTcwOC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249712.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTcxMi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249484.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTQ4NC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249723.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTcyMy5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249574.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTU3NC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249577.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTU3Ny5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249580.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTU4MC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249784.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTc4NC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250017.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDAxNy5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250110.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDExMC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249878.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTg3OC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250043.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDA0My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250044.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDA0NC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250136.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDEzNi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250055.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDA1NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250143.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDE0My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250062.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDA2Mi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250067.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDA2Ny5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250204.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDIwNC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698249998.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI0OTk5OC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250081.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDA4MS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250160.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDE2MC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250091.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDA5MS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250321.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDMyMS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250100.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDEwMC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250182.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDE4Mi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250236.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDIzNi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250337.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDMzNy5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250243.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDI0My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250348.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDM0OC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250506.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDUwNi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250354.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDM1NC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442092364\/698250256.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkyMzY0LzY5ODI1MDI1Ni5zaHRtbA&#061;&#061;.html \u00a0<\/p>\n<p> \u6ce8\u610f&#xff1a;\u751f\u4ea7\u73af\u5883\u5206\u5e03\u5f0f\u9501\u9700\u8003\u8651\u66f4\u590d\u6742\u7684\u573a\u666f&#xff08;\u5982\u9501\u7eed\u671f\u3001\u5b89\u5168\u91ca\u653e&#xff09;&#xff0c;\u5efa\u8bae\u4f7f\u7528 Redlock \u7b97\u6cd5\u6216\u6210\u719f\u5e93\u3002<\/li>\n<\/ul>\n<h4>4. \u5178\u578b\u5e94\u7528\u573a\u666f<\/h4>\n<ul>\n<li>\u7528\u6237\u6807\u7b7e\/\u5174\u8da3: \u6bcf\u4e2a\u7528\u6237\u7684\u6807\u7b7e\u5b58\u50a8\u5728\u4e00\u4e2a SET \u4e2d\u3002\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6807\u7b7e\u6dfb\u52a0\u3001\u5220\u9664\u3001\u5224\u65ad\u7528\u6237\u662f\u5426\u6709\u67d0\u6807\u7b7e (SISMEMBER)&#xff0c;\u8ba1\u7b97\u5171\u540c\u5174\u8da3 (SINTER)\u3002<\/li>\n<li>\u793e\u4ea4\u5173\u7cfb: \u5b58\u50a8\u7528\u6237\u7684\u5173\u6ce8\u5217\u8868 (SET)\u3001\u7c89\u4e1d\u5217\u8868 (SET)\u3002\u8ba1\u7b97\u5171\u540c\u5173\u6ce8 (SINTER)&#xff0c;\u53ef\u80fd\u8ba4\u8bc6\u7684\u4eba (SDIFF\/SUNION \u7ed3\u5408)\u3002<\/li>\n<li>\u62bd\u5956\/\u552f\u4e00\u53c2\u4e0e: \u8bb0\u5f55\u5df2\u53c2\u4e0e\u6d3b\u52a8\u7684\u7528\u6237 ID (SET)&#xff0c;\u786e\u4fdd\u7528\u6237\u4e0d\u91cd\u590d\u53c2\u4e0e (SADD \u7684\u8fd4\u56de\u503c\u5224\u65ad)\u3002<\/li>\n<li>\u6570\u636e\u53bb\u91cd (\u521d\u7ea7): \u8bb0\u5f55\u5df2\u5904\u7406\u8fc7\u7684\u6570\u636e ID (SET)&#xff0c;\u907f\u514d\u91cd\u590d\u5904\u7406\u3002\u6ce8\u610f\u5185\u5b58\u6d88\u8017&#xff0c;\u8d85\u5927\u96c6\u5408\u9700\u8c28\u614e\u3002<\/li>\n<li>\u6743\u9650\/\u767d\u540d\u5355: \u5b58\u50a8\u62e5\u6709\u67d0\u9879\u6743\u9650\u7684\u7528\u6237\u6216 IP (SET)&#xff0c;\u5feb\u901f\u68c0\u67e5 (SISMEMBER)\u3002<\/li>\n<\/ul>\n<h4>5. \u6ce8\u610f\u4e8b\u9879<\/h4>\n<ul>\n<li>\u5185\u5b58\u6d88\u8017: SET \u91c7\u7528\u54c8\u5e0c\u8868\u6216\u6574\u6570\u96c6\u5408\u7f16\u7801\u3002\u6210\u5458\u8f83\u591a\u4e14\u4e0d\u662f\u6574\u6570\u65f6&#xff0c;\u5185\u5b58\u5360\u7528\u53ef\u80fd\u8f83\u5927\u3002\u9700\u76d1\u63a7 info memory\u3002<\/li>\n<li>\u5927 Key \u95ee\u9898: \u5305\u542b\u6570\u767e\u4e07\u6210\u5458\u7684 SET \u4f1a\u5f71\u54cd\u64cd\u4f5c\u6027\u80fd&#xff08;\u5982 SINTER\u3001SMEMBERS&#xff09;\u548c\u6301\u4e45\u5316\u6548\u7387\u3002\u8003\u8651\u62c6\u5206\u3001\u4f7f\u7528 SSCAN \u6216\u8fc1\u79fb\u5230\u66f4\u9002\u5408\u7684\u7ed3\u6784\u3002<\/li>\n<li>\u7f51\u7edc\u5f00\u9500: C&#043;&#043; \u5e94\u7528\u4e0e Redis \u7684\u901a\u4fe1\u5b58\u5728\u7f51\u7edc\u5ef6\u8fdf\u3002\u6279\u91cf\u64cd\u4f5c (Pipeline) \u548c\u5408\u7406\u7684\u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u81f3\u5173\u91cd\u8981\u3002<\/li>\n<li>\u5ba2\u6237\u7aef\u9009\u62e9: hiredis \u662f\u57fa\u7840&#xff0c;\u4e5f\u53ef\u8003\u8651\u66f4\u9ad8\u7ea7\u7684 C&#043;&#043; \u5c01\u88c5\u5e93&#xff08;\u5982 cpp_redis&#xff09;&#xff0c;\u5b83\u4eec\u53ef\u80fd\u63d0\u4f9b\u66f4\u53cb\u597d\u7684\u5f02\u6b65\u63a5\u53e3\u3001\u8fde\u63a5\u6c60\u7ba1\u7406\u3002<\/li>\n<li>\u7ebf\u7a0b\u5b89\u5168: redisContext \u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d&#xff0c;\u5e94\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u521b\u5efa\u72ec\u7acb\u8fde\u63a5\u6216\u4f7f\u7528\u8fde\u63a5\u6c60\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u7ed3\u5408 Redis SET \u7684\u5f3a\u5927\u529f\u80fd\u548c C&#043;&#043; \u7684\u9ad8\u6548\u63a7\u5236\u80fd\u529b&#xff0c;\u4f60\u53ef\u4ee5\u5728\u9700\u8981\u9ad8\u6027\u80fd\u96c6\u5408\u64cd\u4f5c\u7684\u573a\u666f\u4e2d\u6784\u5efa\u51fa\u5f3a\u5927\u7684\u89e3\u51b3\u65b9\u6848\u3002\u52a1\u5fc5\u6839\u636e\u5b9e\u9645\u4e1a\u52a1\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\u9009\u62e9\u5408\u9002\u7684\u64cd\u4f5c\u548c\u4f18\u5316\u7b56\u7565\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u597d\u7684&#xff0c;\u6211\u4eec\u6765\u6df1\u5165\u63a2\u8ba8 Redis \u4e2d\u7684 SET \u6570\u636e\u7c7b\u578b\u53ca\u5176\u5728 C \u4e2d\u7684\u5b9e\u6218\u5e94\u7528\u3002Redis \u7684 SET \u662f\u4e00\u4e2a\u65e0\u5e8f\u7684\u3001\u5143\u7d20\u552f\u4e00\u7684\u96c6\u5408&#xff0c;\u975e\u5e38\u9002\u5408\u5904\u7406\u9700\u8981\u53bb\u91cd\u3001\u6210\u5458\u5173\u7cfb\u5224\u65ad\u6216\u96c6\u5408\u8fd0\u7b97\u7684\u573a\u666f\u3002<br \/>\n1. Redis SET \u6838\u5fc3\u7279\u6027\u56de\u987e<br \/>\n\u65e0\u5e8f\u6027 (Unordered): \u5143\u7d20\u5b58\u50a8\u987a\u5e8f\u4e0d\u56fa\u5b9a\u3002\u552f\u4e00\u6027 (Unique): \u6bcf\u4e2a\u5143\u7d20\u5728\u96c6\u5408\u4e2d\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21\u3002\u9ad8\u6548\u64cd\u4f5c: \u652f\u6301\u6dfb\u52a0\u3001\u5220\u9664\u3001\u5224\u65ad\u6210\u5458\u5b58\u5728\u6027\u3001\u96c6\u5408\u8fd0\u7b97&#xff08;\u4ea4\u96c6\u3001\u5e76\u96c6\u3001\u5dee\u96c6&#xff09;\u7b49\u64cd\u4f5c<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[566,567,87],"topic":[],"class_list":["post-71089","post","type-post","status-publish","format-standard","hentry","category-server","tag-bootstrap","tag-html","tag-87"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Redis SET\u4e0eC++\u5b9e\u6218\uff1a\u9ad8\u6548\u96c6\u5408\u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/71089.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Redis SET\u4e0eC++\u5b9e\u6218\uff1a\u9ad8\u6548\u96c6\u5408\u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u597d\u7684&#xff0c;\u6211\u4eec\u6765\u6df1\u5165\u63a2\u8ba8 Redis \u4e2d\u7684 SET \u6570\u636e\u7c7b\u578b\u53ca\u5176\u5728 C \u4e2d\u7684\u5b9e\u6218\u5e94\u7528\u3002Redis \u7684 SET \u662f\u4e00\u4e2a\u65e0\u5e8f\u7684\u3001\u5143\u7d20\u552f\u4e00\u7684\u96c6\u5408&#xff0c;\u975e\u5e38\u9002\u5408\u5904\u7406\u9700\u8981\u53bb\u91cd\u3001\u6210\u5458\u5173\u7cfb\u5224\u65ad\u6216\u96c6\u5408\u8fd0\u7b97\u7684\u573a\u666f\u3002 1. Redis SET \u6838\u5fc3\u7279\u6027\u56de\u987e \u65e0\u5e8f\u6027 (Unordered): \u5143\u7d20\u5b58\u50a8\u987a\u5e8f\u4e0d\u56fa\u5b9a\u3002\u552f\u4e00\u6027 (Unique): \u6bcf\u4e2a\u5143\u7d20\u5728\u96c6\u5408\u4e2d\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21\u3002\u9ad8\u6548\u64cd\u4f5c: \u652f\u6301\u6dfb\u52a0\u3001\u5220\u9664\u3001\u5224\u65ad\u6210\u5458\u5b58\u5728\u6027\u3001\u96c6\u5408\u8fd0\u7b97&#xff08;\u4ea4\u96c6\u3001\u5e76\u96c6\u3001\u5dee\u96c6&#xff09;\u7b49\u64cd\u4f5c\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/71089.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-02T23:05:10+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/71089.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/71089.html\",\"name\":\"Redis SET\u4e0eC++\u5b9e\u6218\uff1a\u9ad8\u6548\u96c6\u5408\u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-02T23:05:10+00:00\",\"dateModified\":\"2026-02-02T23:05:10+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/71089.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/71089.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/71089.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Redis SET\u4e0eC++\u5b9e\u6218\uff1a\u9ad8\u6548\u96c6\u5408\u64cd\u4f5c\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Redis SET\u4e0eC++\u5b9e\u6218\uff1a\u9ad8\u6548\u96c6\u5408\u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/71089.html","og_locale":"zh_CN","og_type":"article","og_title":"Redis SET\u4e0eC++\u5b9e\u6218\uff1a\u9ad8\u6548\u96c6\u5408\u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u597d\u7684&#xff0c;\u6211\u4eec\u6765\u6df1\u5165\u63a2\u8ba8 Redis \u4e2d\u7684 SET \u6570\u636e\u7c7b\u578b\u53ca\u5176\u5728 C \u4e2d\u7684\u5b9e\u6218\u5e94\u7528\u3002Redis \u7684 SET \u662f\u4e00\u4e2a\u65e0\u5e8f\u7684\u3001\u5143\u7d20\u552f\u4e00\u7684\u96c6\u5408&#xff0c;\u975e\u5e38\u9002\u5408\u5904\u7406\u9700\u8981\u53bb\u91cd\u3001\u6210\u5458\u5173\u7cfb\u5224\u65ad\u6216\u96c6\u5408\u8fd0\u7b97\u7684\u573a\u666f\u3002 1. Redis SET \u6838\u5fc3\u7279\u6027\u56de\u987e \u65e0\u5e8f\u6027 (Unordered): \u5143\u7d20\u5b58\u50a8\u987a\u5e8f\u4e0d\u56fa\u5b9a\u3002\u552f\u4e00\u6027 (Unique): \u6bcf\u4e2a\u5143\u7d20\u5728\u96c6\u5408\u4e2d\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21\u3002\u9ad8\u6548\u64cd\u4f5c: \u652f\u6301\u6dfb\u52a0\u3001\u5220\u9664\u3001\u5224\u65ad\u6210\u5458\u5b58\u5728\u6027\u3001\u96c6\u5408\u8fd0\u7b97&#xff08;\u4ea4\u96c6\u3001\u5e76\u96c6\u3001\u5dee\u96c6&#xff09;\u7b49\u64cd\u4f5c","og_url":"https:\/\/www.wsisp.com\/helps\/71089.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-02T23:05:10+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"13 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/71089.html","url":"https:\/\/www.wsisp.com\/helps\/71089.html","name":"Redis SET\u4e0eC++\u5b9e\u6218\uff1a\u9ad8\u6548\u96c6\u5408\u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-02T23:05:10+00:00","dateModified":"2026-02-02T23:05:10+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/71089.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/71089.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/71089.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Redis SET\u4e0eC++\u5b9e\u6218\uff1a\u9ad8\u6548\u96c6\u5408\u64cd\u4f5c"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/71089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=71089"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/71089\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=71089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=71089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=71089"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=71089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}