SpringBoot(18)---通过Lua脚本批量插入数据到Redis布隆过滤器

本文介绍了SpringBoot项目中使用Redis布隆过滤器进行数据存在的判断,对比了List、Map和Google布隆过滤器的性能,探讨了不同数据量场景下的适用方案。通过Lua脚本实现批量插入,分析了Redis服务器搭建和脚本执行过程,并提供了测试结果。

在实际开发过程中经常会做的一步操作,就是判断当前的key是否存在。

那这篇博客主要分为三部分:

1、几种方式判断当前key是否存在的性能进行比较。
2、Redis实现布隆过滤器并批量插入数据,并判断当前key值是否存在。
3、针对以上做一个总结。

一、性能对比

主要对以下方法进行性能测试比较:

1、List的 contains 方法

2、Map的 containsKey 方法

3、Google布隆过滤器 mightContain 方法

前提准备

在SpringBoot项目启动的时候,向 List集合Map集合Google布隆过滤器 分布存储500万条,长度为32位的String字符串。

1、演示代码

@Slf4j
@RestController
public class PerformanceController {

    /**
     * 存储500万条数据
     */
    public static final int SIZE = 5000000;
    /**
     * list集合存储数据
     */
    public static List<String> list = Lists.newArrayListWithCapacity(SIZE);
    /**
     * map集合存储数据
     */
    public static Map<String, Integer> map = Maps.newHashMapWithExpectedSize(SIZE);
    /**
     * guava 布隆过滤器
     */
    BloomFilter<String> bloomFilter = BloomFilter.create(Funnels.unencodedCharsFunnel(), SIZE);
    /**
     * 用来校验的集合
     */
    public static List<String> exist = Lists.newArrayList();
    /**
     * 计时工具类
     */
    public static Stopwatch stopwatch = Stopwatch.createUnstarted();

    /**
     * 初始化数据
     */
    @PostConstruct
    public void insertData() {
        for (int i = 0; i < SIZE; i++) {
            String data = UUID.randomUUID().toString();
            data = data.replace("-", "");
            //1、存入list
            list.add(data);
            //2、存入map
           map.put(data, 0);
            
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值