https://blog.csdn.net/m0_50976581/article/details/113837970
布隆过滤器初体验: 项目启动时创建bloomFilter对象,给他存数据,再注入到容器里,这个就不写了,跟下面差不多
... ...
定时任务触发,清空bloomFilter,存入最新的数据。
/**
* @author gaoyuzheng
*/
@Component
public class BloomTask {
@Autowired
PmsFeign pmsFeign;
@Autowired
RBloomFilter rBloomFilter;
@Autowired
RedissonClient redissonClient;
//每天凌晨2点刷新bloomFilter
@Scheduled(cron = "0 0 2 * * ? ")
public void flashBloom(){
//不delete()原有旧数据还在
rBloomFilter.delete();
rBloomFilter = redissonClient.getBloomFilter("index:bloomFilter");
//定时刷新,不需要设置特别大
rBloomFilter.tryInit(50000l, 0.05);
//openFeign远程调用 查询新数据 更新到bloomFilter
ResponseVo<List<CategoryEntity>> categoryEntityByparentId = pmsFeign.getCategoryEntityByparentId(0l);
List<CategoryEntity> categoryEntityList = categoryEntityByparentId.getData();
for (CategoryEntity categoryEntity : categoryEntityList) {
Long cId = categoryEntity.getId();
rBloomFilter.add("categoryId:" + cId);
}
}
}
避免缓存穿透,判断有没有,没有就return
if (!rBloomFilter.contains("categoryId:"+cId)){
return null;
}
完成!!!