SpringBoot实现Redis失效监听事件——KeyExpirationEventMessageListener

这篇说一下通过集成KeyExpirationEventMessageListener类实现redis失效监听事件。
首先还是SpringBoot和 Redis的集成,不会的自行百度吧。

步骤:

  1. 首先加入依赖
org.springframework.boot spring-boot-starter-data-redis 1 2 3 4 在可以正常连接Redis存取数据之后,创建监听类KeyExpiredListener,集成KeyExpirationEventMessageListener类,重写onMessage方法。 这说明一下:在key失效之后,会出发onMessage方法。 在这里,我只是获取redis中失效的key值。 注意:只能获取失效的key值,不能获取key对应的value值。代码中的输出语句为null,可以证明。 如何解决在上一篇文章里有——点这里 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;

@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) {
    String key = new String(message.getBody(), StandardCharsets.UTF_8);
    String myKey = redisTemplate.opsForValue().get("myKey");
    System.out.println(myKey);
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
创建一个配置类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.RedisMessageListenerContainer;

@Configuration
public class RedisListenerConfig {
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {

    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    return container;
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
这样就搞定了,下面写一个Controller测试一下:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class TestController {

@Autowired
public RedisTemplate<String,String> redisTemplate;

@RequestMapping(value = "/redisTest", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public Map<String,Object> redisTest(){
    redisTemplate.opsForValue().set("myKey", "myValue",100, TimeUnit.SECONDS);
    String myKey = redisTemplate.opsForValue().get("myKey");
    System.out.println(myKey);
    Map<String,Object> resultMap = new HashMap<String,Object>();
    resultMap.put("myKey",myKey);
    return resultMap;
}

}
————————————————
版权声明:本文为CSDN博主「oeSeven」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/oeSeven/article/details/106359816

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值