1.redis失效key监听器KeyExpirationEventMessageListener
@Component
@Slf4j
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Resource
private SysUserDao sysUserDao;
public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer, RedisTemplate<String, Object> redisTemplate) {
super(listenerContainer);
}
@Override
public void onMessage(Message message, byte[] pattern) {
try {
log.info("获取失效redisKey");
// 获取失效的key
String key = message.toString();
log.info("失效redisKey:" + key);
//从失效key中筛选代表订单失效的key
if (StringUtils.isNotBlank(key) && !key.contains("R_S_APPIP")) {
String userIdIp = (String) redisTemplate.opsForValue().get(RedisKey.getUserAppIp(key));
log.info("失效userIdIp:" + userIdIp);
if (StringUtils.isNotBlank(userIdIp)) {
String userId = userIdIp.split(",")[0];
String ip = userIdIp.split(",")[1];
sysUserDao.updateAiLoginOutTime(Long.parseLong(userId), ip);
log.info("用户id为:" + userId + ",ip为:" + ip + "已登出!");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.注册rediskey失效监听器
@Configuration
public class RedisConfigurer {
// @Resource
// private ObjectMapper jacksonObjectMapper;
@Bean(name="redisTemplate")
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();
//objectMapper.registerModule(new ParameterNamesModule()).registerModule(new Jdk8Module()).registerModule(new JavaTimeModule());
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
//Redis监听容器
@Bean
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
// container.addMessageListener(listenerAdapter, new PatternTopic(“keyevent@0:expired”));
return container;
}
}
3.因为原有失效redis只能获取key,而不能获取失效key的value,所以新建redis保存所需的值
原有:redisTemplate.opsForValue().set(token, userId, 1, TimeUnit.HOURS);//保存userId
新建:redisTemplate.opsForValue().set(RedisKey.getUserAppIp(token), userId + “,” + IpUtils.getRealIp(request), 2, TimeUnit.HOURS);//改为2小时过期,有操作时会重置时间,保存userId和ip