当前位置: 首页 > news >正文

Redis key 过期监听实现

1.技术背景,想知道 redis 设置了TTL时间的key 过期,且有后续的业务处理的场景可以使用。
bug点:
使用redis 缓存失效监听会有一定的延迟, 过期事件是在redis服务器删除键的时候生成的,而不是在理论上生存时间到达0值得时候生成的。

搭建:
1.前置条件为 redis服务器端开启了事件通知。配置文件redis.conf文件中 开启相关配置。
默认情况下redis是未开启事件通知的。

############################# EVENT NOTIFICATION ############################### Redis can notify Pub/Sub clients about events happening in the key space.
# This feature is documented at https://redis.io/topics/notifications
#
# For instance if keyspace events notification is enabled, and a client
# performs a DEL operation on key "foo" stored in the Database 0, two
# messages will be published via Pub/Sub:
#
# PUBLISH __keyspace@0__:foo del
# PUBLISH __keyevent@0__:del foo
#
# It is possible to select the events that Redis will notify among a set
# of classes. Every class is identified by a single character:
#
#  K     Keyspace events, published with __keyspace@<db>__ prefix.
#  E     Keyevent events, published with __keyevent@<db>__ prefix.
#  g     Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
#  $     String commands
#  l     List commands
#  s     Set commands
#  h     Hash commands
#  z     Sorted set commands
#  x     Expired events (events generated every time a key expires)
#  e     Evicted events (events generated when a key is evicted for maxmemory)
#  t     Stream commands
#  d     Module key type events
#  m     Key-miss events (Note: It is not included in the 'A' class)
#  A     Alias for g$lshzxetd, so that the "AKE" string means all the events
#        (Except key-miss events which are excluded from 'A' due to their
#         unique nature).
#
#  The "notify-keyspace-events" takes as argument a string that is composed
#  of zero or multiple characters. The empty string means that notifications
#  are disabled.
#
#  By default all notifications are disabled because most users don't need
#  this feature and the feature has some overhead. Note that if you don't
#  specify at least one of K or E, no events will be delivered.
#  notify-keyspace-events ""
# 配置文件修改项
notify-keyspace-events "Ex" 

可直接修改 notify-keyspace-events “Ex” 即为开启了rediss 事件监听,然后重启redis 服务(或者使用命令的形式,不需要重启redis服务)。不同key 类型的监听见上文档。

2.项目中编写相关的监听程序(前置条件,redis已经集成进项目里了)

