Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Redis 在spring引导中使用OPSFervalue增加值时,是否可以设置过期时间_Redis - Fatal编程技术网

Redis 在spring引导中使用OPSFervalue增加值时,是否可以设置过期时间

Redis 在spring引导中使用OPSFervalue增加值时,是否可以设置过期时间,redis,Redis,现在,我使用以下代码在spring boot中增加一个值: String loginFailedKey = "admin-login-failed:" + request.getPhone(); Object loginFailedCount = loginFailedTemplate.opsForValue().get(loginFailedKey); if (loginFailedCount != null && Inte

现在,我使用以下代码在spring boot中增加一个值:

 String loginFailedKey = "admin-login-failed:" + request.getPhone();
        Object loginFailedCount = loginFailedTemplate.opsForValue().get(loginFailedKey);
        if (loginFailedCount != null && Integer.valueOf(loginFailedCount.toString()) > 3) {
            throw PostException.REACH_MAX_RETRIES_EXCEPTION;
        }
        List<Users> users = userService.list(request);
        if (CollectionUtils.isEmpty(users)) {
            loginFailedTemplate.opsForValue().increment(loginFailedKey, 1);
            throw PostException.LOGIN_INFO_NOT_MATCH_EXCEPTION;
        }
String loginFailedKey=“管理员登录失败:”+request.getPhone();
对象loginFailedCount=loginFailedTemplate.opsForValue().get(loginFailedKey);
if(loginFailedCount!=null&&Integer.valueOf(loginFailedCount.toString())>3){
抛出PostException.REACH\u MAX\u重试\u异常;
}
List users=userService.List(请求);
if(CollectionUtils.isEmpty(用户)){
loginFailedTemplate.opsForValue().increment(loginFailedKey,1);
抛出PostException.LOGIN\u INFO\u NOT\u MATCH\u异常;
}

增加密钥时是否可以设置过期时间?如果出现新的增量命令,请更新过期时间。我阅读了文档,没有找到工具。

在Spring Boot中没有直接的方法

间接方法之一是使用LUA srcipt

例如:

RedisScript script = RedisScript.of(
        "local i = redis.call('INCRBY', KEYS[1], ARGV[1])"
        + " redis.call('EXPIRE', KEYS[1], ARGV[2])"
        + " return i");

redisTemplate.execute(script, key, String.valueOf(increment),
        String.valueOf(expiration));