布隆过滤器使用

初始化布隆过滤器

  /**
     * 启动时候将产品加入到 布隆过滤器中
     */
    @PostConstruct
    public void init() {
        bloomFilter = redissonClient.getBloomFilter(BLOOM_STR, new JsonJacksonCodec());
        this.refreshBloom();
    }

    @Override
    public void refreshBloom() {
        bloomFilter.delete();
        //初始化布隆过滤器:预计元素为 1000000L (这个值根据实际的数量进行调整),误差率为3%
        bloomFilter.tryInit(1000000L, 0.03);
        List<Integer> productIdList = this.list(new LambdaQueryWrapper<Product>().select(Product::getId))
                .stream().map(Product::getId).collect(Collectors.toList());
        productIdList.forEach(bloomFilter::add);
    }

controller层

    @Resource
    ProductService productService; 

 /**
     * 根据 id 查询详情
     *
     * @param id id
     * @return json string
     */
    @GetMapping("/{id}")
    public String getProduct(@PathVariable Integer id) {
        Product product = productService.getProductById(id);
        if (BeanUtil.isEmpty(product)) {
            return "暂无该产品!";
        }
        return JSONUtil.toJsonStr(product);
    }
 /**
     * 添加产品
     *
     * @param product 产品
     * @return boolean
     */
    @PostMapping("/add")
    public Boolean addProduct(@RequestBody Product product) {
        try {
            return productService.addProduct(product);
        } catch (Exception e) {
            log.error("异常:",e);
            throw new RuntimeException(e);
        }

    }
 /**
     * 根据id删除产品
     *
     * @param product 产品
     * @return boolean
     */
    @DeleteMapping("/{id}")
    public Boolean delProduct(@PathVariable Integer id) {
        return productService.removeById(id);
    }

service层

 查询

   private static final String BLOOM_STR = "product_list_bloom";

    private static final String REDIS_CACHE = "product_list";
    @Resource
    RedissonClient redissonClient;

    RBloomFilter<Integer> bloomFilter; 
@Override
    public Product getProductById(Integer id) {
        // 走布隆过滤器筛选一下,防止被缓存穿透
        boolean contains = bloomFilter.contains(id);
        // 如果布隆过滤器判断当前产品id 存在,则去查询数据库
        if (contains) {
            // 先去缓存中查
            log.info("正在redis缓存中查询:{}",id);
            RMap<Integer, String> productCache = redissonClient.getMap(REDIS_CACHE);
            String cacheProduct = productCache.get(id);
            if (StrUtil.isNotEmpty(cacheProduct)) {
                // 如果缓存中不是空 则返回
                return JSONUtil.toBean(cacheProduct, Product.class);
            }
            Product product = this.getById(id);
            // 如果查到了数据,那么存一份到 redis 中去
            if (BeanUtil.isNotEmpty(product)) {
                productCache.put(id, JSONUtil.toJsonStr(product));
                return product;
            }
        } else {
            log.info("布隆过滤器中不存在产品id:{}的数据", id);
        }
        return null;
    }

添加:

 /**
     * 添加产品
     *
     * @param product 产品
     * @return boolean
     */
    @PostMapping("/add")
    public Boolean addProduct(@RequestBody Product product) {
        try {
            return productService.addProduct(product);
        } catch (Exception e) {
            log.error("异常:",e);
            throw new RuntimeException(e);
        }

    }
import com.baomidou.mybatisplus.extension.service.IService;
import top.bulk.bloom.entity.Product;

/**
 * 产品demo(Product)表服务接口
 *
 * @author wanglu
 * @since 2022-12-08 18:00:14
 */
public interface ProductService extends IService<Product> {
    /**
     * 查询产品详情
     *
     * @param id id
     * @return product
     */
    Product getProductById(Integer id);

    /**
     * 添加产品操作
     *
     * @param product 产品信息
     * @return 返回
     */
    Boolean addProduct(Product product);

    /**
     * 清空 布隆过滤器中的数据,然后重新添加
     * 删除后,重新添加
     */
    void refreshBloom();
}

实体层:

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * 产品demo(Product)表实体类
 *
 * @author wanglu
 * @since 2022-12-08 18:00:11
 */
@Data
public class Product implements Serializable {
    /**
     * 主键
     **/
    private Integer id;
    /**
     * 名称
     **/
    private String productName;
    /**
     * 价钱
     **/
    private Double productPrice;
    /**
     * 数量
     **/
    private Integer productNum;
    /**
     * 添加时间
     **/
    @DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")                    // 表示返回时间类型
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")      // 表示接收时间类型
    private LocalDateTime addTime;
    /**
     * 创建人
     **/
    private String addBy;
    /**
     * 更新时间
     **/
    @DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")                    // 表示返回时间类型
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")      // 表示接收时间类型
    private Date updateTime;
    /**
     * 更新人
     **/
    private String updateBy;

}

mapper层

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import top.bulk.bloom.entity.Product;

/**
 * 产品demo(Product)表数据库访问层
 *
 * @author wanglu
 * @since 2022-12-08 18:00:10
 */
public interface ProductMapper extends BaseMapper<Product> {

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Redis布隆过滤器是一种基于布隆过滤器实现的数据结构,它可以用于高效地判断一个元素是否存在于集合中。在Redis中,通过使用BITMAPS和HASHES命令来实现布隆过滤器。 要使用Redis布隆过滤器,首先需要在Redis服务器上安装和配置Redis,并确保已经加载了布隆过滤器模块。然后,可以使用BF.ADD命令将元素添加到布隆过滤器中,使用BF.EXISTS命令查询元素是否存在于布隆过滤器中。 Redis布隆过滤器使用相对简单,但也存在一些注意事项。由于布隆过滤器是基于概率的数据结构,存在一定的误判率。为了减少误判率,可以增加二进制数组的位数或增加哈希次数。此外,由于布隆过滤器是基于内存的数据结构,需要确保Redis服务器具有足够的内存来存储布隆过滤器。 总之,通过安装和配置Redis服务器,并使用BF.ADD和BF.EXISTS命令,可以轻松地在Redis中使用布隆过滤器来判断元素是否存在于集合中。但需要注意误判率和内存需求等因素。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [redis使用布隆过滤器](https://blog.csdn.net/qq_40179653/article/details/125716731)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Python+Redis实现布隆过滤器](https://download.csdn.net/download/weixin_38751905/14912053)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值