欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

redis 对过期key监听处理

程序员文章站 2022-08-10 22:47:34
/** * redis事件监听 * @author YOLO * @date 2020-11-26 * @param * @return*/@Configurationpublic class RedisListenerConfig { @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMe...
/**
    * redis事件监听
    * @author YOLO
    * @date 2020-11-26
    * @param
    * @return
*/
@Configuration
public class RedisListenerConfig {
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }
}
package com.github.wxiaoqi.security.yibum.listener;

import com.github.wxiaoqi.security.common.util.DateUtil;
import com.github.wxiaoqi.security.yibum.entity.Activity;
import com.github.wxiaoqi.security.yibum.mapper.ActivityMapper;
import com.github.wxiaoqi.security.yibum.mapper.YsBrandsApplyMapper;
import com.github.wxiaoqi.security.yibum.util.SendWxTemplateMessage;
import com.github.wxiaoqi.security.yibum.vo.WxMessageVo;
import com.github.wxiaoqi.security.yibum.vo.WxTemplateMessageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * reids过期监听器
 *
 * @param
 * @author YOLO
 * @date 2020-11-26
 * @return
 */
@Component
@Slf4j
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Autowired
    private YsBrandsApplyMapper ysBrandsApplyMapper;

    @Autowired
    private ActivityMapper activityMapper;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    /**
     * redis数据失效事件,进行数据处理
     *
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        // message.toString()------获取失效的key
        String expiredKey = message.toString();
        log.info("===========>key失效:" + expiredKey);      


    }
}

redis 配置的 notify-keyspace-events Ex打开

本文地址:https://blog.csdn.net/qq_28687183/article/details/110195677

相关标签: JAVA redis