一.引入依赖

    <!-- redis --> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-data-redis</artifactId> 
        </dependency> 
        <!-- redisson --> 
        <dependency> 
            <groupId>org.redisson</groupId> 
            <artifactId>redisson</artifactId> 
        </dependency>    
    <!-- springbootb版本 -->
     <dependency>

     <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.2.6.RELEASE</version>
    
</dependency>

二.配置类

import org.redisson.api.RBloomFilter; 
import org.redisson.api.RedissonClient; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
 
import java.util.function.Predicate; 
 
/** 
 * @author Sam.yang 
 * @since 2021/7/30 10:10 
 */ 
@Configuration 
public class RedissonBloomFilterConfig { 
 
    /** 
     * 初始化设备信息布隆过滤器 
     * 
     * @param redissonClient {@link RedissonClient} 
     * @return {@link RBloomFilter} 
     */ 
    @Bean 
    public RBloomFilter<String> getNnPcDeviceBloomFilter(@Autowired RedissonClient redissonClient) { 
        //获取布隆过滤器 
        RBloomFilter<String> nnPcDeviceFilter = redissonClient.getBloomFilter(CacheConstant.BLOOM_FILTER_DEVICE_CONSTANT); 
        Predicate<RBloomFilter> predicate = rBloomFilter -> !nnPcDeviceFilter.isExists(); 
        //初始化布隆过滤器,注意这里必须要初始化,否则在使用的时候,会报错未初始化布隆过滤器,初始值5000000 是一个经验值,根据业务场景设置 
        if (predicate.test(nnPcDeviceFilter)) { 
            nnPcDeviceFilter.tryInit(5000000, 0.03); 
        } 
        return nnPcDeviceFilter; 
    } 
 
}

三.使用

    @Autowired 
    @Qualifier(value = "getNnPcDeviceBloomFilter") 
    private RBloomFilter<String> nnPcDeviceBloomFilter;
       exist = nnPcDeviceBloomFilter.contains(deviceId); 
        if (!exist) { 
         //业务逻辑,这里省略*********** 
        }

本文参考链接:https://www.cnblogs.com/july-sunny/p/15088071.html
评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!