{"id":201614,"date":"2025-05-10T09:32:07","date_gmt":"2025-05-10T01:32:07","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201614\/"},"modified":"2025-05-10T09:32:07","modified_gmt":"2025-05-10T01:32:07","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:\/\/server.hk\/cnblog\/201614\/","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 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","protected":false},"excerpt":{"rendered":"<p>Redis\u4e2dBloomfilte&#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-201614","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201614","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=201614"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201614\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}