RedisBloom使用

安装RedisBloom模块,从git获取对应的原码,make生成.so文件,挂载.so文件,启动redis

docker run --name test-redis -v /iothub/test-redis/data:/data -v /iothub/test-redis/modules:/modules -p 6378:6379 -d redis:4.0.10 redis-server --requirepass jimi@123 --appendonly yes --loadmodule /modules/redismodule.so
package com.jimi.rtvos.helper;import com.jimi.rtvos.consts.Constants;
import com.khan.utils.JacksonUtils;
import com.khan.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;/*** Description:* <p>* Redis布隆过滤器* </p>* <p>Redis 布隆过滤器需要 先安装RedisBloom模块</p>* <p>启动服务,需要先创建成功对应的Bloom过滤器,防止使用功能异常</p>** @Author: leo.xiong* @CreateDate: 2025/8/7 13:52* @Email: xionglang@xxxx.com* @Since:*/
@Component
public class RedisBloomHelper {private static final Logger LOGGER = LoggerFactory.getLogger(RedisBloomHelper.class);private static final String OK = "OK";private static final Long SUCCESS = 1L;@Resourceprotected JedisPool jedisPool;/*** 初始化布隆过滤器,初始化失败,服务启动失败,需要处理异常*/@PostConstructpublic void initBloom() {try {boolean creationResult = create(Constants.DEVICE_IMEI_BLOOM_FILTER, "0.00000001", "5000000", 2);if (!creationResult) {throw new RuntimeException("Failed to initialize bloom filter.");}LOGGER.info("Successfully initialized bloom filter: {}", Constants.DEVICE_IMEI_BLOOM_FILTER);} catch (Exception e) {LOGGER.error("Error occurred during initialization of bloom filter: ", e);throw new RuntimeException("Initialization failed due to an error.", e);}}/*** 创建一个新的布隆过滤器** @param filterName        布隆过滤器名称* @param falsePositiveRate 误报率,小于1,值越大,误报率越高,千万分之一误报率0.00000001* @param capacity          容量,容量如果小于写入数据,并且不自增,误报率会加大* @param autoCapacity      容量是否自增长,自增长的值,如果是1,就是不会自增长,可以设置为2*/private boolean create(String filterName, String falsePositiveRate, String capacity, Integer autoCapacity) {if (StringUtils.isEmpty(filterName) || StringUtils.isEmpty(falsePositiveRate) || StringUtils.isEmpty(capacity)) {LOGGER.error("The base configuration for creating a bloom filter is empty filterName:{} falsePositiveRate:{} capacity:{}", filterName, falsePositiveRate, capacity);return false;}try {if (isBloomFilterExists(filterName)) {// 布隆过滤器已存在,无需再次创建return true;}String result;if (autoCapacity == null || autoCapacity <= 1) {result = jedisPool.getResource().eval("return redis.call('BF.RESERVE', KEYS[1], ARGV[1], ARGV[2])",1, filterName, falsePositiveRate, capacity).toString();} else {result = jedisPool.getResource().eval("return redis.call('BF.RESERVE', KEYS[1], ARGV[1], ARGV[2], 'EXPANSION', ARGV[3])",1, filterName, falsePositiveRate, capacity, autoCapacity.toString()).toString();}LOGGER.info("Create bloom result:{} filterName:{} falsePositiveRate:{} capacity:{} autoCapacity:{}", result, filterName, falsePositiveRate, capacity, autoCapacity);return OK.equals(result);} catch (Exception e) {LOGGER.error("Error during bloom filter creation for filter name: {}", filterName, e);return false;}}private boolean isBloomFilterExists(String filterName) {Object value = null;try {value = jedisPool.getResource().eval("return redis.call('BF.INFO', KEYS[1])",1,filterName);// 如果返回值为空或者是一个空的哈希表,说明布隆过滤器不存在if (value == null) {return false;}LOGGER.info("Bloom filter already exists value:{}", JacksonUtils.toJson(value));// 返回值不为空且不是空哈希表,说明布隆过滤器存在return true;} catch (Exception e) {// 处理其他可能的异常LOGGER.info("checking bloom filter not exist");return false;}}/*** 向布隆过滤器中添加元素** @param filterName* @param value*/public boolean set(String filterName, String value) {Long result = execute("BF.ADD", filterName, value);//一般1是成功,0是已存在LOGGER.info("Write bloom filter result:{} filterName:{} value:{}", result, filterName, value);return result != null;}/*** 批量写入** @param filterName* @param imeiList* @return*/public List<Long> batchSet(String filterName, List<String> imeiList) {StringBuilder luaScript = new StringBuilder();luaScript.append("local result = {}\n");luaScript.append("for i = 1, #ARGV do\n");luaScript.append("    table.insert(result, redis.call('BF.ADD', KEYS[1], ARGV[i]))\n");luaScript.append("end\n");luaScript.append("return result\n");List<Long> resultList = (List<Long>) jedisPool.getResource().eval(luaScript.toString(), Arrays.asList(filterName), imeiList);LOGGER.info("Batch write bloom filter result:{} filterName:{} value:{}", resultList, filterName, imeiList);return resultList;}/*** 检查元素是否可能存在于布隆过滤器中** @return*/public boolean exist(String filterName, String value) {Long result = execute("BF.EXISTS", filterName, value);//一般1是成功,0是已存在LOGGER.info("exist bloom filter result:{} filterName:{} value:{}", result, filterName, value);return SUCCESS.equals(result);}private Long execute(String command, String filterName, String value) {if (StringUtils.isEmpty(filterName) || StringUtils.isEmpty(value)) {LOGGER.error("Bloom filter parameter is empty filterName:{} value:{}", filterName, value);return null;}return (Long) jedisPool.getResource().eval("return redis.call('" + command + "', KEYS[1], ARGV[1])",1, filterName, value);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/92205.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/92205.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ADC、Flash、SPI、watchdog

ADCADC(Analog-to-Digital Converter), 即模拟信号 - 数字信号转换器在STM32F103C8T6中, 同样具有ADC功能.以我们的芯片为例, 也存在2个片上外设ADC, 即ADC1和ADC2, 这两个ADC片上外设都挂载在APB2总线上.我们的ADC片上外设, 是一种具有12位逐次逼近型ADC,ADC转换的本质是不断的…

冷库设备远程监控物联网+省电节能解决方案

随着生鲜电商、医药冷链、跨境物流等行业的爆发式增长&#xff0c;我国冷库容量激增&#xff0c;但传统冷库管理模式正面临严峻挑战。据统计&#xff0c;国内冷链运输损耗率高达12%-15%&#xff0c;其中因温度失控导致的货损占比超60%。在某医药企业冷库事故中&#xff0c;因制…

如何开发一个运行在windows系统服务器上的服务

第一步&#xff1a;vs2022创建一个windows服务项目第二步&#xff1a;从工具箱拖拽出一个timer第三步&#xff1a;按下图所示进入&#xff0c;开始编辑业务逻辑下面给个例子using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; …

本地组策略编辑器无法打开(gpedit.msc命令异常)

一、本地组策略编辑器打开方式1、直接搜索打开&#xff08;1&#xff09;在搜索栏中直接输入以下内容进行搜索本地组策略编辑器&#xff08;2&#xff09;搜索到后直接点击打开即可&#xff08;但是一部分同志无法搜索到&#xff0c;搜索到的内容基本都是网页信息而非本地系统的…

kafka部署集群模式

Kafka部署&#xff08;3.7&#xff09; 生产环境推荐的kafka部署方式为operator方式部署&#xff0c;Strimzi是目前最主流的operator方案。集群数据量较小的话&#xff0c;可以采用NFS共享存储&#xff0c;数据量较大的话可使用local pv存储 部署operator operator部署方式为he…

C语言中级_动态内存分配、指针和常量、各种指针类型、指针和数组、函数指针

0、前言&#xff1a; 动态内存分配是一个重要概念&#xff0c;要和静态数组对比着学习&#xff1b;指针和数组搭配在一起&#xff0c;让指针理解的难度上了一个台阶&#xff0c;尤其是二维数组搭配指针&#xff0c;要获取数组的值&#xff0c;什么时候“取地址”&#xff0c;什…

单变量单步时序预测:CNN-GRU卷积神经网络结合门控循环单元

目录预测效果1. **CNN-GRU的基本原理**2. **应用场景**3. **模型结构与实现**4. **优势与挑战**5. **相关研究与实现**6. **未来发展方向**结论代码设计预测效果 CNN-GRU卷积神经网络结合门控循环单元是一种结合了卷积神经网络&#xff08;CNN&#xff09;和门控循环单元&#…

MonoFusion 与 Genie 3

卡内基梅隆大学的研究者发明了一种叫 MonoFusion 的新技术&#xff0c;它能用很少的普通相机&#xff08;比如4个&#xff09;&#xff0c;就能拍出像电影特效一样细腻流畅的动态3D场景&#xff08;4D重建&#xff09;&#xff0c;比如弹钢琴、修自行车这种复杂动作&#xff0c…

kubernets命令行创建Token并附加权限给dashboard控制台登录

1、创建登录token kubectl create token default -n graph-node-test dgjeojrgopejgeropjgpsdjgerjglsdjfsjogjeojgeorjgortlfhj4yu493460uwperg3wef;lsj2y3r934tnrhifrlfe9t4h5tlhobdrmlgw485tw4yp653ut9ogogjerolj4w9erjgotj3fgjletyj49yr20o359truyo5u6908430jt5grjsdtgj49…

什么是SpringBoot

题目详细答案Spring Boot 是由 Pivotal 团队提供的一个基于 Spring 框架的项目&#xff0c;它旨在简化 Spring 应用的开发和部署。Spring Boot 通过提供一系列的约定和开箱即用的功能&#xff0c;使得开发者可以更快地构建独立的、生产级的 Spring 应用程序&#xff0c;而无需进…

从零开始设计一个分布式KV存储:基于Raft的协程化实现

从零开始设计一个分布式KV存储&#xff1a;基于Raft的协程化实现 本文将以一个最小可运行的分布式KV系统为例&#xff0c;带你拆解如何用C、Raft算法和协程模型构建高可用的Key-Value存储。 一、为什么需要分布式KV&#xff1f; 单机KV&#xff08;如Redis&#xff09;存在单点…

虚拟机或docker的ubuntu无界面安装完成后镜像源设置

ubuntu系统源 在装好虚拟机或者docker镜像后&#xff0c;直接使用apt update && apt upgrade是无法完更新的。 此时系统中也没有vim工具&#xff0c;我们可以在清华源的网站中找到帮助文档。mirrors.tuna.tsinghua.edu.cn/help/ubuntu/为了避免冲突&#xff0c;我们使用…

串口通信02 温度传感DS18B20 01 day49

九&#xff1a;串口通信 通信&#xff1a;无线和有线 ​ 单工 半双工 全双工 并行&#xff1a;多个数据线 串行&#xff1a;一根数据线 同步&#xff1a;通信双方使用同一个时钟&#xff0c;SPI信息帧&#xff0c;有CLK引脚 异步&#xff1a;通信双方使用不同时钟&#xff0c;双…

【FreeRTOS 】任务通知

FreeRTOS 任务通知任务通知简介一 、发送通知1.1 xTaskNotify()1.2 xTaskNotifyFromISR()1.3 xTaskNotifyGive()1.4 xTaskNotifyAndQuery()1.5 xTaskNotifyAndQueryFromISR()二、接收通知2.1 ulTaskNotifyTake()2.2 xTaskNotifyWait()三、清除通知状态和值3.1 xTaskNotifyState…

Android视图状态以及重绘

一、视图状态&#xff08;View States&#xff09;1. 五种核心状态状态作用修改方法特点enabled视图是否响应交互setEnabled(boolean)禁用状态下不响应onTouch事件focused视图是否获得焦点requestFocus()需同时满足focusable和focusableInTouchModewindow_focused视图所在窗口是…

vue3接收SSE流数据进行实时渲染日志

后端使用的是 Spring Boot WebFlux&#xff08;响应式编程框架&#xff09;&#xff0c;并且返回的是 Server-Sent Events (SSE) 流式数据&#xff0c;那么在 Vue3 中&#xff0c;需要使用 EventSource API 或 fetch 流式读取 来正确获取响应内容。方案 1&#xff1a;使用 Eve…

每日五个pyecharts可视化图表-bars(6)

探索pyecharts库中条形图的高级用法与定制技巧 在数据可视化中&#xff0c;条形图是最常用的图表类型之一&#xff0c;它能够清晰地展示不同类别之间的数量对比。今天&#xff0c;我们将继续学习如何使用pyecharts创建5种不同风格的条形图。pyecahts源码 图表1&#xff1a;带…

【C语言】文件操作全解析

文章目录一、为什么需要文件操作&#xff1f;二、认识文件&#xff1a;不止是磁盘上的存储2.1 程序文件2.2 数据文件2.3 文件名的构成三、文本文件与二进制文件&#xff1a;数据的两种形态3.1 存储方式差异3.2 实例对比&#xff1a;整数10000的存储3.3 二进制文件操作示例四、文…

C结构体的几种定义形式 + typedef结合使用的好处

struct 语句定义了一个包含多个成员的新的数据类型&#xff0c;struct 语句的格式如下&#xff1a; struct tag { member-list member-list member-list ... } variable-…

SPICE电容矩阵

SPICE电容矩阵: 如果有许多条传输线,就可以用下标来标记每一条线。例如,如果有5条线,就用1~5分别标记,依惯例把返回路径导体标记为导线0。图10.6给出了5条导线和一个公共返回平面的横截面图。首先研究电容器元件,下一节再讨论电感器元件。 在这个线的集合中,每对导线之间…