{"id":2145,"date":"2023-04-02T13:19:53","date_gmt":"2023-04-02T05:19:53","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2145"},"modified":"2023-04-05T19:54:22","modified_gmt":"2023-04-05T11:54:22","slug":"spring-boot-uses-bloomfilter-bloom-filter-mechanism-in-redis","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/02\/spring-boot-uses-bloomfilter-bloom-filter-mechanism-in-redis\/","title":{"rendered":"SpringBoot\u5728Redis\u4e2d\u4f7f\u7528BloomFilter\u5e03\u9686\u8fc7\u6ee4\u5668\u673a\u5236"},"content":{"rendered":"<p>Redis\u7f13\u5b58\u7a7f\u900f\uff1a\u67e5\u8be2Redis\uff0c\u4e3a\u4e86\u9632\u6b62\u4ed6\u4eba\u6076\u610f\u4f7f\u7528\u4e0d\u5b58\u5728\u7684key\u8bbf\u95eeredis\uff0c\u9020\u6210\u5927\u6279\u91cf\u7684\u51fa\u73b0\u7f13\u5b58\u7a7f\u900f\u73b0\u8c61\uff08\u76f4\u63a5\u67e5\u8be2\u6570\u636e\u5e93\uff0c\u5bfc\u81f4\u6570\u636e\u5e93\u625b\u4e0d\u4f4f\uff09<\/p>\n<h2>Maven\u4f9d\u8d56<\/h2>\n<p>\u6dfb\u52a0 Redis &amp; BloomFilter \u7684\u6838\u5fc3\u4f9d\u8d56\u5305\uff1a<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-xml\">&lt;!--\u4f7f\u7528Redis--&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-data-redis&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n&lt;!--\u501f\u52a9guava\u7684\u5e03\u9686\u8fc7\u6ee4\u5668--&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;com.google.guava&lt;\/groupId&gt;\n    &lt;artifactId&gt;guava&lt;\/artifactId&gt;\n    &lt;version&gt;19.0&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h2>\u9879\u76ee\u914d\u7f6e<\/h2>\n<p>\u914d\u7f6eRedis\u8fde\u63a5\u4fe1\u606f\uff1a<\/p>\n<pre><code class=\"language-yml\">spring:\n  redis:\n    database: 3\n    host: 127.0.0.1\n    port: 6379\n    password: 12345\n    jedis.pool.max-idle: 100\n    jedis.pool.max-wait: -1ms\n    jedis.pool.min-idle: 2\n    timeout: 2000ms<\/code><\/pre>\n<h2>\u9879\u76ee\u4ee3\u7801<\/h2>\n<p>\u5982\u679c\u53ea\u662f\u4e00\u822c\u7684\u4f7f\u7528Redis\u5b58\u5b57\u7b26\u4e32\u7684\u8bdd\uff0c\u4f7f\u7528StringRedisTemplate\uff0c\u5c31\u4e0d\u9700\u8981\u914d\u7f6e\u5e8f\u5217\u5316\u3002<br \/>\n\u4f46\u662f\u8fd9\u91cc\u4f7f\u7528\u7684\u662f<code>RedisTemplate&lt;String, Object&gt; redisTemplate<\/code>\uff0c\u5b58\u50a8\u7684\u662f\u5bf9\u8c61\uff0c\u6240\u4ee5\u4e3a\u4e86\u9632\u6b62\u5b58\u5165\u7684\u5bf9\u8c61\u503c\u5728\u67e5\u770b\u7684\u65f6\u5019\u4e0d\u663e\u793a\u4e71\u7801\uff0c\u5c31\u9700\u8981\u914d\u7f6e\u76f8\u5173\u7684\u5e8f\u5217\u5316\uff08\u5176\u5b9e\u6211\u4eec\u5b58\u7684bit\u7ed3\u6784\u6570\u636e\uff0c\u5e03\u9686\u8fc7\u6ee4\u5668\u5b58\u503c\u5206\u5206\u949f\u90fd\u662f\u767e\u4e07\u7ea7\u522b\u7684\uff0c\u4f1a\u56e0\u4e3a\u6570\u636e\u91cf\u592a\u5927redis\u5ba2\u6237\u7aef\u4e5f\u6ca1\u529e\u6cd5\u663e\u793a\uff0c\u4e0d\u8fc7\u4e0d\u5f71\u54cd\u4f7f\u7528\uff09<\/p>\n<p><code>RedisConfig.class<\/code>\uff1a<\/p>\n<pre><code class=\"language-java\">import com.fasterxml.jackson.annotation.JsonAutoDetect;\nimport com.fasterxml.jackson.annotation.PropertyAccessor;\nimport com.fasterxml.jackson.databind.ObjectMapper;\nimport com.google.common.base.Charsets;\nimport com.google.common.hash.Funnel;\nimport cn.appblog.mall.util.BloomFilterHelper;\nimport org.springframework.cache.CacheManager;\nimport org.springframework.cache.annotation.EnableCaching;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.data.redis.cache.RedisCacheManager;\nimport org.springframework.data.redis.connection.RedisConnectionFactory;\nimport org.springframework.data.redis.core.RedisTemplate;\nimport org.springframework.data.redis.core.StringRedisTemplate;\nimport org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;\n\n@Configuration\n@EnableCaching\npublic class RedisConfig {\n\n    @Bean\n    public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {\n        RedisCacheManager rcm=RedisCacheManager.create(connectionFactory);\n        return rcm;\n    }\n\n    @Bean\n    public RedisTemplate&lt;String, Object&gt; redisTemplate(RedisConnectionFactory factory) {\n        RedisTemplate&lt;String, Object&gt; redisTemplate = new RedisTemplate&lt;String, Object&gt;();\n        redisTemplate.setConnectionFactory(factory);\n\n        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new\n                Jackson2JsonRedisSerializer(Object.class);\n        ObjectMapper om = new ObjectMapper();\n        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);\n        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);\n        jackson2JsonRedisSerializer.setObjectMapper(om);\n        \/\/\u5e8f\u5217\u5316\u8bbe\u7f6e \uff0c\u8fd9\u6837\u8ba1\u7b97\u662f\u6b63\u5e38\u663e\u793a\u7684\u6570\u636e\uff0c\u4e5f\u80fd\u6b63\u5e38\u5b58\u50a8\u548c\u83b7\u53d6\n        redisTemplate.setKeySerializer(jackson2JsonRedisSerializer);\n        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);\n        redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);\n        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);\n\n        return redisTemplate;\n    }\n\n    @Bean\n    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {\n        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();\n        stringRedisTemplate.setConnectionFactory(factory);\n        return stringRedisTemplate;\n    }\n\n    \/\/\u521d\u59cb\u5316\u5e03\u9686\u8fc7\u6ee4\u5668\uff0c\u653e\u5165\u5230spring\u5bb9\u5668\u91cc\u9762\n    @Bean\n    public BloomFilterHelper&lt;String&gt; initBloomFilterHelper() {\n        return new BloomFilterHelper&lt;&gt;((Funnel&lt;String&gt;) (from, into) -&gt; into.putString(from, Charsets.UTF_8).putString(from, Charsets.UTF_8), 1000000, 0.01);\n    }\n}<\/code><\/pre>\n<p><code>BloomFilterHelper.calss<\/code>\uff1a<\/p>\n<pre><code class=\"language-java\">import com.google.common.base.Preconditions;\nimport com.google.common.hash.Funnel;\nimport com.google.common.hash.Hashing;\n\npublic class BloomFilterHelper&lt;T&gt; {\n\n    private int numHashFunctions;\n\n    private int bitSize;\n\n    private Funnel&lt;T&gt; funnel;\n\n    public BloomFilterHelper(Funnel&lt;T&gt; funnel, int expectedInsertions, double fpp) {\n        Preconditions.checkArgument(funnel != null, &quot;funnel\u4e0d\u80fd\u4e3a\u7a7a&quot;);\n        this.funnel = funnel;\n        \/\/ \u8ba1\u7b97bit\u6570\u7ec4\u957f\u5ea6\n        bitSize = optimalNumOfBits(expectedInsertions, fpp);\n        \/\/ \u8ba1\u7b97hash\u65b9\u6cd5\u6267\u884c\u6b21\u6570\n        numHashFunctions = optimalNumOfHashFunctions(expectedInsertions, bitSize);\n    }\n\n    public int[] murmurHashOffset(T value) {\n        int[] offset = new int[numHashFunctions];\n\n        long hash64 = Hashing.murmur3_128().hashObject(value, funnel).asLong();\n        int hash1 = (int) hash64;\n        int hash2 = (int) (hash64 &gt;&gt;&gt; 32);\n        for (int i = 1; i &lt;= numHashFunctions; i++) {\n            int nextHash = hash1 + i * hash2;\n            if (nextHash &lt; 0) {\n                nextHash = ~nextHash;\n            }\n            offset[i - 1] = nextHash % bitSize;\n        }\n\n        return offset;\n    }\n\n    \/**\n     * \u8ba1\u7b97bit\u6570\u7ec4\u957f\u5ea6\n     *\/\n    private int optimalNumOfBits(long n, double p) {\n        if (p == 0) {\n            \/\/ \u8bbe\u5b9a\u6700\u5c0f\u671f\u671b\u957f\u5ea6\n            p = Double.MIN_VALUE;\n        }\n        int sizeOfBitArray = (int) (-n * Math.log(p) \/ (Math.log(2) * Math.log(2)));\n        return sizeOfBitArray;\n    }\n\n    \/**\n     * \u8ba1\u7b97hash\u65b9\u6cd5\u6267\u884c\u6b21\u6570\n     *\/\n    private int optimalNumOfHashFunctions(long n, long m) {\n        int countOfHash = Math.max(1, (int) Math.round((double) m \/ n * Math.log(2)));\n        return countOfHash;\n    }\n}<\/code><\/pre>\n<p>\u7136\u540e\u662f\u5177\u4f53\u7684\u5e03\u9686\u8fc7\u6ee4\u5668\u914d\u5408Redis\u4f7f\u7528\u7684\u65b9\u6cd5\u7c7b<code>RedisBloomFilter.class<\/code>\uff1a<\/p>\n<pre><code class=\"language-java\">import com.google.common.base.Preconditions;\nimport com.jc.mytest.util.BloomFilterHelper;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.data.redis.core.RedisTemplate;\nimport org.springframework.stereotype.Service;\n\n@Service\npublic class RedisBloomFilter {\n    @Autowired\n    private RedisTemplate redisTemplate;\n\n    \/**\n     * \u6839\u636e\u7ed9\u5b9a\u7684\u5e03\u9686\u8fc7\u6ee4\u5668\u6dfb\u52a0\u503c\n     *\/\n    public &lt;T&gt; void addByBloomFilter(BloomFilterHelper&lt;T&gt; bloomFilterHelper, String key, T value) {\n        Preconditions.checkArgument(bloomFilterHelper != null, &quot;bloomFilterHelper\u4e0d\u80fd\u4e3a\u7a7a&quot;);\n        int[] offset = bloomFilterHelper.murmurHashOffset(value);\n        for (int i : offset) {\n           System.out.println(&quot;key : &quot; + key + &quot; &quot; + &quot;value : &quot; + i);\n            redisTemplate.opsForValue().setBit(key, i, true);\n        }\n    }\n\n    \/**\n     * \u6839\u636e\u7ed9\u5b9a\u7684\u5e03\u9686\u8fc7\u6ee4\u5668\u5224\u65ad\u503c\u662f\u5426\u5b58\u5728\n     *\/\n    public &lt;T&gt; boolean includeByBloomFilter(BloomFilterHelper&lt;T&gt; bloomFilterHelper, String key, T value) {\n        Preconditions.checkArgument(bloomFilterHelper != null, &quot;bloomFilterHelper\u4e0d\u80fd\u4e3a\u7a7a&quot;);\n        int[] offset = bloomFilterHelper.murmurHashOffset(value);\n        for (int i : offset) {\n            System.out.println(&quot;key : &quot; + key + &quot; &quot; + &quot;value : &quot; + i);\n            if (!redisTemplate.opsForValue().getBit(key, i)) {\n                return false;\n            }\n        }\n        return true;\n    }\n}<\/code><\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u5176\u5b9e\u6574\u5408Redis\u5e76\u4f7f\u7528BloomFilter\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u4ee3\u7801\u90fd\u5df2\u7ecf\u5b8c\u6bd5<\/p>\n<h2>\u6d4b\u8bd5\u63a5\u53e3<\/h2>\n<pre><code class=\"language-java\">@Autowired\nRedisBloomFilter redisBloomFilter;\n\n@Autowired\nprivate BloomFilterHelper bloomFilterHelper;\n\n@ResponseBody\n@RequestMapping(&quot;\/add&quot;)\npublic String addBloomFilter(@RequestParam (&quot;orderNum&quot;) String orderNum) {\n    try {\n        redisBloomFilter.addByBloomFilter(bloomFilterHelper, &quot;bloom&quot;, orderNum);\n    } catch (Exception e) {\n        e.printStackTrace();\n        return &quot;\u6dfb\u52a0\u5931\u8d25&quot;;\n    }\n\n    return &quot;\u6dfb\u52a0\u6210\u529f&quot;;\n}\n\n@ResponseBody\n@RequestMapping(&quot;\/check&quot;)\npublic boolean checkBloomFilter(@RequestParam (&quot;orderNum&quot;) String orderNum) {\n    boolean b = redisBloomFilter.includeByBloomFilter(bloomFilterHelper, &quot;bloom&quot;, orderNum);\n    return b;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u7f13\u5b58\u7a7f\u900f\uff1a\u67e5\u8be2Redis\uff0c\u4e3a\u4e86\u9632\u6b62\u4ed6\u4eba\u6076\u610f\u4f7f\u7528\u4e0d\u5b58\u5728\u7684key\u8bbf\u95eeredis\uff0c\u9020\u6210\u5927\u6279\u91cf\u7684\u51fa\u73b0\u7f13\u5b58\u7a7f\u900f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,41],"tags":[544,543],"class_list":["post-2145","post","type-post","status-publish","format-standard","hentry","category-redis","category-spring-boot","tag-bloomfilter","tag-543"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2145"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2145\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}