update 优化 RedisUtils 重构过期方法

2.X
疯狂的狮子Li 3 years ago
parent d61757a885
commit 7303af9621

@ -19,7 +19,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
import java.time.Duration;
/**
*
@ -55,7 +55,7 @@ public class UserActionListener implements SaTokenListener {
if (ObjectUtil.isNotNull(user.getDeptName())) {
userOnline.setDeptName(user.getDeptName());
}
RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, userOnline, tokenConfig.getTimeout(), TimeUnit.SECONDS);
RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, userOnline, Duration.ofSeconds(tokenConfig.getTimeout()));
log.info("user doLogin, useId:{}, token:{}", loginId, tokenValue);
} else if (userType == UserType.APP_USER) {
// app端 自行根据业务编写

@ -24,7 +24,7 @@ import com.ruoyi.system.api.model.XcxLoginUser;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
import java.time.Duration;
import java.util.function.Supplier;
/**
@ -156,7 +156,7 @@ public class SysLoginService {
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));
recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), errorLimitTime));
throw new UserException(loginType.getRetryLimitExceed(), errorLimitTime);
} else {

@ -22,9 +22,9 @@ import org.springframework.web.multipart.MultipartFile;
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)
@ -58,7 +58,7 @@ public class RepeatSubmitAspect {
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();

@ -6,11 +6,11 @@ import lombok.AccessLevel;
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;
/**
@ -107,7 +107,7 @@ public class RedisUtils {
} catch (Exception e) {
long timeToLive = bucket.remainTimeToLive();
bucket.set(value);
bucket.expire(timeToLive, TimeUnit.MILLISECONDS);
bucket.expire(Duration.ofMillis(timeToLive));
}
} else {
bucket.set(value);
@ -119,13 +119,12 @@ public class RedisUtils {
*
* @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);
}
/**
@ -149,20 +148,19 @@ public class RedisUtils {
* @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 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);
}
/**

@ -5,10 +5,10 @@ import cn.dev33.satoken.util.SaFoxUtil;
import com.ruoyi.common.redis.utils.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 )
@ -38,7 +38,7 @@ public class PlusSaTokenDao implements SaTokenDao {
if (timeout == SaTokenDao.NEVER_EXPIRE) {
RedisUtils.setCacheObject(key, value);
} 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;
}
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) {
RedisUtils.setCacheObject(key, object);
} 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;
}
RedisUtils.expire(key, timeout, TimeUnit.SECONDS);
RedisUtils.expire(key, Duration.ofSeconds(timeout));
}

@ -19,9 +19,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
*
@ -57,7 +57,7 @@ public class ValidateCodeServiceImpl implements ValidateCodeService {
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);

Loading…
Cancel
Save