• 使用Redis Key失效事件实现定时任务


    修改 redis 相关事件配置

    • 找到 redis 配置文件 redis.conf,查看 notify-keyspace-events 配置项,如果没有,添加 notify-keyspace-events Ex,如果有值,则追加 Ex,相关参数说明如下:
    K:keyspace 事件,事件以 keyspace@ 为前缀进行发布
    E:keyevent 事件,事件以 keyevent@ 为前缀进行发布
    g:一般性的,非特定类型的命令,比如del,expire,rename等
    $:字符串特定命令
    l:列表特定命令
    s:集合特定命令
    h:哈希特定命令
    z:有序集合特定命令
    x:过期事件,当某个键过期并删除时会产生该事件
    e:驱逐事件,当某个键因 maxmemore 策略而被删除时,产生该事件
    A:g$lshzxe的别名,因此”AKE”意味着所有事件
    

    引入依赖

    • 在 pom.xml 中添加 org.springframework.boot:spring-boot-starter-data-redis 依赖
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    

    代码编写

    • 定义配置 RedisListenerConfig 实现监听 Redis key 过期时间
    package com.format.common.config;
    
    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.RedisMessageListenerContainer;
    
    /**
     * @Description: redis 监听
     * @Author Format
     * @Date 2020/11/10 14:26
     * @Version V1.0
     */
    
    @Configuration
    public class RedisListenerConfig {
    
        @Bean
        RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
            RedisMessageListenerContainer container = new RedisMessageListenerContainer();
            container.setConnectionFactory(connectionFactory);
            return container;
        }
    
    
    }
    
    
    • 定义监听器 RedisKeyExpirationListener,实现KeyExpirationEventMessageListener 接口,查看源码发现,该接口监听所有 db 的过期事件 keyevent@*:expired"
    package com.format.common.handler;
    
    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;
    
    /**
     * @Description: 监听所有db的过期事件__keyevent@*__:expired"
     * @Author Format
     * @Date 2020/11/10 14:32
     * @Version V1.0
     */
    
    @Component
    public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
    
        public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
            super(listenerContainer);
        }
    
        /**
         * 针对 redis 数据失效事件,进行数据处理
         * @param message
         * @param pattern
         */
        @Override
        public void onMessage(Message message, byte[] pattern) {
    
            // 获取到失效的 key,进行取消订单业务处理
            String expiredKey = message.toString();
            System.out.println(expiredKey);
        }
    
    }
    
    
  • 相关阅读:
    C#单例模式的三种写法转载
    silverlight 添加配置项
    oracle 如何实现上一条、下一条、查找不连续的值
    一个IT民工眼中的保障房不能保证公平,赞成取消保障房
    c# where 转载
    进度条 silverlight
    中国软件公司我深表认同:软硬结合
    计算经纬度两点之间的距离(c#)
    如何高效使用SQLITE .NET (C#)
    如何判断系统是否安装了flash插件
  • 原文地址:https://www.cnblogs.com/format-ch/p/14862148.html
Copyright © 2020-2023  润新知