{"id":169003,"date":"2024-11-06T18:04:20","date_gmt":"2024-11-06T10:04:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/169003\/"},"modified":"2024-11-06T18:04:21","modified_gmt":"2024-11-06T10:04:21","slug":"%e9%9d%a2%e8%a9%a6%e6%ae%ba%e6%89%8b%e9%90%a7%ef%bc%9aredis%e6%ba%90%e7%a2%bc%e4%b9%8bbitmap","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/169003\/","title":{"rendered":"\u9762\u8a66\u6bba\u624b\u9427\uff1aRedis\u6e90\u78bc\u4e4bBitMap"},"content":{"rendered":"<h1 id=\"%e9%9d%a2%e8%a9%a6%e6%ae%ba%e6%89%8b%e9%90%a7%ef%bc%9aredis%e6%ba%90%e7%a2%bc%e4%b9%8bbitmap-KiFltJimbW\">\u9762\u8a66\u6bba\u624b\u9427\uff1aRedis\u6e90\u78bc\u4e4bBitMap<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6280\u8853\u9762\u8a66\u4e2d\uff0c\u719f\u6089\u8cc7\u6599\u7d50\u69cb\u548c\u6f14\u7b97\u6cd5\u662f\u6c42\u8077\u8005\u5fc5\u5099\u7684\u6280\u80fd\u4e4b\u4e00\u3002Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5176\u5167\u90e8\u5be6\u73fe\u7684\u8cc7\u6599\u7d50\u69cb\u66f4\u662f\u9762\u8a66\u4e2d\u7684\u71b1\u9580\u8a71\u984c\u4e4b\u4e00\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eRedis\u4e2d\u7684BitMap\u8cc7\u6599\u7d50\u69cb\uff0c\u4e26\u5206\u6790\u5176\u6e90\u78bc\u5be6\u73fe\uff0c\u5e6b\u52a9\u8b80\u8005\u5728\u9762\u8a66\u4e2d\u812b\u7a4e\u800c\u51fa\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afbitmap%ef%bc%9f-KiFltJimbW\">\u4ec0\u9ebc\u662fBitMap\uff1f<\/h2>\n<p>BitMap\u662f\u4e00\u7a2e\u4ee5\u4f4d\u5143\uff08bit\uff09\u70ba\u55ae\u4f4d\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u901a\u5e38\u7528\u65bc\u9ad8\u6548\u5730\u5b58\u5132\u548c\u64cd\u4f5c\u5927\u91cf\u7684\u5e03\u6797\u503c\u3002\u6bcf\u4e00\u500b\u4f4d\u5143\u53ef\u4ee5\u8868\u793a\u4e00\u500b\u72c0\u614b\uff08\u59820\u62161\uff09\uff0c\u56e0\u6b64BitMap\u5728\u9700\u8981\u5927\u91cf\u5e03\u6797\u503c\u7684\u5834\u666f\u4e2d\uff0c\u80fd\u5920\u7bc0\u7701\u5927\u91cf\u7684\u7a7a\u9593\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u8868\u793a1000\u500b\u5e03\u6797\u503c\uff0c\u4f7f\u7528BitMap\u53ea\u97001000\u4f4d\u5143\uff0c\u800c\u4f7f\u7528\u9663\u5217\u5247\u9700\u89811000\u500b\u4f4d\u5143\u7d44\uff0c\u9019\u6a23\u53ef\u4ee5\u7bc0\u7701\u7d0487.5%\u7684\u7a7a\u9593\u3002<\/p>\n<h2 id=\"redis%e4%b8%ad%e7%9a%84bitmap%e5%af%a6%e7%8f%be-KiFltJimbW\">Redis\u4e2d\u7684BitMap\u5be6\u73fe<\/h2>\n<p>\u5728Redis\u4e2d\uff0cBitMap\u662f\u57fa\u65bc\u5b57\u4e32\uff08string\uff09\u8cc7\u6599\u985e\u578b\u5be6\u73fe\u7684\u3002Redis\u7684\u5b57\u4e32\u5be6\u969b\u4e0a\u662f\u4e8c\u9032\u4f4d\u5b89\u5168\u7684\uff0c\u9019\u610f\u5473\u8457\u5b83\u53ef\u4ee5\u5b58\u5132\u4efb\u4f55\u985e\u578b\u7684\u8cc7\u6599\uff0c\u5305\u62ec\u4f4d\u5143\u7d44\u3002BitMap\u7684\u64cd\u4f5c\u4e3b\u8981\u4f9d\u8cf4\u65bc\u5b57\u4e32\u7684\u4f4d\u5143\u64cd\u4f5c\uff0c\u9019\u4f7f\u5f97Redis\u80fd\u5920\u9ad8\u6548\u5730\u9032\u884cBitMap\u7684\u8b80\u53d6\u548c\u5beb\u5165\u3002<\/p>\n<h3 id=\"bitmap%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c-KiFltJimbW\">BitMap\u7684\u57fa\u672c\u64cd\u4f5c<\/h3>\n<p>Redis\u63d0\u4f9b\u4e86\u5e7e\u500b\u57fa\u672c\u7684BitMap\u64cd\u4f5c\u547d\u4ee4\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><code>SETBIT key offset value<\/code>\uff1a\u8a2d\u7f6e\u6307\u5b9a\u9375\u7684\u4f4d\u5143\u503c\u3002<\/li>\n<li><code>GETBIT key offset<\/code>\uff1a\u7372\u53d6\u6307\u5b9a\u9375\u7684\u4f4d\u5143\u503c\u3002<\/li>\n<li><code>BITCOUNT key<\/code>\uff1a\u8a08\u7b97\u6307\u5b9a\u9375\u4e2d\u4f4d\u5143\u503c\u70ba1\u7684\u500b\u6578\u3002<\/li>\n<li><code>BITOP operation destkey key1 [key2 ...]<\/code>\uff1a\u5c0d\u591a\u500b\u9375\u9032\u884c\u4f4d\u5143\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<h3 id=\"%e6%ba%90%e7%a2%bc%e5%88%86%e6%9e%90-KiFltJimbW\">\u6e90\u78bc\u5206\u6790<\/h3>\n<p>\u5728Redis\u7684\u6e90\u78bc\u4e2d\uff0cBitMap\u7684\u5be6\u73fe\u4e3b\u8981\u96c6\u4e2d\u5728\u5b57\u4e32\u7684\u64cd\u4f5c\u4e0a\u3002\u4ee5\u4e0b\u662f<code>SETBIT<\/code>\u547d\u4ee4\u7684\u7c21\u5316\u5be6\u73fe\uff1a<\/p>\n<pre><code>\nvoid setbitCommand(client *c) {\n    robj *o = lookupKeyRead(c-&gt;db,c-&gt;argv[1]);\n    long offset;\n    if (getLongFromObject(c-&gt;argv[2],&amp;offset) != C_OK) {\n        addReplyError(c,\"value is not an integer or out of range\");\n        return;\n    }\n    \/\/ \u9032\u884c\u4f4d\u5143\u8a2d\u7f6e\n    if (o == NULL) {\n        \/\/ \u5982\u679c\u9375\u4e0d\u5b58\u5728\uff0c\u5275\u5efa\u4e00\u500b\u65b0\u7684\u5b57\u4e32\n        o = createStringObject(\"\", 1);\n        dbAdd(c-&gt;db,c-&gt;argv[1],o);\n    }\n    \/\/ \u8a2d\u7f6e\u4f4d\u5143\n    setBit(o-&gt;ptr, offset, (c-&gt;argv[3]-&gt;ptr[0] == '1') ? 1 : 0);\n    addReplyLongLong(c, 0);\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u9996\u5148\u6aa2\u67e5\u6307\u5b9a\u7684\u9375\u662f\u5426\u5b58\u5728\u3002\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5247\u5275\u5efa\u4e00\u500b\u65b0\u7684\u5b57\u4e32\u7269\u4ef6\u3002\u63a5\u8457\uff0c\u901a\u904e<code>setBit<\/code>\u51fd\u6578\u8a2d\u7f6e\u6307\u5b9a\u504f\u79fb\u91cf\u7684\u4f4d\u5143\u503c\u3002\u9019\u6a23\u7684\u8a2d\u8a08\u4f7f\u5f97Redis\u80fd\u5920\u5728O(1)\u7684\u6642\u9593\u5167\u5b8c\u6210\u4f4d\u5143\u7684\u8a2d\u7f6e\u548c\u7372\u53d6\uff0c\u6975\u5927\u5730\u63d0\u9ad8\u4e86\u6548\u7387\u3002<\/p>\n<h2 id=\"bitmap%e7%9a%84%e6%87%89%e7%94%a8%e5%a0%b4%e6%99%af-KiFltJimbW\">BitMap\u7684\u61c9\u7528\u5834\u666f<\/h2>\n<p>BitMap\u5728\u8a31\u591a\u5834\u666f\u4e2d\u90fd\u80fd\u767c\u63ee\u5176\u512a\u52e2\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u7528\u6236\u884c\u70ba\u8ffd\u8e64\uff1a\u53ef\u4ee5\u7528\u4f86\u8a18\u9304\u7528\u6236\u7684\u767b\u9304\u72c0\u614b\u3002<\/li>\n<li>\u6578\u64da\u53bb\u91cd\uff1a\u901a\u904eBitMap\u53ef\u4ee5\u5feb\u901f\u5224\u65b7\u67d0\u500b\u5143\u7d20\u662f\u5426\u5b58\u5728\u3002<\/li>\n<li>\u7d71\u8a08\u5206\u6790\uff1a\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u67d0\u4e9b\u4e8b\u4ef6\u7684\u767c\u751f\u6b21\u6578\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-KiFltJimbW\">\u7e3d\u7d50<\/h2>\n<p>Redis\u4e2d\u7684BitMap\u8cc7\u6599\u7d50\u69cb\u4ee5\u5176\u9ad8\u6548\u7684\u7a7a\u9593\u5229\u7528\u548c\u5feb\u901f\u7684\u64cd\u4f5c\u6027\u80fd\uff0c\u6210\u70ba\u4e86\u8a31\u591a\u61c9\u7528\u5834\u666f\u4e2d\u7684\u7406\u60f3\u9078\u64c7\u3002\u4e86\u89e3\u5176\u6e90\u78bc\u5be6\u73fe\u4e0d\u50c5\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u5728\u9762\u8a66\u4e2d\u5c55\u793a\u81ea\u5df1\u7684\u6280\u8853\u80fd\u529b\uff0c\u9084\u80fd\u5728\u5be6\u969b\u958b\u767c\u4e2d\u66f4\u597d\u5730\u5229\u7528Redis\u7684\u7279\u6027\u3002\u5982\u679c\u60a8\u5c0d\u65bc<code>VPS<\/code>\u3001<code>\u9999\u6e2fVPS<\/code>\u6216\u5176\u4ed6<code>\u4f3a\u670d\u5668<\/code>\u89e3\u6c7a\u65b9\u6848\u611f\u8208\u8da3\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9 <a href=\"https:\/\/server.hk\">Server.HK<\/a> \u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Redis\u6e90\u78bc\u4e2d\u7684BitMap\uff0c\u63ed\u793a\u5176\u9ad8\u6548\u5b58\u5132\u8207\u64cd\u4f5c\u6280\u5de7\uff0c\u52a9\u4f60\u5728\u9762\u8a66\u4e2d\u812b\u7a4e\u800c\u51fa\uff0c\u638c\u63e1\u95dc\u9375\u6280\u8853\uff01<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-169003","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/169003","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=169003"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/169003\/revisions"}],"predecessor-version":[{"id":169004,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/169003\/revisions\/169004"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=169003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=169003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=169003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}