Redis-监听过期key-JAVA实现方案

这篇具有很好参考价值的文章主要介绍了Redis-监听过期key-JAVA实现方案。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、创建监听配置类  RedisListenerConfig。



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;

@Configuration
public class RedisListenerConfig {

    @Bean
    RedisMessageListenerContainer listenerContainer(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();
        listenerContainer.setConnectionFactory(connectionFactory);
        return listenerContainer;
    }

    @Bean
    KeyExpirationEventMessageListener redisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        return new RedisKeyExpirationListener(listenerContainer);
    }

}

二、 创建监听器: RedisKeyExpirationListener 继承  KeyExpirationEventMessageListener .

这里只能获取到 过期的key,无法获取过期的value. 


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {


    @Autowired
    RedisTemplate redisTemplate ;

    /**
     * Creates new {@link } for {@code __keyevent@*__:expired} messages.
     *
     * @param listenerContainer must not be {@literal null}.
     */
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    /**
     * 针对redis数据失效事件,进行数据处理
     *
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        //获得失效的key
        System.out.println("onMessage pattern " + pattern + " " + " " + message);
        String channel = new String(message.getChannel());
        System.out.println(channel);
    }

}

三: 设置redis的配置文件redis.config中的  notify-keyspace-events 为 Ex 

四、验证一下:

          在redis客户端执行:setex localhost:1>key 5 value 设置一个过期的key

Redis-监听过期key-JAVA实现方案,Java,redis

          观察idea控制台:发现打印了过期的key,成功监听到过期的数据。

Redis-监听过期key-JAVA实现方案,Java,redis

参考博客:  文章来源地址https://www.toymoban.com/news/detail-682107.html

到了这里,关于Redis-监听过期key-JAVA实现方案的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 监听redis过期业务处理

    配置类: 过期监听类 

    2024年02月06日
    浏览(35)
  • Redis,过期监听

    应用场景,优惠卷过期,监听 配置类 监听类

    2024年02月14日
    浏览(27)
  • Redis中Key的过期策略

    1.立即删除:         在设置键的过期时间时,会创建一个回调事件,当过期时间达到时, 自动执行回调事件去删除键。 但是立即删除对 cpu 是最不友好的。 2.惰性删除:         惰性删除是指某个键值过期后,此键值不会马上被删除,而是加入到 删除字典( dict和e

    2024年02月15日
    浏览(25)
  • zookeeper监听集群节点的实现zkclient组件实现方案(Java版)

    ZooKeeper Watcher 机制 client 向zookeeper 注册监听 client注册的同时会存储一个WatchManager对象 向zookeeper发生改变则notification client 并发送一个WatchManager对象,然后client再更新该对象

    2024年04月24日
    浏览(30)
  • redis的key过期策略;主动/被动

    redis 可以设置 key 的过期时间,但这个 key 的过期时间不一定准确。 因为 redis 执行 key 过期的策略有 2种 : 主动(定时) 被动( get 的时候判断) redis 会给设置过期时间的 key 维护一个字典,然后定时轮询这个字典(每秒10次),随机抓取一批 key (20个),删除其中过期的

    2024年02月16日
    浏览(26)
  • Redis基本全局命令(含key过期策略)

    KEY 返回所有满⾜样式(pattern)的key。⽀持如下统配样式。 h?llo 匹配 hello , hallo 和 hxllo h*llo 匹配 hllo 和 heeeello h[ae]llo 匹配 hello 和 hallo 但不匹配 hillo h[^e]llo 匹配 hallo , hbllo ,…但不匹配 hello h[a-b]llo 匹配 hallo 和 hbllo 语法: 时间复杂度:O(N) 返回值:匹配pattern的所有key。 ⽰例

    2024年02月15日
    浏览(28)
  • 架构师成长之路Redis第三篇|Redis key过期清除策略

    maxmemory 100mb 当我们设置的内存达到指定的内存量时, 清除策略的 配置方式决定了默认行为。Redis可以为可能导致使用更多内存的命令返回错误,也可以在每次添加新数据时清除一些旧数据以返回到指定的限制。 当达到最大内存限制时,Redis所遵循的确切行为是使用MaxMemory-

    2024年02月10日
    浏览(29)
  • 【Java】三种方案实现 Redis 分布式锁

    setnx、Redisson、RedLock 都可以实现分布式锁,从易到难得排序为:setnx Redisson RedLock。一般情况下,直接使用 Redisson 就可以啦,有很多逻辑框架的作者都已经考虑到了。 1.1、简单实现 下面的锁实现可以用在测试或者简单场景,但是它存在以下问题,使其不适合用在正式环境。

    2024年02月05日
    浏览(35)
  • Redis(概述、应用场景、线程模式、数据持久化、数据一致、事务、集群、哨兵、key过期策略、缓存穿透、击穿、雪崩)

    目录 Redis概述 应用场景 Redis的线程模式 数据持久化 1.Rdb(Redis DataBase) 2.Aof(Append Only File) mysql与redis保持数据一致 redis事务 主从复制(Redis集群) 哨兵模式 key过期策略 缓存穿透、击穿、雪崩 1.缓存穿透:缓存中没有,在mysql中也没有 2.缓存击穿:数据在数据库中存在,某个

    2024年01月16日
    浏览(48)
  • springboot+redis+mysql+quartz-通过Java操作redis的KEYS*命令获取缓存数据定时更新数据库

    代码讲解: 3-点赞功能-定时持久化到数据库(pipeline+lua)-完善过程2_哔哩哔哩_bilibili https://www.bilibili.com/video/BV1w14y1o7BV 本文章代码: blogLike_schedule/like03 · xin麒/XinQiUtilsOrDemo - 码云 - 开源中国 (gitee.com) https://gitee.com/flowers-bloom-is-the-sea/XinQiUtilsOrDemo/tree/master/blogLike_schedule/like03 数据

    2024年02月15日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包