当前位置: 代码迷 >> 综合 >> Redis应用-BloomFilter(布隆过滤器)
  详细解决方案

Redis应用-BloomFilter(布隆过滤器)

热度:26   发布时间:2023-09-22 12:38:30.0

Redis应用(BloomFilter)

redis集成bloomFilter,可以提供命中率,减少缓存击穿的发生。
bloomFilter通过位图及多hash定位,可在一定的范围内避免hahs冲突,因为布隆过滤器的基础是位图,所以占用空间低,查询效率高。但因为布隆过滤器的数据特点,所以无法移除失效数据;还有就是布隆过滤器具有一定的误判率。

链接: bloomFilter项目演示demo.

pom.xml

<!-- springboot集成redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- pool化 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.8.0</version></dependency><!-- 时间格式处理库 --><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId><version>2.10.6</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.9</version></dependency><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>21.0</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.12</version></dependency>

properties

# 应用服务 WEB 访问端口
server:port: 8080
# 应用名称
spring:application:name: bloomfilterredis:host: xxxxxport: 6379password: xxxxdatabase: 2lettuce:pool:max-active: -1max-idle: 2000max-wait: -1min-idle: 1time-between-eviction-runs: 5000

实现调用方法

public class BloomFilterCache {
    public static BloomFilter<String> bloomCache;
}
@Slf4j
@Component
public class BloomFilterDataLoadApplicationRunner implements ApplicationRunner {
    @Autowiredprivate RedisTemplate redisTemplate;@Overridepublic void run(ApplicationArguments args) throws Exception {
    ListOperations listOperations = redisTemplate.opsForList();// 获取数据List<String> list = listOperations.range(RedisKeyConstants.KEY, 0, -1);BloomFilter<String> bloomFilter = BloomFilter.create(Funnels.stringFunnel(Charsets.UTF_8), 100000, 0.03);list.parallelStream().forEach(entity->{
    bloomFilter.put(entity);});BloomFilterCache.bloomCache = bloomFilter;}
}