update 优化 RedisUtils 重构过期方法
| | |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import java.time.Duration; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * 验证码操作处理 |
| | |
| | | captcha.setGenerator(codeGenerator); |
| | | captcha.createCode(); |
| | | String code = isMath ? getCodeResult(captcha.getCode()) : captcha.getCode(); |
| | | RedisUtils.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); |
| | | RedisUtils.setCacheObject(verifyKey, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION)); |
| | | ajax.put("uuid", uuid); |
| | | ajax.put("img", captcha.getImageBase64()); |
| | | return R.ok(ajax); |
| | |
| | | import lombok.NoArgsConstructor; |
| | | import org.redisson.api.*; |
| | | |
| | | import java.time.Duration; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.function.Consumer; |
| | | |
| | | /** |
| | |
| | | } catch (Exception e) { |
| | | long timeToLive = bucket.remainTimeToLive(); |
| | | bucket.set(value); |
| | | bucket.expire(timeToLive, TimeUnit.MILLISECONDS); |
| | | bucket.expire(Duration.ofMillis(timeToLive)); |
| | | } |
| | | } else { |
| | | bucket.set(value); |
| | |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param value 缓存的值 |
| | | * @param timeout 时间 |
| | | * @param timeUnit 时间颗粒度 |
| | | * @param duration 时间 |
| | | */ |
| | | public static <T> void setCacheObject(final String key, final T value, final long timeout, final TimeUnit timeUnit) { |
| | | public static <T> void setCacheObject(final String key, final T value, final Duration duration) { |
| | | RBucket<T> result = CLIENT.getBucket(key); |
| | | result.set(value); |
| | | result.expire(timeout, timeUnit); |
| | | result.expire(duration); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return true=设置成功;false=设置失败 |
| | | */ |
| | | public static boolean expire(final String key, final long timeout) { |
| | | return expire(key, timeout, TimeUnit.SECONDS); |
| | | return expire(key, Duration.ofSeconds(timeout)); |
| | | } |
| | | |
| | | /** |
| | | * 设置有效时间 |
| | | * |
| | | * @param key Redis键 |
| | | * @param timeout 超时时间 |
| | | * @param unit 时间单位 |
| | | * @param key Redis键 |
| | | * @param duration 超时时间 |
| | | * @return true=设置成功;false=设置失败 |
| | | */ |
| | | public static boolean expire(final String key, final long timeout, final TimeUnit unit) { |
| | | public static boolean expire(final String key, final Duration duration) { |
| | | RBucket rBucket = CLIENT.getBucket(key); |
| | | return rBucket.expire(timeout, unit); |
| | | return rBucket.expire(duration); |
| | | } |
| | | |
| | | /** |
| | |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.time.Duration; |
| | | |
| | | /** |
| | | * spring-cache 演示案例 |
| | |
| | | @GetMapping("/test6") |
| | | public R<Boolean> test6(String key, String value) { |
| | | RedisUtils.setCacheObject(key, value); |
| | | boolean flag = RedisUtils.expire(key, 10, TimeUnit.SECONDS); |
| | | boolean flag = RedisUtils.expire(key, Duration.ofSeconds(10)); |
| | | System.out.println("***********" + flag); |
| | | try { |
| | | Thread.sleep(11 * 1000); |
| | |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.time.Duration; |
| | | import java.util.Collection; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * 防止重复提交(参考美团GTIS防重系统) |
| | |
| | | String cacheRepeatKey = Constants.REPEAT_SUBMIT_KEY + url + submitKey; |
| | | String key = RedisUtils.getCacheObject(cacheRepeatKey); |
| | | if (key == null) { |
| | | RedisUtils.setCacheObject(cacheRepeatKey, "", interval, TimeUnit.MILLISECONDS); |
| | | RedisUtils.setCacheObject(cacheRepeatKey, "", Duration.ofMillis(interval)); |
| | | KEY_CACHE.set(cacheRepeatKey); |
| | | } else { |
| | | String message = repeatSubmit.message(); |
| | |
| | | * 安全模式,这里指定token通过Authorization头请求头传递 |
| | | */ |
| | | private List<SecurityScheme> securitySchemes() { |
| | | List<SecurityScheme> apiKeyList = new ArrayList<SecurityScheme>(); |
| | | List<SecurityScheme> apiKeyList = new ArrayList<>(); |
| | | String header = saTokenConfig.getTokenName(); |
| | | apiKeyList.add(new ApiKey(header, header, In.HEADER.toValue())); |
| | | return apiKeyList; |
| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.time.Duration; |
| | | |
| | | /** |
| | | * 用户行为 侦听器的实现 |
| | |
| | | dto.setTokenId(tokenValue); |
| | | dto.setUserName(user.getUsername()); |
| | | dto.setDeptName(user.getDeptName()); |
| | | RedisUtils.setCacheObject(Constants.ONLINE_TOKEN_KEY + tokenValue, dto, tokenConfig.getTimeout(), TimeUnit.SECONDS); |
| | | RedisUtils.setCacheObject(Constants.ONLINE_TOKEN_KEY + tokenValue, dto, Duration.ofSeconds(tokenConfig.getTimeout())); |
| | | log.info("user doLogin, userId:{}, token:{}", loginId, tokenValue); |
| | | } else if (userType == UserType.APP_USER) { |
| | | // app端 自行根据业务编写 |
| | |
| | | import com.ruoyi.common.utils.redis.RedisUtils; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.time.Duration; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * Sa-Token持久层接口(使用框架自带RedisUtils实现 协议统一) |
| | |
| | | if (timeout == SaTokenDao.NEVER_EXPIRE) { |
| | | RedisUtils.setCacheObject(key, value); |
| | | } else { |
| | | RedisUtils.setCacheObject(key, value, timeout, TimeUnit.SECONDS); |
| | | RedisUtils.setCacheObject(key, value, Duration.ofSeconds(timeout)); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | return; |
| | | } |
| | | RedisUtils.expire(key, timeout, TimeUnit.SECONDS); |
| | | RedisUtils.expire(key, Duration.ofSeconds(timeout)); |
| | | } |
| | | |
| | | |
| | |
| | | if (timeout == SaTokenDao.NEVER_EXPIRE) { |
| | | RedisUtils.setCacheObject(key, object); |
| | | } else { |
| | | RedisUtils.setCacheObject(key, object, timeout, TimeUnit.SECONDS); |
| | | RedisUtils.setCacheObject(key, object, Duration.ofSeconds(timeout)); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | return; |
| | | } |
| | | RedisUtils.expire(key, timeout, TimeUnit.SECONDS); |
| | | RedisUtils.expire(key, Duration.ofSeconds(timeout)); |
| | | } |
| | | |
| | | |
| | |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.time.Duration; |
| | | import java.util.List; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.function.Supplier; |
| | | |
| | | /** |
| | |
| | | errorNumber = ObjectUtil.isNull(errorNumber) ? 1 : errorNumber + 1; |
| | | // 达到规定错误次数 则锁定登录 |
| | | if (errorNumber.equals(setErrorNumber)) { |
| | | RedisUtils.setCacheObject(errorKey, errorNumber, errorLimitTime, TimeUnit.MINUTES); |
| | | RedisUtils.setCacheObject(errorKey, errorNumber, Duration.ofMinutes(errorLimitTime)); |
| | | asyncService.recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), errorLimitTime), request); |
| | | throw new UserException(loginType.getRetryLimitExceed(), errorLimitTime); |
| | | } else { |