{"id":44516,"date":"2024-12-01T15:52:25","date_gmt":"2024-12-01T07:52:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/44516\/"},"modified":"2024-12-01T15:52:25","modified_gmt":"2024-12-01T07:52:25","slug":"redis%e4%b8%adbloomfilter%e5%b8%83%e9%9a%86%e8%bf%87%e6%bb%a4%e5%99%a8%e7%9a%84%e5%ad%a6%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/44516\/","title":{"rendered":"Redis\u4e2dBloomfilter\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u5b66\u4e60"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u4e2dBloomfilter\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u5b66\u4e60<\/h1>\n<p><span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728\u6570\u636e\u5e93\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929golang\u5b66\u4e60\u7f51\u5c31\u6574\u7406\u5206\u4eab\u300aRedis\u4e2dBloomfilter\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u5b66\u4e60\u300b\uff0c\u804a\u804a\u5e03\u9686\u8fc7\u6ee4\u5668\u3001RedisBloom\u3001filter\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<h2>1.\u6982\u5ff5<\/h2>\n<p>\u200b \u5e03\u9686\u8fc7\u6ee4\u5668\u662f\u4e00\u4e2a\u9ad8\u7a7a\u95f4\u5229\u7528\u7387\u7684\u6982\u7387\u6027\u6570\u636e\u7ed3\u6784\uff0c\u4e3b\u8981\u76ee\u7684\u662f\u8282\u7701\u5185\u5b58\u7a7a\u95f4\u4ee5\u53ca\u5224\u65ad\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u4e00\u4e2a\u96c6\u5408\u4e2d\uff08\u5b58\u5728\u8bef\u5224\u7684\u60c5\u51b5\uff09\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u4e2a\u4e0d\u600e\u4e48\u7cbe\u786e\u7684 set \u7ed3\u6784\uff0c\u5f53\u4f60\u4f7f\u7528\u5b83\u7684 contains \u65b9\u6cd5\u5224\u65ad\u67d0\u4e2a\u5bf9\u8c61\u662f\u5426\u5b58\u5728\u65f6\uff0c\u5b83\u53ef\u80fd\u4f1a\u8bef\u5224\u3002\u4f46\u662f\u5e03\u9686\u8fc7\u6ee4\u5668\u4e5f\u4e0d\u662f\u7279\u522b\u4e0d\u7cbe\u786e\uff0c\u53ea\u8981\u53c2\u6570\u8bbe\u7f6e\u7684\u5408\u7406\uff0c\u5b83\u7684\u7cbe\u786e\u5ea6\u53ef\u4ee5\u63a7\u5236\u7684\u76f8\u5bf9\u8db3\u591f\u7cbe\u786e\uff0c\u53ea\u4f1a\u6709\u5c0f\u5c0f\u7684\u8bef\u5224\u6982\u7387\uff08\u63a7\u5236\u53c2\u6570\uff1aerror_rate-\u8bef\u5224\u7387 initial_size-\u521d\u59cb\u5bb9\u91cf\uff09<\/p>\n<p>\u200b error_rate\u8d8a\u5c0f\uff0c\u8d8a\u7cbe\u786e,\u9700\u8981\u7684\u7a7a\u95f4\u8d8a\u5927<\/p>\n<p>\u200b initial_size\u8d8a\u5927\uff0c\u8d8a\u7cbe\u786e,\u5f53\u5b9e\u9645\u6570\u91cf\u8d85\u51fa\u8fd9\u4e2a\u6570\u503c\u65f6\uff0c\u8bef\u5224\u7387\u4f1a\u4e0a\u5347<\/p>\n<p>\u5e03\u9686\u8fc7\u6ee4\u5668\u53ef\u4ee5\u5224\u65ad\u67d0\u4e2a\u6570\u636e\u4e00\u5b9a\u4e0d\u5b58\u5728\uff0c\u4f46\u662f\u65e0\u6cd5\u5224\u65ad\u4e00\u5b9a\u5b58\u5728<\/p>\n<h2>2.guava\u5b9e\u73b0<\/h2>\n<h3>2.1.\u4f9d\u8d56<\/h3>\n<pre>\n   \n\n   \n    \n     com.google.guava\n    \n    \n     guava\n    \n    \n     19.0\n    \n   <\/pre>\n<h3>2.2.\u521d\u59cb\u5316\u5e03\u9686\u8fc7\u6ee4\u5668<\/h3>\n<pre>\/\/\u521d\u59cb\u5316\u5e03\u9686\u8fc7\u6ee4\u5668\uff0c\u653e\u5165\u5230spring\u5bb9\u5668\u91cc\u9762\n@Bean\npublic MyBloomFilter\n   \n     initBloomFilterHelper() {\n    return new MyBloomFilter((Funnel\n    \n     ) (from, into) -&gt; into.putString(from, Charsets.UTF_8).putString(from, Charsets.UTF_8) , 1000000, 0.01); }\n    \n   <\/pre>\n<h3>2.3.\u5e03\u9686\u8fc7\u6ee4\u5668<\/h3>\n<pre>package com.qin.redis.bloomfilter;\nimport com.google.common.base.Preconditions;\nimport com.google.common.hash.Funnel;\nimport com.google.common.hash.Hashing;\n\/**\n * @version: V1.0.0\n * @className: MyBloomFilter\n *\/\npublic class MyBloomFilter\n   \n     {\n    private int numHashFunctions;\n    private int bitSize;\n    private Funnel\n    \n      funnel; public MyBloomFilter(Funnel\n     \n       funnel, int expectedInsertions, double fpp) { Preconditions.checkArgument(funnel != null, \"funnel\u4e0d\u80fd\u4e3a\u7a7a\"); this.funnel = funnel; \/\/ \u8ba1\u7b97bit\u6570\u7ec4\u957f\u5ea6 bitSize = optimalNumOfBits(expectedInsertions, fpp); \/\/ \u8ba1\u7b97hash\u65b9\u6cd5\u6267\u884c\u6b21\u6570 numHashFunctions = optimalNumOfHashFunctions(expectedInsertions, bitSize); } public int[] murmurHashOffset(T value) { int[] offset = new int[numHashFunctions]; long hash64 = Hashing.murmur3_128().hashObject(value, funnel).asLong(); int hash1 = (int) hash64; int hash2 = (int) (hash64 &gt;&gt;&gt; 32); for (int i = 1; i \n      <h3>2.4.\u6dfb\u52a0\u5143\u7d20\u6216\u8005\u5224\u65ad\u662f\u5426\u5b58\u5728<\/h3> \n      <pre>package com.qin.redis.bloomfilter.service;\nimport com.google.common.base.Preconditions;\nimport com.hikvison.aksk.redis.bloomfilter.MyBloomFilter;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.data.redis.core.RedisTemplate;\nimport org.springframework.stereotype.Service;\n\/**\n * @version: V1.0.0\n * @className: RedisBloomFilterService\n *\/\n@Service\npublic class RedisBloomFilterService {\n    @Autowired\n    private RedisTemplate redisTemplate;\n    \/**\n     * \u6839\u636e\u7ed9\u5b9a\u7684\u5e03\u9686\u8fc7\u6ee4\u5668\u6dfb\u52a0\u503c\n     *\/\n    public \n       \n         void addByBloomFilter(MyBloomFilter\n        \n          bloomFilterHelper, String key, T value) { Preconditions.checkArgument(bloomFilterHelper != null, \"myBloomFilter\u4e0d\u80fd\u4e3a\u7a7a\"); int[] offset = bloomFilterHelper.murmurHashOffset(value); for (int i : offset) { System.out.println(\"key : \" + key + \" \" + \"value : \" + i); redisTemplate.opsForValue().setBit(key, i, true); } } \/** * \u6839\u636e\u7ed9\u5b9a\u7684\u5e03\u9686\u8fc7\u6ee4\u5668\u5224\u65ad\u503c\u662f\u5426\u5b58\u5728 *\/ public \n         \n           boolean includeByBloomFilter(MyBloomFilter\n          \n            bloomFilterHelper, String key, T value) { Preconditions.checkArgument(bloomFilterHelper != null, \"myBloomFilter\u4e0d\u80fd\u4e3a\u7a7a\"); int[] offset = bloomFilterHelper.murmurHashOffset(value); for (int i : offset) { System.out.println(\"key : \" + key + \" \" + \"value : \" + i); if (!redisTemplate.opsForValue().getBit(key, i)) { return false; } } return true; } }\n          \n         \n        \n       <\/pre>\n<h2>3.Redisson\u5b9e\u73b0<\/h2>\n<h3>3.1.\u4f9d\u8d56<\/h3>\n<pre>\n       \n        \n         org.redisson\n        \n        \n         redisson\n        \n        \n         2.7.0\n        \n       <\/pre>\n<h3>3.2.\u6ce8\u5165\u6216\u6d4b\u8bd5<\/h3>\n<pre> \/\/\u5355\u673a\u6a21\u5f0f\uff1a\u53ef\u4ee5\u8bbe\u7f6e\u96c6\u7fa4\u3001\u54e8\u5175\u6a21\u5f0f\n    @Bean\n    public Redisson redisson() {\n        Config config = new Config();\n        config.useSingleServer().setAddress(\"redis:\/\/127.0.0.1:6379\");\n        RedissonClient redissonClient = Redisson.create(config);\n        \/\/\u521d\u59cb\u5316\u8fc7\u6ee4\u5668\n        RBloomFilter bloomFilter = redissonClient.getBloomFilter(\"testBloomFilter\");\n        bloomFilter.tryInit(1000000L,0.05);\n        \/\/\u63d2\u5165\u5143\u7d20\n        bloomFilter.add(\"zhangsan\");\n        bloomFilter.add(\"lisi\");\n        \/\/\u5224\u65ad\u5143\u7d20\u662f\u5426\u5b58\u5728\n        boolean flag = bloomFilter.contains(\"lisi\");\n        return (Redisson) redissonClient;\n    }\n<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aRedis\u4e2dBloomfilter\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u5b66\u4e60\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8eredis\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<p>        \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1a\u811a\u672c\u4e4b\u5bb6 \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664  <\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5fae\u670d\u52a1SpringBoot\u6574\u5408Redis\u5b9e\u73b0\u597d\u53cb\u5173\u6ce8\u529f\u80fd\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u89e3\u51b3Redis\u6570\u636e\u503e\u659c\u3001\u70ed\u70b9\u7b49\u95ee\u9898\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u4e2dBloomfilter\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u5b66\u4e60 0\u6d4f\u89c8 \u6536\u85cf \u5728\u6570\u636e\u5e93\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929golang\u5b66\u4e60\u7f51\u5c31\u6574\u7406\u5206\u4eab\u300aRedis\u4e2dBloomfilter\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u5b66\u4e60\u300b\uff0c\u804a\u804a\u5e03\u9686\u8fc7\u6ee4\u5668\u3001RedisBloom\u3001filter\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002 1.\u6982\u5ff5 \u200b \u5e03\u9686\u8fc7\u6ee4\u5668\u662f\u4e00\u4e2a\u9ad8\u7a7a\u95f4\u5229\u7528\u7387\u7684\u6982\u7387\u6027\u6570\u636e\u7ed3\u6784\uff0c\u4e3b\u8981\u76ee\u7684\u662f\u8282\u7701\u5185\u5b58\u7a7a\u95f4\u4ee5\u53ca\u5224\u65ad\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u4e00\u4e2a\u96c6\u5408\u4e2d\uff08\u5b58\u5728\u8bef\u5224\u7684\u60c5\u51b5\uff09\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u4e2a\u4e0d\u600e\u4e48\u7cbe\u786e\u7684 set \u7ed3\u6784\uff0c\u5f53\u4f60\u4f7f\u7528\u5b83\u7684 contains \u65b9\u6cd5\u5224\u65ad\u67d0\u4e2a\u5bf9\u8c61\u662f\u5426\u5b58\u5728\u65f6\uff0c\u5b83\u53ef\u80fd\u4f1a\u8bef\u5224\u3002\u4f46\u662f\u5e03\u9686\u8fc7\u6ee4\u5668\u4e5f\u4e0d\u662f\u7279\u522b\u4e0d\u7cbe\u786e\uff0c\u53ea\u8981\u53c2\u6570\u8bbe\u7f6e\u7684\u5408\u7406\uff0c\u5b83\u7684\u7cbe\u786e\u5ea6\u53ef\u4ee5\u63a7\u5236\u7684\u76f8\u5bf9\u8db3\u591f\u7cbe\u786e\uff0c\u53ea\u4f1a\u6709\u5c0f\u5c0f\u7684\u8bef\u5224\u6982\u7387\uff08\u63a7\u5236\u53c2\u6570\uff1aerror_rate-\u8bef\u5224\u7387 initial_size-\u521d\u59cb\u5bb9\u91cf\uff09 \u200b error_rate\u8d8a\u5c0f\uff0c\u8d8a\u7cbe\u786e,\u9700\u8981\u7684\u7a7a\u95f4\u8d8a\u5927 \u200b initial_size\u8d8a\u5927\uff0c\u8d8a\u7cbe\u786e,\u5f53\u5b9e\u9645\u6570\u91cf\u8d85\u51fa\u8fd9\u4e2a\u6570\u503c\u65f6\uff0c\u8bef\u5224\u7387\u4f1a\u4e0a\u5347 \u5e03\u9686\u8fc7\u6ee4\u5668\u53ef\u4ee5\u5224\u65ad\u67d0\u4e2a\u6570\u636e\u4e00\u5b9a\u4e0d\u5b58\u5728\uff0c\u4f46\u662f\u65e0\u6cd5\u5224\u65ad\u4e00\u5b9a\u5b58\u5728 2.guava\u5b9e\u73b0 2.1.\u4f9d\u8d56 com.google.guava guava 19.0 2.2.\u521d\u59cb\u5316\u5e03\u9686\u8fc7\u6ee4\u5668 \/\/\u521d\u59cb\u5316\u5e03\u9686\u8fc7\u6ee4\u5668\uff0c\u653e\u5165\u5230spring\u5bb9\u5668\u91cc\u9762 @Bean public MyBloomFilter initBloomFilterHelper() { return new MyBloomFilter((Funnel ) (from, into) -&gt; into.putString(from, Charsets.UTF_8).putString(from, Charsets.UTF_8) , 1000000, 0.01); } 2.3.\u5e03\u9686\u8fc7\u6ee4\u5668 package com.qin.redis.bloomfilter; import com.google.common.base.Preconditions; import com.google.common.hash.Funnel; import com.google.common.hash.Hashing; \/** * @version: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-44516","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=44516"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44516\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=44516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=44516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=44516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}