Redis-布隆过滤器

news/2025/2/2 7:51:09/

文章目录

布隆过滤器的特点:

就可以把布隆过滤器理解为一个set集合,我们可以通过add往里面添加元素,通过contains来判断是否包含某个元素

布隆过滤器是一个很长的二进制向量和一系列随机映射函数。 可以用来检索一个元素是否存在一个集合中,优点是空间效率和时间都比一般算法要好,缺点是存在一定的误判和删除困难。 因为 hash 冲突的存在,可能会有误判的概率。

应用场景包括下面:

布隆过滤器的特点:

结构图:

查询的时间复杂度是 O(N) n 是 hash 函数的数量。

如果某个位置为 0,可以一定判断不存在,但是某个位置不为 0 不能就肯定一定存在 mysql 中.

实践布隆过滤器

底层使用的位图数据结构。

import redis.clients.jedis.Jedis;
import java.nio.charset.StandardCharsets;
import java.util.BitSet;
import java.util.List;
import java.util.ArrayList;public class RedisBloomFilter {private static final String BLOOM_FILTER_KEY = "bloom_filter";private static final int BITMAP_SIZE = 1000000; // 位图大小private static final int[] HASH_SEEDS = {3, 5, 7, 11, 13, 17}; // 多个哈希函数的种子private Jedis jedis;private List<SimpleHash> hashFunctions;public RedisBloomFilter() {this.jedis = new Jedis("localhost", 6379);this.hashFunctions = new ArrayList<>();for (int seed : HASH_SEEDS) {hashFunctions.add(new SimpleHash(BITMAP_SIZE, seed));}}// 添加元素到布隆过滤器public void add(String value) {for (SimpleHash hashFunction : hashFunctions) {jedis.setbit(BLOOM_FILTER_KEY, hashFunction.hash(value), true);}}// 检查元素是否可能存在于布隆过滤器public boolean mightContain(String value) {for (SimpleHash hashFunction : hashFunctions) {if (!jedis.getbit(BLOOM_FILTER_KEY, hashFunction.hash(value))) {return false;}}return true;}// 关闭连接public void close() {jedis.close();}// 简单哈希函数public static class SimpleHash {private int cap;private int seed;public SimpleHash(int cap, int seed) {this.cap = cap;this.seed = seed;}public int hash(String value) {int result = 0;byte[] bytes = value.getBytes(StandardCharsets.UTF_8);for (byte b : bytes) {result = seed * result + b;}// 让hash分布的均匀一谢return (cap - 1) & result;}}public static void main(String[] args) {RedisBloomFilter bloomFilter = new RedisBloomFilter();// 添加元素到布隆过滤器bloomFilter.add("user1");bloomFilter.add("user2");bloomFilter.add("user3");// 检查元素是否可能存在System.out.println("Does user1 exist? " + bloomFilter.mightContain("user1")); // 输出: trueSystem.out.println("Does user4 exist? " + bloomFilter.mightContain("user4")); // 输出: false// 关闭连接bloomFilter.close();}
}

应用

IP 黑名单:

https://blog.csdn.net/m0_56079407/article/details/127046242

参考文章:

https://blog.csdn.net/qq_41125219/article/details/119982158


http://www.ppmy.cn/news/1568635.html

相关文章

QT简单实现验证码(字符)

0&#xff09; 运行结果 1&#xff09; 生成随机字符串 Qt主要通过QRandomGenerator类来生成随机数。在此之前的版本中&#xff0c;qrand()函数也常被使用&#xff0c;但从Qt 5.10起&#xff0c;推荐使用更现代化的QRandomGenerator类。 在头文件添加void generateRandomNumb…

vim操作简要记录

操作容易忘记&#xff0c;记录一下基本使用的 :wq保存退出 :w :q :q! :wq! i I a A 方向键 h左 j下 k上 l右 dd删除方行&#xff08;这其实是剪切行操作&#xff0c;不过一般用作删除&#xff0c;长按可删除&#xff0c;不过按.执行上一次操作删除更快&#xff09; .执行上…

Spring MVC消息转换器

在Spring MVC框架中&#xff0c;extendMessageConverters 通常与消息转换器&#xff08;Message Converters&#xff09;相关。消息转换器是Spring MVC用于将HTTP请求和响应主体&#xff08;body&#xff09;转换为Java对象和字符串的组件。它们在处理不同的媒体类型&#xff0…

appium自动化环境搭建

一、appium介绍 appium介绍 appium是一个开源工具、支持跨平台、用于自动化ios、安卓手机和windows桌面平台上面的原生、移动web和混合应用&#xff0c;支持多种编程语言(python&#xff0c;java&#xff0c;Ruby&#xff0c;Javascript、PHP等) 原生应用和混合应用&#xf…

文献阅读 250201-The carbon budget of China: 1980–2021

The carbon budget of China: 1980–2021 来自 <https://www.sciencedirect.com/science/article/pii/S2095927323007703> 中国碳预算&#xff1a;1980–2021 年 ## Abstract: Using state-of-the-art datasets and models, this study comprehensively estimated the an…

CSS关系选择器详解

CSS关系选择器详解 学习前提什么是关系选择器&#xff1f;后代选择器&#xff08;Descendant Combinator&#xff09;语法示例注意事项 子代选择器&#xff08;Child Combinator&#xff09;语法示例注意事项 邻接兄弟选择器&#xff08;Adjacent Sibling Combinator&#xff0…

Go学习:格式化输入输出

目录 1. 输出 2. 输入 1. 输出 常用格式&#xff1a; 格式说明%d整型格式%s字符串格式%c字符格式%f浮点数格式%T操作变量所属类型%v自动匹配格式输出 简单示例代码&#xff1a; package mainimport "fmt"func main() {a : 10b : "abc"c : ad : 3.14/…

MediaPipe与YOLO已训练模型实现可视化人脸和手势关键点检测

项目首页 - ZiTai_YOLOV11:基于前沿的 MediaPipe 技术与先进的 YOLOv11 预测试模型&#xff0c;精心打造一款强大的实时检测应用。该应用无缝连接摄像头&#xff0c;精准捕捉画面&#xff0c;能即时实现人脸检测、手势识别以及骨骼关键点检测&#xff0c;将检测结果实时、直观地…