在實際的開發項目中,監聽 key 的過期事件,應用非常廣泛,例如:優惠券過期,處理各種超時事件等等
先貼出SpringBoot工程結構

實現步驟:
1.修改Redis配置文件
找到 redis.windows.conf或redis.conf 文件,搜索 “notify-keyspace-events”找到原本的notify-keyspace-events " ",修改為 “notify-keyspace-events Ex”,這樣我們的 Redis 就支持 key 過期事件的監聽了

2.添加依賴
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
3.配置yml
spring: application: name: Redis redis: host: 127.0.0.1 port: 6379
4.創建監聽類
package com.zk.redis.redisTimeoutEvent; 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; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.TimeUnit; @Slf4j @Component public class KeyExpiredListener extends KeyExpirationEventMessageListener { @Autowired public RedisTemplate<String,String> redisTemplate; public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); } @Override public void onMessage(Message message, byte[] bytes) {
//獲取失效key名稱 String expireKey = new String(message.getBody(), StandardCharsets.UTF_8);
//獲取key原本的value 獲取不到 是null String expireKeyValue = redisTemplate.opsForValue().get("myKey"); log.info("expireKey---"+expireKey); log.info("expireKeyValue---"+expireKeyValue); } }
5.創建一個配置類RedisListenerConfig
package com.zk.redis.redisTimeoutEvent; 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; @Configuration public class RedisListenerConfig { @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); return container; } }
6.寫一個Controller測試一下
package com.zk.redis.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; @RestController public class RedisController { @Autowired public RedisTemplate<String,String> redisTemplate; @RequestMapping(value = "/redisTest", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") public Map<String,Object> redisTest(){
//redis中存入5秒失效的key redisTemplate.opsForValue().set("myKey", "myValue",5, TimeUnit.SECONDS); String myKey = redisTemplate.opsForValue().get("myKey"); Map<String,Object> resultMap = new HashMap<String,Object>(); resultMap.put("myKey",myKey); return resultMap; } }
7.項目運行后,瀏覽器輸入訪問地址,輸出結果,說明key失效后觸發onMessage().

上圖說明成功監聽並觸發事件.
