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