opt: 内存缓存模式
This commit is contained in:
@@ -5,7 +5,6 @@ import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
@@ -36,8 +35,7 @@ public class AuthInterceptor implements HandlerInterceptor {
|
||||
if (!Files.exists(path)) {
|
||||
result = LicenseResult.requestResult(false, "授权码失败", null);
|
||||
} else {
|
||||
// 考虑存内存
|
||||
String authCode = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).trim();
|
||||
String authCode = verifier.getInstalledAuthCode();
|
||||
result = verifier.verify(authCode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,11 @@ package org.nl.core;
|
||||
import org.nl.util.RSAUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
@@ -18,6 +20,9 @@ public class LicenseVerifier {
|
||||
|
||||
private final LicenseProperties properties;
|
||||
|
||||
// 内存缓存授权码,避免频繁读取文件
|
||||
private volatile String cachedAuthCode;
|
||||
|
||||
public LicenseVerifier(RSAUtil rsaUtil, LicenseProperties properties) {
|
||||
this.rsaUtil = rsaUtil;
|
||||
this.properties = properties;
|
||||
@@ -56,12 +61,46 @@ public class LicenseVerifier {
|
||||
}
|
||||
Files.write(
|
||||
targetPath,
|
||||
(authCode == null ? "" : authCode).getBytes(java.nio.charset.StandardCharsets.UTF_8),
|
||||
java.nio.file.StandardOpenOption.CREATE,
|
||||
java.nio.file.StandardOpenOption.TRUNCATE_EXISTING
|
||||
(authCode == null ? "" : authCode).getBytes(StandardCharsets.UTF_8),
|
||||
StandardOpenOption.CREATE,
|
||||
StandardOpenOption.TRUNCATE_EXISTING
|
||||
);
|
||||
// 更新内存缓存
|
||||
cachedAuthCode = authCode == null ? "" : authCode.trim();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("写入授权码失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 懒加载读取已安装授权码,优先走内存缓存。
|
||||
*/
|
||||
public String getInstalledAuthCode() {
|
||||
String local = cachedAuthCode;
|
||||
if (local != null) {
|
||||
return local;
|
||||
}
|
||||
if (cachedAuthCode != null) {
|
||||
return cachedAuthCode;
|
||||
}
|
||||
String sdkLocation = properties.getSdkLocation();
|
||||
if (sdkLocation == null || sdkLocation.trim().isEmpty()) {
|
||||
cachedAuthCode = null;
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Path path = Paths.get(sdkLocation);
|
||||
if (!Files.exists(path)) {
|
||||
cachedAuthCode = null;
|
||||
return null;
|
||||
}
|
||||
String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).trim();
|
||||
cachedAuthCode = content;
|
||||
return cachedAuthCode;
|
||||
} catch (IOException e) {
|
||||
// 读取失败时,不缓存错误结果,直接返回 null
|
||||
cachedAuthCode = null;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user