parent
e55d98a36b
commit
cd68e6166a
5 changed files with 81 additions and 11 deletions
@ -0,0 +1,37 @@ |
||||
package org.springblade.system.user.util; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
import org.springblade.system.user.entity.Train; |
||||
import org.springblade.system.user.service.ITrainService; |
||||
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; |
||||
|
||||
/** |
||||
* redis监听器,监听key过期事件并处理 |
||||
* @author ytl |
||||
* @since 2022-10-09 16:49 |
||||
*/ |
||||
@Component |
||||
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener { |
||||
@Autowired |
||||
private ITrainService trainService; |
||||
|
||||
public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { |
||||
super(listenerContainer); |
||||
} |
||||
|
||||
@Override |
||||
public void onMessage(Message message, byte[] pattern) { |
||||
// 获取到失效的 key,进行取消订单业务处理
|
||||
String expiredKey = message.toString(); |
||||
System.out.println("缓存过期的key为:" + expiredKey); |
||||
LambdaQueryWrapper<Train> queryWrapper = new LambdaQueryWrapper<>(); |
||||
queryWrapper.eq(Train::getId,expiredKey); |
||||
Train train = trainService.getOne(queryWrapper); |
||||
System.out.println(train.getName()); |
||||
//这里可以根据培训的id查询到培训的具体信息,然后再实现通知功能
|
||||
} |
||||
} |
||||
@ -0,0 +1,21 @@ |
||||
package org.springblade.system.user.util; |
||||
|
||||
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; |
||||
|
||||
/** |
||||
* 配置实现监听 Redis key 过期时间 |
||||
* @author ytl |
||||
* @since 2022-10-09 16:47 |
||||
*/ |
||||
@Configuration |
||||
public class RedisListenerConfig { |
||||
@Bean |
||||
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory){ |
||||
RedisMessageListenerContainer container =new RedisMessageListenerContainer(); |
||||
container.setConnectionFactory(connectionFactory); |
||||
return container; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue