java 实现 redis存储定时数据失效后的事件发布订阅

存储数据, 设置数据超时时间

public void sendMsg(){
	stringRedisTemplate.opsForValue().set("test1", "abc");
    stringRedisTemplate.expire("test1", 30, TimeUnit.SECONDS);
}

自定义事件监听器

package com.example.demo.conf;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @program: demo
 * @description:
 * @author: zcz
 * @create: 2019-02-15 15:39
 **/

@Component
public class RedisMessageListener implements MessageListener {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public StringRedisTemplate getStringRedisTemplate() {
        return stringRedisTemplate;
    }

    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Override
    public void onMessage(Message message, byte[] bytes) {
        byte[] body = message.getBody();
        String msgBody = (String) stringRedisTemplate.getValueSerializer().deserialize(body);
        System.out.println(msgBody);
        byte[] channel = message.getChannel();
        String msgChannel = (String) stringRedisTemplate.getValueSerializer().deserialize(channel);
        System.out.println(msgChannel);
        String msgPattern = new String(bytes);
        System.out.println(msgPattern);
    }
}

创建事件监听器容器, 配置监听类型为__keyevent@*__:expired

@ConditionalOnBean(RedisMessageListener.class)
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(JedisConnectionFactory factory,  RedisMessageListener redisMessageListener) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(10);
        scheduler.initialize();
        container.setTaskExecutor(scheduler);
        List topics = new ArrayList<>();
        topics.add(new PatternTopic("__keyevent@*__:expired"));
        Map> map = new HashMap<>();
        map.put(redisMessageListener, topics);
        container.setMessageListeners(map);
        return container;
    }

配置redis.config配置文件发布事件类型

notify-keyspace-events Ex

#K 键空间通知,以__keyspace@__为前缀

#E 键事件通知,以__keysevent@__为前缀

#g del , expipre , rename 等类型无关的通用命令的通知, …

#$ String命令

#l List命令

#s Set命令

#h Hash命令

#z 有序集合命令

#x 过期事件(每次key过期时生成)

#e 驱逐事件(当key在内存满了被清除时生成)

#A g$lshzxe的别名,因此”AKE”意味着所有的事件


#K Keyspace events, published with keyspace@ prefix.

#E Keyevent events, published with keyevent@ 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)

#A Alias for g$lshzxe, so that the “AKE” string means all the events.

你可能感兴趣的:(redis)