redis key过期 业务处理

This commit is contained in:
2024-03-05 09:41:41 +08:00
parent 48f43f7c4d
commit c6103ca00f
10 changed files with 112 additions and 55 deletions

View File

@@ -0,0 +1,55 @@
package cn.ysk.cashier.config;
import cn.ysk.cashier.service.shop.TbShopInfoService;
import cn.ysk.cashier.system.service.UserService;
import cn.ysk.cashier.utils.CacheKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
@Slf4j
@Configuration
public class RedisKeyExpirationListener implements MessageListener {
@Lazy
@Autowired
private TbShopInfoService shopInfoService;
@Lazy
@Autowired
private UserService userServiceu;
//redis key失效监听
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
// 监听特定键的过期事件
container.addMessageListener(this, new PatternTopic("__keyevent@0__:expired"));
return container;
}
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public void onMessage(Message message, byte[] pattern) {
String expiredKey = new String(message.getBody());
// 检查过期的键是否以特定前缀开头
if(expiredKey.startsWith(CacheKey.ACT_CODE)) {
String account = expiredKey.substring("act_code:".length());
log.info("商户到期 账户名为:{}",account);
shopInfoService.update(account);
userServiceu.upEnableByusername(account);
}
}
}