我无法更改redis中的spring会话ttl数据

问题描述 投票:0回答:1
    @RequestMapping("/loginByEmail")
    @GlobalInterceptor(checkParams = true)
    public ResponseVO loginByEmail(HttpSession httpSession,
                            HttpServletRequest request,
                            @VerifyParam(required = true, param = "email") String email,
                            @VerifyParam(required = true, param = "password") String password,
                            @VerifyParam(required = true, param = "checkCode") String checkCode) {


        try {
            
//            if (!checkCode.equalsIgnoreCase((String) httpSession.getAttribute(CHECK_CODE_KEY))) {
//                throw new BusinessException("wrong code");
//            }

          
            String token = loginService.loginByEmail(email, password, getIpAddress(request));
            httpSession.setAttribute(Constants.SESSION_KEY, token);


            
            return getSuccessResponseVO(token);

        } finally {
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(new StringRedisSerializer());
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashValueSerializer(new StringRedisSerializer());
            redisTemplate.opsForValue().set("2","2");
            redisTemplate.expire("2", 1, TimeUnit.DAYS);
            System.out.println(RedisPrefixConstants.SPRING_SESSION_SESSIONS + httpSession.getId());
            httpSession.removeAttribute(CHECK_CODE_KEY);
        }

    }

我尝试测试是否可以在redis中存储数据并更改它的ttl并且我成功了,但是当我更改spring会话在redis中存储的数据时,我无法更改它。密钥肯定是正确的,因为我已经比较过。我不知道为什么我不能改变它。

spring spring-boot spring-data-redis spring-session
1个回答
0
投票

好吧,我得到了答案。 这里是: Spring会话中数据的TTL不能通过redisTemplate.expire()方法修改,因为spring会话中的TTL是由其自身的maxInactiveInterval控制的。所以如果你想改变他的时间,就需要直接改变session过期时间来控制他的TTL。

您可以使用:

httpSession.setMaxInactiveInterval(300000); // 3000000 in seconds
© www.soinside.com 2019 - 2024. All rights reserved.