{"id":201165,"date":"2025-05-10T10:05:44","date_gmt":"2025-05-10T02:05:44","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201165\/"},"modified":"2025-05-10T10:05:44","modified_gmt":"2025-05-10T02:05:44","slug":"redis-%e4%b8%ad%e7%9a%84%e5%b8%83%e9%9a%86%e8%bf%87%e6%bb%a4%e5%99%a8%e7%9a%84%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201165\/","title":{"rendered":"Redis \u4e2d\u7684\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u5b9e\u73b0"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis \u4e2d\u7684\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u5b9e\u73b0<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aRedis \u4e2d\u7684\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u5b9e\u73b0\u300b\uff0c\u4e3b\u8981\u5305\u62ecRedis\u5e03\u9686\u8fc7\u6ee4\u5668\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p><span style=\"color: #ff0000\"><strong>\u4ec0\u4e48\u662f\u300e\u5e03\u9686\u8fc7\u6ee4\u5668\u300f<\/strong><\/span><\/p>\n<p>\u5e03\u9686\u8fc7\u6ee4\u5668\u662f\u4e00\u4e2a\u795e\u5947\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5728\u4e00\u4e2a\u96c6\u5408\u4e2d\u3002\u5f88\u5e38\u7528\u7684\u4e00\u4e2a\u529f\u80fd\u662f\u7528\u6765\u53bb\u91cd\u3002\u5728\u722c\u866b\u4e2d\u5e38\u89c1\u7684\u4e00\u4e2a\u9700\u6c42\uff1a\u76ee\u6807\u7f51\u7ad9 URL \u5343\u5343\u4e07\uff0c\u600e\u4e48\u5224\u65ad\u67d0\u4e2a URL \u722c\u866b\u662f\u5426\u5ba0\u5e78\u8fc7\uff1f\u7b80\u5355\u70b9\u53ef\u4ee5\u722c\u866b\u6bcf\u91c7\u96c6\u8fc7\u4e00\u4e2a URL\uff0c\u5c31\u628a\u8fd9\u4e2a URL \u5b58\u5165\u6570\u636e\u5e93\u4e2d\uff0c\u6bcf\u6b21\u4e00\u4e2a\u65b0\u7684 URL \u8fc7\u6765\u5c31\u5230\u6570\u636e\u5e93\u67e5\u8be2\u4e0b\u662f\u5426\u8bbf\u95ee\u8fc7\u3002<\/p>\n<pre>\nselect id from table where url = 'https:\/\/jaychen.cc'<\/pre>\n<p>\u4f46\u662f\u968f\u7740\u722c\u866b\u722c\u8fc7\u7684 URL \u8d8a\u6765\u8d8a\u591a\uff0c\u6bcf\u6b21\u8bf7\u6c42\u524d\u90fd\u8981\u8bbf\u95ee\u6570\u636e\u5e93\u4e00\u6b21\uff0c\u5e76\u4e14\u5bf9\u4e8e\u8fd9\u79cd\u5b57\u7b26\u4e32\u7684 SQL \u67e5\u8be2\u6548\u7387\u5e76\u4e0d\u9ad8\u3002\u9664\u4e86\u6570\u636e\u5e93\u4e4b\u5916\uff0c\u4f7f\u7528 Redis \u7684 set \u7ed3\u6784\u4e5f\u53ef\u4ee5\u6ee1\u8db3\u8fd9\u4e2a\u9700\u6c42\uff0c\u5e76\u4e14\u6027\u80fd\u4f18\u4e8e\u6570\u636e\u5e93\u3002\u4f46\u662f Redis \u4e5f\u5b58\u5728\u4e00\u4e2a\u95ee\u9898\uff1a\u8017\u8d39\u8fc7\u591a\u7684\u5185\u5b58\u3002\u8fd9\u4e2a\u65f6\u5019\u5e03\u9686\u8fc7\u6ee4\u5668\u5c31\u5f88\u6a2a\u7684\u51fa\u573a\u4e86\uff1a\u8fd9\u4e2a\u95ee\u9898\u8ba9\u6211\u6765\u3002<\/p>\n<p>\u76f8\u6bd4\u4e8e\u6570\u636e\u5e93\u548c Redis\uff0c\u4f7f\u7528\u5e03\u9686\u8fc7\u6ee4\u5668\u53ef\u4ee5\u5f88\u597d\u7684\u907f\u514d\u6027\u80fd\u548c\u5185\u5b58\u5360\u7528\u7684\u95ee\u9898\u3002<\/p>\n<p>\u5e03\u9686\u8fc7\u6ee4\u5668\u672c\u8d28\u662f\u4e00\u4e2a\u4f4d\u6570\u7ec4\uff0c\u4f4d\u6570\u7ec4\u5c31\u662f\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u53ea\u5360\u7528 1 bit \u3002\u6bcf\u4e2a\u5143\u7d20\u53ea\u80fd\u662f 0 \u6216\u8005 1\u3002\u8fd9\u6837\u7533\u8bf7\u4e00\u4e2a 10000 \u4e2a\u5143\u7d20\u7684\u4f4d\u6570\u7ec4\u53ea\u5360\u7528 10000 \/ 8 = 1250 B \u7684\u7a7a\u95f4\u3002\u5e03\u9686\u8fc7\u6ee4\u5668\u9664\u4e86\u4e00\u4e2a\u4f4d\u6570\u7ec4\uff0c\u8fd8\u6709 K \u4e2a\u54c8\u5e0c\u51fd\u6570\u3002\u5f53\u4e00\u4e2a\u5143\u7d20\u52a0\u5165\u5e03\u9686\u8fc7\u6ee4\u5668\u4e2d\u7684\u65f6\u5019\uff0c\u4f1a\u8fdb\u884c\u5982\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 K \u4e2a\u54c8\u5e0c\u51fd\u6570\u5bf9\u5143\u7d20\u503c\u8fdb\u884c K \u6b21\u8ba1\u7b97\uff0c\u5f97\u5230 K \u4e2a\u54c8\u5e0c\u503c\u3002<\/li>\n<li>\u6839\u636e\u5f97\u5230\u7684\u54c8\u5e0c\u503c\uff0c\u5728\u4f4d\u6570\u7ec4\u4e2d\u628a\u5bf9\u5e94\u4e0b\u6807\u7684\u503c\u7f6e\u4e3a 1\u3002<\/li>\n<\/ul>\n<p>\u4e3e\u4e2a\uff0c\u5047\u8bbe\u5e03\u9686\u8fc7\u6ee4\u5668\u6709 3 \u4e2a\u54c8\u5e0c\u51fd\u6570\uff1af1, f2, f3 \u548c\u4e00\u4e2a\u4f4d\u6570\u7ec4 <code>arr<\/code>\u3002\u73b0\u5728\u8981\u628a <code>https:\/\/jaychen.cc<\/code> \u63d2\u5165\u5e03\u9686\u8fc7\u6ee4\u5668\u4e2d\uff1a<\/p>\n<ul>\n<li>\u5bf9\u503c\u8fdb\u884c\u4e09\u6b21\u54c8\u5e0c\u8ba1\u7b97\uff0c\u5f97\u5230\u4e09\u4e2a\u503c n1, n2, n3\u3002<\/li>\n<li>\u628a\u4f4d\u6570\u7ec4\u4e2d\u4e09\u4e2a\u5143\u7d20 arr[n1], arr[n2], arr[3] \u7f6e\u4e3a 1\u3002<\/li>\n<\/ul>\n<p>\u5f53\u8981\u5224\u65ad\u4e00\u4e2a\u503c\u662f\u5426\u5728\u5e03\u9686\u8fc7\u6ee4\u5668\u4e2d\uff0c\u5bf9\u5143\u7d20\u518d\u6b21\u8fdb\u884c\u54c8\u5e0c\u8ba1\u7b97\uff0c\u5f97\u5230\u503c\u4e4b\u540e\u5224\u65ad\u4f4d\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u90fd\u4e3a 1\uff0c\u5982\u679c\u503c\u90fd\u4e3a 1\uff0c\u90a3\u4e48\u8bf4\u660e\u8fd9\u4e2a\u503c\u5728\u5e03\u9686\u8fc7\u6ee4\u5668\u4e2d\uff0c\u5982\u679c\u5b58\u5728\u4e00\u4e2a\u503c\u4e0d\u4e3a 1\uff0c\u8bf4\u660e\u8be5\u5143\u7d20\u4e0d\u5728\u5e03\u9686\u8fc7\u6ee4\u5668\u4e2d\u3002<\/p>\n<blockquote><p>\n  \u770b\u4e0d\u61c2\u6587\u5b57\u770b\u4e0b\u9762\u7684\u7075\u9b42\u753b\u624b\u7684\u56fe\u89e3\u91ca\n<\/p><\/blockquote>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167240702663aee7f249456.png\" class=\"aligncenter\"><\/p>\n<p>\u770b\u4e86\u4e0a\u9762\u7684\u8bf4\u660e\uff0c\u5fc5\u7136\u4f1a\u63d0\u51fa\u4e00\u4e2a\u95ee\u9898\uff1a\u5f53\u63d2\u5165\u7684\u5143\u7d20\u539f\u6765\u8d8a\u591a\uff0c\u4f4d\u6570\u7ec4\u4e2d\u88ab\u7f6e\u4e3a 1 \u7684\u4f4d\u7f6e\u5c31\u8d8a\u591a\uff0c\u5f53\u4e00\u4e2a\u4e0d\u5728\u5e03\u9686\u8fc7\u6ee4\u5668\u4e2d\u7684\u5143\u7d20\uff0c\u7ecf\u8fc7\u54c8\u5e0c\u8ba1\u7b97\u4e4b\u540e\uff0c\u5f97\u5230\u7684\u503c\u5728\u4f4d\u6570\u7ec4\u4e2d\u67e5\u8be2\uff0c\u6709\u53ef\u80fd\u8fd9\u4e9b\u4f4d\u7f6e\u4e5f\u90fd\u88ab\u7f6e\u4e3a 1\u3002\u8fd9\u6837\u4e00\u4e2a\u4e0d\u5b58\u5728\u5e03\u9686\u8fc7\u6ee4\u5668\u4e2d\u7684\u4e5f\u6709\u53ef\u80fd\u88ab\u8bef\u5224\u6210\u5728\u5e03\u9686\u8fc7\u6ee4\u5668\u4e2d\u3002\u4f46\u662f\u5982\u679c\u5e03\u9686\u8fc7\u6ee4\u5668\u5224\u65ad\u8bf4\u4e00\u4e2a\u5143\u7d20\u4e0d\u5728\u5e03\u9686\u8fc7\u6ee4\u5668\u4e2d\uff0c\u90a3\u4e48\u8fd9\u4e2a\u503c\u5c31\u4e00\u5b9a\u4e0d\u5728\u5e03\u9686\u8fc7\u6ee4\u5668\u4e2d\u3002\u7b80\u5355\u6765\u8bf4\uff1a<\/p>\n<ul>\n<li>\u5e03\u9686\u8fc7\u6ee4\u5668\u8bf4\u67d0\u4e2a\u5143\u7d20\u5728\uff0c\u53ef\u80fd\u4f1a\u88ab\u8bef\u5224\u3002<\/li>\n<li>\u5e03\u9686\u8fc7\u6ee4\u5668\u8bf4\u67d0\u4e2a\u5143\u7d20\u4e0d\u5728\uff0c\u90a3\u4e48\u4e00\u5b9a\u4e0d\u5728\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e2a\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u7f3a\u9677\u653e\u5230\u4e0a\u9762\u722c\u866b\u7684\u9700\u6c42\u4e2d\uff0c\u53ef\u80fd\u5b58\u5728\u67d0\u4e9b\u6ca1\u6709\u8bbf\u95ee\u8fc7\u7684 URL \u53ef\u80fd\u4f1a\u88ab\u8bef\u5224\u4e3a\u8bbf\u95ee\u8fc7\uff0c\u4f46\u662f\u5982\u679c\u662f\u8bbf\u95ee\u8fc7\u7684 URL \u4e00\u5b9a\u4e0d\u4f1a\u88ab\u8bef\u5224\u4e3a\u6ca1\u8bbf\u95ee\u8fc7\u3002<\/p>\n<p><span style=\"color: #ff0000\"><strong>Redis \u4e2d\u7684\u5e03\u9686\u8fc7\u6ee4\u5668<\/strong><\/span><\/p>\n<p>redis \u5728 4.0 \u7684\u7248\u672c\u4e2d\u52a0\u5165\u4e86 module \u529f\u80fd\uff0c\u5e03\u9686\u8fc7\u6ee4\u5668\u53ef\u4ee5\u901a\u8fc7 module \u7684\u5f62\u5f0f\u6dfb\u52a0\u5230 redis \u4e2d\uff0c\u6240\u4ee5\u4f7f\u7528 redis 4.0 \u4ee5\u4e0a\u7684\u7248\u672c\u53ef\u4ee5\u901a\u8fc7\u52a0\u8f7d \u6765\u4f7f\u7528 redis \u4e2d\u7684\u5e03\u9686\u8fc7\u6ee4\u5668\u3002\u4f46\u662f\u8fd9\u4e0d\u662f\u6700\u7b80\u5355\u7684\u65b9\u5f0f\uff0c\u4f7f\u7528 docker \u53ef\u4ee5\u76f4\u63a5\u5728 redis \u4e2d\u4f53\u9a8c\u5e03\u9686\u8fc7\u6ee4\u5668\u3002<\/p>\n<pre>\n&gt; docker run -d -p 6379:6379 --name bloomfilter redislabs\/rebloom\n&gt; docker exec -it bloomfilter redis-cli<\/pre>\n<p>redis \u5e03\u9686\u8fc7\u6ee4\u5668\u4e3b\u8981\u5c31\u4e24\u4e2a\u547d\u4ee4\uff1a<\/p>\n<ul>\n<li><code>bf.add<\/code> \u6dfb\u52a0\u5143\u7d20\u5230\u5e03\u9686\u8fc7\u6ee4\u5668\u4e2d\uff1a<code>bf.add urls https:\/\/jaychen.cc<\/code>\u3002<\/li>\n<li><code>bf.exists<\/code> \u5224\u65ad\u67d0\u4e2a\u5143\u7d20\u662f\u5426\u5728\u8fc7\u6ee4\u5668\u4e2d\uff1a<code>bf.exists urls https:\/\/jaychen.cc<\/code>\u3002<\/li>\n<\/ul>\n<p>\u4e0a\u9762\u8bf4\u8fc7\u5e03\u9686\u8fc7\u6ee4\u5668\u5b58\u5728\u8bef\u5224\u7684\u60c5\u51b5\uff0c\u5728 redis \u4e2d\u6709\u4e24\u4e2a\u503c\u51b3\u5b9a\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u51c6\u786e\u7387\uff1a<\/p>\n<ul>\n<li><code>error_rate <\/code>\uff1a\u5141\u8bb8\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u9519\u8bef\u7387\uff0c\u8fd9\u4e2a\u503c\u8d8a\u4f4e\u8fc7\u6ee4\u5668\u7684\u4f4d\u6570\u7ec4\u7684\u5927\u5c0f\u8d8a\u5927\uff0c\u5360\u7528\u7a7a\u95f4\u4e5f\u5c31\u8d8a\u5927\u3002<\/li>\n<li><code>initial_size <\/code>\uff1a\u5e03\u9686\u8fc7\u6ee4\u5668\u53ef\u4ee5\u50a8\u5b58\u7684\u5143\u7d20\u4e2a\u6570\uff0c\u5f53\u5b9e\u9645\u5b58\u50a8\u7684\u5143\u7d20\u4e2a\u6570\u8d85\u8fc7\u8fd9\u4e2a\u503c\u4e4b\u540e\uff0c\u8fc7\u6ee4\u5668\u7684\u51c6\u786e\u7387\u4f1a\u4e0b\u964d\u3002<\/li>\n<\/ul>\n<p>redis \u4e2d\u6709\u4e00\u4e2a\u547d\u4ee4\u53ef\u4ee5\u6765\u8bbe\u7f6e\u8fd9\u4e24\u4e2a\u503c\uff1a<\/p>\n<pre>\nbf.reserve urls 0.01 100<\/pre>\n<p>\u4e09\u4e2a\u53c2\u6570\u7684\u542b\u4e49\uff1a<\/p>\n<ul>\n<li>\u7b2c\u4e00\u4e2a\u503c\u662f\u8fc7\u6ee4\u5668\u7684\u540d\u5b57\u3002<\/li>\n<li>\u7b2c\u4e8c\u4e2a\u503c\u4e3a <code>error_rate<\/code> \u7684\u503c\u3002<\/li>\n<li>\u7b2c\u4e09\u4e2a\u503c\u4e3a <code>initial_size<\/code> \u7684\u503c\u3002<\/li>\n<\/ul>\n<p>\u4f7f\u7528\u8fd9\u4e2a\u547d\u4ee4\u8981\u6ce8\u610f\u4e00\u70b9\uff1a\u6267\u884c\u8fd9\u4e2a\u547d\u4ee4\u4e4b\u524d\u8fc7\u6ee4\u5668\u7684\u540d\u5b57\u5e94\u8be5\u4e0d\u5b58\u5728\uff0c\u5982\u679c\u6267\u884c\u4e4b\u524d\u5c31\u5b58\u5728\u4f1a\u62a5\u9519\uff1a<code>(error) ERR item exists <\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis \u4e2d\u7684\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u5b9e\u73b0&#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-201165","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201165","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=201165"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201165\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}