1. RedisKeys 类
bash
package com.cqcloud.platform.constants;
/**
* @author weimeilayer@gmail.com ✨
* @date 💓💕 2024年10月21日 🐬🐇 💓💕
*/
public class RedisKeys {
/**
* redis文件夹
*/
public static final String COLON = ":";
/**
* 系统公共参数
*/
public static final String SYS_PUBLIC_PARAM_LIST = "SysPublicParamList";
/**
* 行政区划(根据PID)
*/
public static final String DIC_CITY_INFO = "DicCityInfo";
/**
* 行政区划(根据全程)
*/
public static final String DIC_CITY_INFO_FOR_NAME = "DicCityInfoForName";
/**
* 设备在线状态
*/
public static final String DEVICE_ONLINE_STATUS = "DeviceOnlineStatus";
/**
* 微信开放平台的唯一标识
*/
public static final String WX_UNION_ID = "WxUnionId";
/**
* 微信开放平台的唯一标识(刷新)
*/
public static final String WX_UNION_ID_REFRESH = "WxUnionIdRefresh";
}
2. KeyExpiredListener 类
bash
package com.cqcloud.platform.config;
import com.cqcloud.platform.constants.RedisKeys;
import lombok.extern.slf4j.Slf4j;
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 weimeilayer@gmail.com ✨
* @date 💓💕 2024年9月28日 🐬🐇 💓💕
*/
@Slf4j
@Component
public class KeyExpiredListener extends KeyExpirationEventMessageListener {
public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}
@Override
public void onMessage(Message message) {
String expiredKey = message.toString();
log.info("Expired key: {}", expiredKey);
if (expiredKey.startsWith(RedisKeys.DEVICE_ALARM_RECORD + RedisKeys.COLON)) {
String id = expiredKey.substring((RedisKeys.DEVICE_ALARM_RECORD + RedisKeys.COLON).length());
log.info("Alarm record expired: {}", id);
// 这里可以注入服务并处理过期逻辑
}
if (expiredKey.startsWith(RedisKeys.WX_UNION_ID_REFRESH + RedisKeys.COLON)) {
String unionId = expiredKey.substring((RedisKeys.WX_UNION_ID_REFRESH + RedisKeys.COLON).length());
log.info("WX Union ID refreshed: {}", unionId);
// 这里可以注入服务并处理过期逻辑
}
}
}
3. RedisConfiguration 类
/**
- redis配置类
- @author weimeilayer@gmail.com ✨
- @date 💓💕 2024年9月28日 🐬🐇 💓💕
*/
bash
package com.cqcloud.platform.config;
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 RedisConfiguration {
private final RedisConnectionFactory redisConnectionFactory;
public RedisConfiguration(RedisConnectionFactory redisConnectionFactory) {
this.redisConnectionFactory = redisConnectionFactory;
}
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer() {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(redisConnectionFactory);
return container;
}
@Bean
public KeyExpiredListener keyExpiredListener() {
return new KeyExpiredListener(redisMessageListenerContainer());
}
}