redis失效key监听器KeyExpirationEventMessageListener

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值