import com.jinyi.up.user.listener.RedisKeyExpirationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
/*** spring容器 里通过 java配置方式注入RedisMessageListenerContainer 和  KeyExpirationEventMessageListener的实现类* @author jinyi* @date 2023/11/25 16:39* @desc*/
@Configuration
public class RedisListenerConfig {@BeanRedisMessageListenerContainer listenerContainer(RedisConnectionFactory connectionFactory) {RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();listenerContainer.setConnectionFactory(connectionFactory);return listenerContainer;}@BeanKeyExpirationEventMessageListener redisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {return new RedisKeyExpirationListener(listenerContainer);}}
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;/*** 使用redis 缓存失效监听会有一定的延迟, 过期事件是在redis服务器删除键的时候生成的,而不是在理论上生存时间到达0值得时候生成的,** @author jinyi* @date 2023/11/25 16:40* @desc*/
@Slf4j
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {super(listenerContainer);}/*** 针对redis数据失效事件,进行数据处理** @param message key的信息,并不包含缓存值。* @param pattern*/@Overridepublic void onMessage(Message message, byte[] pattern) {//获得失效的keylog.info("KeyExpiration key:" + message);//todo 后续的业务处理}}

相关文章:

Redis key 过期监听实现

1.技术背景&#xff0c;想知道 redis 设置了TTL时间的key 过期&#xff0c;且有后续的业务处理的场景可以使用。 bug点&#xff1a; 使用redis 缓存失效监听会有一定的延迟&#xff0c; 过期事件是在redis服务器删除键的时候生成的&#xff0c;而不是在理论上生存时间到达0值得…...

Gee教程2.上下文Context

先来看看Gin框架的简单例子 func main() {engine : gin.Default()engine.GET("/", func(c *gin.Context) {c.String(http.StatusOK, "hello World!")})//监听并启动服务&#xff0c;默认 http://localhost:8080/engine.Run() }//我们自己写的 func main()…...

【从浅识到熟知Linux】基本指定之cat、more和less

&#x1f388;归属专栏&#xff1a;从浅学到熟知Linux &#x1f697;个人主页&#xff1a;Jammingpro &#x1f41f;每日一句&#xff1a;写完这篇我要去吃晚饭啦&#xff01;&#xff01; 文章前言&#xff1a;本文介绍cat、more和less指令三种查看文件的用法并给出示例和截图…...

2018年7月24日 Go生态洞察:Go Cloud实现便携式云编程

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页——&#x1f405;&#x1f43e;猫头虎的博客&#x1f390; &#x1f433; 《面试题大全专栏》 &#x1f995; 文章图文…...

storyBook常见踩坑报错 和 解决

用StoryBook官网的代码&#xff0c;但报错&#xff0c;Unexpected token’<’ 在js文件中// Button.stories.js|jsx import { Button } from ‘./Button’; export default { component: Button, }; /* *&#x1f447; Render functions are a framework specific featur…...

python 笔记 根据用户轨迹+基站位置,估计基站轨迹+RSRP

1 问题描述 已知用户实际的轨迹&#xff0c;和基站的位置&#xff0c;能不能得到用户所连接的基站&#xff0c;以及基站的信号强度RSRP&#xff1f; 1.1 几个假设 这里我们做几个假设&#xff1a; 每个用户有80%的概率连接最近的基站&#xff0c;有20%的概率选择其他的基站连…...

RocketMQ 安装部署及应用场景记录

文章目录 前言一、RocketMQ简介1.1 整体架构 二、RocketMQ安装部署2.1 RocketMQ 下载2.2 修改 JVM 参数2.3 启动 NameServer 和 Broker2.4 验证发送和接受消息2.5 停止 NameServer 和 Broker2.6 配置全局环境 三、RocketMQ应用场景3.1 异步处理3.2 应用解耦3.3 流量削峰 前言 …...

人工智能面面观

人工智能简介 人工智能&#xff08;Artificial Intelligence&#xff0c;简称AI&#xff09;是一门研究如何使计算机能够模拟和执行人类智能任务的科学和技术领域。它致力于开发能够感知、理解、学习、推理、决策和与人类进行交互的智能系统。人工智能的背景可以追溯到上世纪50…...

vue-router的使用技巧

一、安装 npm install vue-router 二、引入 main.ts引入 import { createApp } from vue import App from ./App.vue import router from ./routerconst app createApp(App)app.use(router) app.mount(#app)三、定义路由文件 路由的参数 meta添加路由的其他参数 redire…...

CV计算机视觉每日开源代码Paper with code速览-2023.11.21

点击CV计算机视觉&#xff0c;关注更多CV干货 论文已打包&#xff0c;点击进入—>下载界面 点击加入—>CV计算机视觉交流群 1.【基础网络架构&#xff1a;Transformer】Multi-entity Video Transformers for Fine-Grained Video Representation Learning 论文地址&…...

人工智能对当代生活的影响

人工智能&#xff08;AI&#xff09;是指通过模拟人类智能的方式&#xff0c;使机器能够执行某些需要智能的任务。随着技术的快速发展和应用的广泛推广&#xff0c;人工智能已经深入到我们的日常生活中&#xff0c;对我们的生活和社会产生了深远的影响。本文将探讨人工智能对当…...

笔记:如何搭建一套前端监控系统?(持续更新中)

数据敏感处理 数据加密&#xff0c;对涉及用户隐私的数据做到加密防护 独立部署&#xff0c;不和其它应用共享监控系统 不采集具体数据&#xff0c;只采集用户操作数据 错误采集 Runtime Error: JS运行错误&#xff0c;可通过error监听器捕获 load Error: 资源加载错误&#x…...

在 Ubuntu 上安装最新版的 Calibre

目录 前言 方法1&#xff1a;从 Ubuntu 的仓库安装 Calibre 卸载 Calibre 方法2&#xff1a;获取最新版本的 Calibre 卸载 Calibre 结语 前言 Calibre 是一款自由开源的电子书软件。下面介绍如何在 Ubuntu Linux 上安装它。 作为电子书管理的瑞士军刀&#xff0c;Calibre …...

docker基础学习笔记

文章目录 Docker简介Linux下安装DockerDocker常用命令Docker网络Docker存储docker-composedockerfile制作镜像私有仓库镜像导入导出参考 Docker简介 定义&#xff1a;Docker是一个开源的应用容器引擎优势&#xff1a; 一键部署&#xff0c;开箱即用&#xff1a;容器使用基于im…...

Could not resolve all files for configuration ‘:app:androidJdkImage‘.

在使用./gradlew build编译项目时候遇到了该问题&#xff0c;整体错误如下: * What went wrong: Configuration cache state could not be cached: field generatedModuleFile of com.android.build.gradle.tasks.JdkImageInput bean found in field compilerArgumentProvider…...

GLP-1 , GLP-1R

-- 6VCB_GLP-1R G_protein, GLP-1 peptidea positive allosteric modulator...

【数据结构】F : 道路建设 (Ver. I)

F : 道路建设 (Ver. I) Description 有N个村庄&#xff0c;编号从1到N&#xff0c;你应该建造一些道路&#xff0c;使每个村庄都可以相互连接。 两个村A和B是相连的&#xff0c;当且仅当A和B之间有一条道路&#xff0c;或者存在一个村C使得在A和C之间有一条道路&#xff0c;并…...

flutter 无法从H5 WebView 访问摄像头和录音权限

AndroidManifest.xml需要在 中添加以下权限&#xff1a; <uses-permission android:name"android.permission.INTERNET"/> <uses-permission android:name"android.permission.CAMERA" /> <uses-permission android:name"android.per…...

electron27-react-mateos:基于electron+react18仿matePad桌面系统

基于Electron27React18ArcoDesign搭建桌面版OS管理系统。 electron-react-mateos 基于最新前端跨端技术栈electron27.xreact18arco-designzustand4sortablejs构建的一款仿制matePad界面多层级路由管理OS系统。 ElectronReactOS支持桌面多路由配置&#xff0c;新开窗口弹窗开启路…...

高精度算法总结

高精度加法 题目链接&#xff1a; https://www.acwing.com/activity/content/problem/content/825/ 代码模版&#xff1a; #include <iostream> #include <vector>using namespace std;// C A B vector<int> add(vector<int> &A, vector<…...

libz_dynamixel:轻量级Dynamixel协议嵌入式C实现

1. 项目概述libz_dynamixel是由嵌入式开发者 Liews Wuttipat 编写的轻量级 Dynamixel 协议实现库&#xff0c;专为资源受限的微控制器平台&#xff08;如 STM32F0/F1/F4、ESP32、nRF52 等&#xff09;设计。该库不依赖操作系统或标准 C 运行时&#xff0c;完全采用 C99 标准编写…...

Qwen3-0.6B-FP8保姆级部署指南:从零搭建你的AI对话机器人

Qwen3-0.6B-FP8保姆级部署指南&#xff1a;从零搭建你的AI对话机器人 1. 环境准备与快速部署 1.1 系统要求 在开始部署Qwen3-0.6B-FP8之前&#xff0c;请确保您的系统满足以下最低要求&#xff1a; 操作系统&#xff1a;Ubuntu 20.04/22.04或兼容的Linux发行版GPU&#xff…...

Qwen3.5-4B模型推理效果展示:复杂逻辑问题与代码生成案例

Qwen3.5-4B模型推理效果展示&#xff1a;复杂逻辑问题与代码生成案例 1. 开篇&#xff1a;当AI遇上复杂逻辑 最近测试了一款名为Qwen3.5-4B的模型&#xff0c;它在处理复杂逻辑和代码生成方面的表现着实让人眼前一亮。不同于常见的对话模型&#xff0c;这个经过蒸馏和强化训练…...

从相亲角到星辰大海:大白话拆解数学建模四大聚类算法

目录 1. 开篇&#xff1a;为什么我们需要聚类&#xff1f;&#xff08;无监督学习的魅力&#xff09; 2. 聚类算法的“四大门派”速览 3. 第一派&#xff1a;K-Means 算法&#xff08;“物以类聚”的极简美学&#xff09; 3.1 大白话原理&#xff1a;一场快递柜的选址博弈 …...

GLM-4.1V-9B-Base视觉能力深度评测:从图标识别到复杂图表理解

GLM-4.1V-9B-Base视觉能力深度评测&#xff1a;从图标识别到复杂图表理解 1. 开篇&#xff1a;当AI开始"看懂"图表 想象一下&#xff0c;你随手拍了一张公司季度报表的截图发给AI&#xff0c;它不仅能准确识别出里面的柱状图、折线图&#xff0c;还能告诉你哪个产品…...

别再踩坑了!SQL Server数据类型那点事儿,看懂这篇少背三个锅型

从0构建WAV文件&#xff1a;读懂计算机文件的本质 虽然接触计算机有一段时间了&#xff0c;但是我的视野一直局限于一个较小的范围之内&#xff0c;往往只能看到于算法竞赛相关的内容&#xff0c;计算机各种文件在我看来十分复杂&#xff0c;认为构建他们并能达到目的是一件困难…...

intv_ai_mk11镜像免配置:开箱即用网页界面+独立venv环境部署解析

intv_ai_mk11镜像免配置&#xff1a;开箱即用网页界面独立venv环境部署解析 1. 镜像概述与核心价值 intv_ai_mk11是一个基于Llama架构的中等规模文本生成模型&#xff0c;专为快速部署和便捷使用而设计。这个镜像的最大特点是实现了"开箱即用"的体验&#xff0c;用…...

tus-js-client错误处理与调试:构建稳定的文件上传系统

tus-js-client错误处理与调试&#xff1a;构建稳定的文件上传系统 【免费下载链接】tus-js-client A pure JavaScript client for the tus resumable upload protocol 项目地址: https://gitcode.com/gh_mirrors/tu/tus-js-client tus-js-client是一个纯JavaScript客户端…...

Alerta高可用部署方案:Docker、Kubernetes与云平台最佳实践

Alerta高可用部署方案&#xff1a;Docker、Kubernetes与云平台最佳实践 【免费下载链接】alerta Alerta monitoring system 项目地址: https://gitcode.com/gh_mirrors/al/alerta Alerta监控系统是一款功能强大的开源告警管理工具&#xff0c;能够帮助运维团队集中处理各…...

somo完全指南:从零开始配置你的网络监控系统

somo完全指南&#xff1a;从零开始配置你的网络监控系统 【免费下载链接】somo A human-friendly alternative to netstat for socket and port monitoring on Linux and macOS. 项目地址: https://gitcode.com/gh_mirrors/somo/somo somo是一款人性化的网络监控工具&am…...