TASK:预签名缓存;

This commit is contained in:
shahaibo
2023-10-11 14:53:38 +08:00
parent 9e2f4f75c1
commit e5beab268c
9 changed files with 79 additions and 28 deletions

View File

@@ -403,4 +403,49 @@ public final class LocalCacheUtils {
return null;
}
/**
* 预签名URL缓存
*/
private static LoadingCache<String, String> presignedUrlCache = CacheBuilder.newBuilder()
.concurrencyLevel(10)
.expireAfterWrite((24 * 60 - 1), TimeUnit.MINUTES)
.initialCapacity(100)
.maximumSize(10000)
.recordStats()
.build(new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
return "null";
}
});
/**
* 添加预签名URL到缓存
*
* @param key URL的唯一标识
* @param value 预签名URL
*/
public static void setPresignedUrlCache(String key, String value) {
presignedUrlCache.put(key, value);
}
/**
* 获取预签名URL
*
* @param key URL的唯一标识
* @return 预签名URL如果不存在则返回null
*/
public static String getPresignedUrlCache(String key) {
try {
String value = presignedUrlCache.get(key);
if ("null".equals(value)) {
return null;
}
return value;
} catch (ExecutionException e) {
log.error("getPresignedUrlCache方法错误", e);
}
return null;
}
}

View File

@@ -370,13 +370,19 @@ public class MinioUtil {
}
public String getPresignedUrl(String path, int expiry) {
if (!path.contains("/")) {
throw new BusinessException("The path is error!");
if (LocalCacheUtils.getPresignedUrlCache(path) != null) {
return LocalCacheUtils.getPresignedUrlCache(path);
}else {
if (!path.contains("/")) {
throw new BusinessException("The path is error!");
}
int index = path.indexOf("/");
String bucketName = path.substring(0, index);
String fileName = path.substring(index + 1);
String presignedUrl = getPresignedUrl(bucketName, fileName, expiry);
LocalCacheUtils.setPresignedUrlCache(path, presignedUrl);
return presignedUrl;
}
int index = path.indexOf("/");
String bucketName = path.substring(0, index);
String fileName = path.substring(index + 1);
return getPresignedUrl(bucketName, fileName, expiry);
}
/**