Redis中Redisson布隆过滤器的学习

 更新时间:2022年05月16日 09:20:42   作者:我家有猫已长成  
布隆过滤器是一个非常长的二进制向量和一系列随机哈希函数的组合,可用于检索一个元素是否存在,本文就详细的介绍一下Redisson布隆过滤器,具有一定的参考价值,感兴趣的可以了解一下

简介

本文基于Spring Boot 2.6.6、redisson 3.16.0简单分析Redisson布隆过滤器的使用。

布隆过滤器是一个非常长的二进制向量和一系列随机哈希函数的组合,可用于检索一个元素是否存在;

使用场景如下:

  • 解决Redis缓存穿透问题;
  • 邮件过滤;

使用

  • 建立一个二进制向量,所有位设置0;
  • 选择K个散列函数,用于对元素进行K次散列,计算向量的位下标;
  • 添加元素:将K个散列函数作用于该元素,生成K个值作为位下标,将向量的对应位设置为1;
  • 检索元素:将K个散列函数作用于该元素,生成K个值作为位下标,若向量的对应位都是1,则说明该元素可能存在;否则,该元素肯定不存在;

Demo

依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>
<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
    <version>3.16.0</version>
</dependency>

测试代码

public class BloomFilterDemo {

    public static void main(String[] args) {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");
        RedissonClient redissonClient = Redisson.create(config);
        RBloomFilter<String> bloomFilter = redissonClient.getBloomFilter("bloom-filter");
        // 初始化布隆过滤器
        bloomFilter.tryInit(200, 0.01);

        List<String> elements = new ArrayList<>();
        for (int i = 0; i < 200; i++) {
            elements.add(UUID.randomUUID().toString());
        }

        // 向布隆过滤器中添加内容
        init(bloomFilter, elements);
        // 测试检索效果
        test(bloomFilter, elements);

        redissonClient.shutdown();
    }

    public static void init(RBloomFilter<String> bloomFilter, List<String> elements) {
        for (int i = 0; i < elements.size(); i++) {
            if (i % 2 == 0) {
                bloomFilter.add(elements.get(i));
            }
        }
    }

    public static void test(RBloomFilter<String> bloomFilter, List<String> elements) {
        int counter = 0;
        for (String element : elements) {
            if (bloomFilter.contains(element)) {
                counter++;
            }
        }
        System.out.println(counter);
    }
}

简析

初始化

布隆过滤器的初始化方法tryInit有两个参数:

  • expectedInsertions:预期的插入元素数量;
  • falseProbability:预期的错误率;

布隆过滤器可以明确元素不存在,但对于元素存在的判断是存在错误率的;所以初始化时指定的这两个参数会决定布隆过滤器的向量长度和散列函数的个数;
RedissonBloomFilter.tryInit方法代码如下:

public boolean tryInit(long expectedInsertions, double falseProbability) {
    if (falseProbability > 1) {
        throw new IllegalArgumentException("Bloom filter false probability can't be greater than 1");
    }
    if (falseProbability < 0) {
        throw new IllegalArgumentException("Bloom filter false probability can't be negative");
    }

    // 根据元素个数和错误率计算得到向量长度
    size = optimalNumOfBits(expectedInsertions, falseProbability);
    if (size == 0) {
        throw new IllegalArgumentException("Bloom filter calculated size is " + size);
    }
    if (size > getMaxSize()) {
        throw new IllegalArgumentException("Bloom filter size can't be greater than " + getMaxSize() + ". But calculated size is " + size);
    }
    // 根据元素个数和向量长度计算得到散列函数的个数
    hashIterations = optimalNumOfHashFunctions(expectedInsertions, size);

    CommandBatchService executorService = new CommandBatchService(commandExecutor);
    executorService.evalReadAsync(configName, codec, RedisCommands.EVAL_VOID,
            "local size = redis.call('hget', KEYS[1], 'size');" +
                    "local hashIterations = redis.call('hget', KEYS[1], 'hashIterations');" +
                    "assert(size == false and hashIterations == false, 'Bloom filter config has been changed')",
                    Arrays.<Object>asList(configName), size, hashIterations);
    executorService.writeAsync(configName, StringCodec.INSTANCE,
                                            new RedisCommand<Void>("HMSET", new VoidReplayConvertor()), configName,
            "size", size, "hashIterations", hashIterations,
            "expectedInsertions", expectedInsertions, "falseProbability", BigDecimal.valueOf(falseProbability).toPlainString());
    try {
        executorService.execute();
    } catch (RedisException e) {
        if (e.getMessage() == null || !e.getMessage().contains("Bloom filter config has been changed")) {
            throw e;
        }
        readConfig();
        return false;
    }

    return true;
}

private long optimalNumOfBits(long n, double p) {
    if (p == 0) {
        p = Double.MIN_VALUE;
    }
    return (long) (-n * Math.log(p) / (Math.log(2) * Math.log(2)));
}

private int optimalNumOfHashFunctions(long n, long m) {
    return Math.max(1, (int) Math.round((double) m / n * Math.log(2)));
}

添加元素

向布隆过滤器中添加元素时,先使用一系列散列函数根据元素得到K个位下标,然后将向量中位下标对应的位设置为1;
RedissonBloomFilter.add方法代码如下:

public boolean add(T object) {
    // 根据带插入元素得到两个long类型散列值
    long[] hashes = hash(object);

    while (true) {
        if (size == 0) {
            readConfig();
        }

        int hashIterations = this.hashIterations;
        long size = this.size;

        // 得到位下标数组
        // 以两个散列值根据指定策略生成hashIterations个散列值,从而得到位下标
        long[] indexes = hash(hashes[0], hashes[1], hashIterations, size);

        CommandBatchService executorService = new CommandBatchService(commandExecutor);
        addConfigCheck(hashIterations, size, executorService);
        RBitSetAsync bs = createBitSet(executorService);
        for (int i = 0; i < indexes.length; i++) {
            // 将位下标对应位设置1
            bs.setAsync(indexes[i]);
        }
        try {
            List<Boolean> result = (List<Boolean>) executorService.execute().getResponses();

            for (Boolean val : result.subList(1, result.size()-1)) {
                if (!val) {
                    // 元素添加成功
                    return true;
                }
            }
            // 元素已存在
            return false;
        } catch (RedisException e) {
            if (e.getMessage() == null || !e.getMessage().contains("Bloom filter config has been changed")) {
                throw e;
            }
        }
    }
}

private long[] hash(Object object) {
    ByteBuf state = encode(object);
    try {
        return Hash.hash128(state);
    } finally {
        state.release();
    }
}

private long[] hash(long hash1, long hash2, int iterations, long size) {
    long[] indexes = new long[iterations];
    long hash = hash1;
    for (int i = 0; i < iterations; i++) {
        indexes[i] = (hash & Long.MAX_VALUE) % size;
        // 散列函数的实现方式
        if (i % 2 == 0) {
            // 新散列值
            hash += hash2;
        } else {
            // 新散列值
            hash += hash1;
        }
    }
    return indexes;
}

hash(long hash1, long hash2, int iterations, long size)方法中,利用根据元素得到的两个散列值,生成一系列散列函数,然后得到位下标数组;

检索元素

检索布隆过滤器中是否存在指定元素时,先使用一系列散列函数根据元素得到K个位下标,然后判断向量中位下标对应的位是否为1,若存在一个不为1,则该元素不存在;否则认为存在;
RedissonBloomFilter.contains方法代码如下:

public boolean contains(T object) {
    // 根据带插入元素得到两个long类型散列值
    long[] hashes = hash(object);

    while (true) {
        if (size == 0) {
            readConfig();
        }

        int hashIterations = this.hashIterations;
        long size = this.size;

        // 得到位下标数组
        // 以两个散列值根据指定策略生成hashIterations个散列值,从而得到位下标
        long[] indexes = hash(hashes[0], hashes[1], hashIterations, size);

        CommandBatchService executorService = new CommandBatchService(commandExecutor);
        addConfigCheck(hashIterations, size, executorService);
        RBitSetAsync bs = createBitSet(executorService);
        for (int i = 0; i < indexes.length; i++) {
            // 获取位下标对应位的值
            bs.getAsync(indexes[i]);
        }
        try {
            List<Boolean> result = (List<Boolean>) executorService.execute().getResponses();

            for (Boolean val : result.subList(1, result.size()-1)) {
                if (!val) {
                    // 若存在不为1的位,则认为元素不存在
                    return false;
                }
            }
            // 都为1,则认为元素存在
            return true;
        } catch (RedisException e) {
            if (e.getMessage() == null || !e.getMessage().contains("Bloom filter config has been changed")) {
                throw e;
            }
        }
    }
}

到此这篇关于Redis中Redisson布隆过滤器的学习的文章就介绍到这了,更多相关Redis Redisson布隆过滤器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Redis GEO实现搜索附近用户的项目实践

    Redis GEO实现搜索附近用户的项目实践

    RedisGEO主要用于存储地理位置信息,并对存储的信息进行操作,本文主要介绍了Redis GEO实现搜索附近用户的项目实践,具有一定的参考价值,感兴趣的可以了解一下
    2024-05-05
  • Redis服务之高可用组件sentinel详解

    Redis服务之高可用组件sentinel详解

    这篇文章主要介绍了Redis服务之高可用组件sentinel,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • redis zrange 与 zrangebyscore的区别解析

    redis zrange 与 zrangebyscore的区别解析

    这篇文章主要介绍了redis zrange与zrangebyscore的区别,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06
  • Redis进行缓存操作的实现

    Redis进行缓存操作的实现

    本文主要介绍了Redis进行缓存操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-03-03
  • Redisson 主从一致性问题详解

    Redisson 主从一致性问题详解

    这篇文章主要为大家介绍了Redisson 主从一致性问题详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • Redis GEO实现附近搜索功能

    Redis GEO实现附近搜索功能

    这篇文章主要介绍了Redis GEO实现附近搜索功能,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2024-12-12
  • Redisson实现Redis分布式锁的几种方式

    Redisson实现Redis分布式锁的几种方式

    本文在讲解如何使用Redisson实现Redis普通分布式锁,以及Redlock算法分布式锁的几种方式的同时,也附带解答这些同学的一些疑问,感兴趣的可以了解一下
    2021-08-08
  • redis如何清理缓存

    redis如何清理缓存

    本文主要介绍了redis如何清理缓存,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01
  • 浅谈Redis对于过期键的三种清除策略

    浅谈Redis对于过期键的三种清除策略

    本文主要介绍了Redis对于过期键的三种清除策略,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • Redis高级玩法之利用SortedSet实现多维度排序的方法

    Redis高级玩法之利用SortedSet实现多维度排序的方法

    Redis的SortedSet是可以根据score进行排序的,以手机应用商店的热门榜单排序为例,根据下载量倒序排列。接下来通过本文给大家分享Redis高级玩法之利用SortedSet实现多维度排序的方法,一起看看吧
    2019-07-07

最新评论