diff --git a/pom.xml b/pom.xml
index 6a409df..39fdf8c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -45,6 +45,10 @@
spring-boot-configuration-processor
true
+
+ org.springframework.boot
+ spring-boot-starter-cache
+
org.springframework.boot
@@ -106,23 +110,9 @@
- com.alicp.jetcache
- jetcache-starter-redis
- 2.5.14
-
-
- com.alicp.jetcache
- jetcache-redis
-
-
- redis.clients
- jedis
-
-
- com.esotericsoftware
- kryo
-
-
+ com.alibaba
+ fastjson
+ 1.2.61
diff --git a/src/main/java/im/zhaojun/ZfileApplication.java b/src/main/java/im/zhaojun/ZfileApplication.java
index e83dd14..683c42d 100644
--- a/src/main/java/im/zhaojun/ZfileApplication.java
+++ b/src/main/java/im/zhaojun/ZfileApplication.java
@@ -1,7 +1,5 @@
package im.zhaojun;
-import com.alicp.jetcache.anno.config.EnableCreateCacheAnnotation;
-import com.alicp.jetcache.anno.config.EnableMethodCache;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@@ -12,8 +10,6 @@ import org.springframework.scheduling.annotation.EnableAsync;
*/
@EnableAsync
@SpringBootApplication
-@EnableMethodCache(basePackages = "im.zhaojun", proxyTargetClass = true)
-@EnableCreateCacheAnnotation
@EnableAspectJAutoProxy(exposeProxy = true)
public class ZfileApplication {
diff --git a/src/main/java/im/zhaojun/common/aop/FileListCacheAspect.java b/src/main/java/im/zhaojun/common/aop/FileListCacheAspect.java
new file mode 100644
index 0000000..02e6f01
--- /dev/null
+++ b/src/main/java/im/zhaojun/common/aop/FileListCacheAspect.java
@@ -0,0 +1,55 @@
+package im.zhaojun.common.aop;
+
+import im.zhaojun.common.cache.ZFileCache;
+import im.zhaojun.common.model.dto.FileItemDTO;
+import im.zhaojun.common.service.SystemConfigService;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+/**
+ * 操作日志切面.
+ */
+@Aspect
+@Component
+public class FileListCacheAspect {
+
+ @Resource
+ private ZFileCache zFileCache;
+
+ @Resource
+ private SystemConfigService systemConfigService;
+
+ @Pointcut("execution(public * im.zhaojun.common.service.AbstractFileService.fileList(..))")
+ public void pointcut() {
+ }
+
+ @Around(value = "pointcut()")
+ public Object around(ProceedingJoinPoint point) throws Throwable {
+ List result;
+
+ Object[] args = point.getArgs();
+ String path = String.valueOf(args[0]);
+
+ boolean enableCache = systemConfigService.getEnableCache();
+
+ if (enableCache) {
+ List cacheFileList = zFileCache.get(path);
+ if (CollectionUtils.isEmpty(cacheFileList)) {
+ result = (List) point.proceed();
+ zFileCache.put(path, result);
+ } else {
+ result = cacheFileList;
+ }
+ } else {
+ result = (List) point.proceed();
+ }
+ return result;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/im/zhaojun/common/cache/ZFileCache.java b/src/main/java/im/zhaojun/common/cache/ZFileCache.java
new file mode 100644
index 0000000..f83dcd2
--- /dev/null
+++ b/src/main/java/im/zhaojun/common/cache/ZFileCache.java
@@ -0,0 +1,115 @@
+package im.zhaojun.common.cache;
+
+import cn.hutool.core.util.StrUtil;
+import im.zhaojun.common.model.dto.FileItemDTO;
+import im.zhaojun.common.model.dto.SystemConfigDTO;
+import im.zhaojun.common.model.enums.FileTypeEnum;
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+/**
+ * @author zhaojun
+ */
+@Component
+public class ZFileCache {
+
+ private ConcurrentMap> fileCache = new ConcurrentHashMap<>();
+
+ private ConcurrentMap fileCountCache = new ConcurrentHashMap<>();
+
+ private SystemConfigDTO systemConfigCache;
+
+ public static final String CACHE_FILE_COUNT_KEY = "file-count";
+
+ public static final String CACHE_DIRECTORY_COUNT_KEY = "directory-count";
+
+ public synchronized void put(String key, List value) {
+ for (FileItemDTO fileItemDTO : value) {
+ if (FileTypeEnum.FILE.equals(fileItemDTO.getType())) {
+ incrCacheFileCount();
+ } else {
+ incrCacheDirectoryCount();
+ }
+ }
+ fileCache.put(key, value);
+ }
+
+ public List get(String key) {
+ return fileCache.get(key);
+ }
+
+ public void clear() {
+ fileCache.clear();
+ fileCountCache.clear();
+ }
+
+ public long cacheCount() {
+ return fileCache.size();
+ }
+
+ public List find(String key, boolean ignoreCase) {
+ List result = new ArrayList<>();
+
+ Collection> values = fileCache.values();
+ for (List fileItemList : values) {
+ for (FileItemDTO fileItemDTO : fileItemList) {
+ boolean testResult;
+
+ if (ignoreCase) {
+ testResult = StrUtil.containsIgnoreCase(fileItemDTO.getName(), key);
+ } else {
+ testResult = fileItemDTO.getName().contains(key);
+ }
+
+ if (testResult) {
+ result.add(fileItemDTO);
+ }
+ }
+ }
+ return result;
+ }
+
+ public Set keySet() {
+ return fileCache.keySet();
+ }
+
+ public void remove(String key) {
+ fileCache.remove(key);
+ }
+
+ private void incrCacheFileCount() {
+ Integer originValue = fileCountCache.getOrDefault(CACHE_FILE_COUNT_KEY, 0);
+ fileCountCache.put(CACHE_FILE_COUNT_KEY, originValue + 1);
+ }
+
+ private void incrCacheDirectoryCount() {
+ Integer originValue = fileCountCache.getOrDefault(CACHE_DIRECTORY_COUNT_KEY, 0);
+ fileCountCache.put(CACHE_DIRECTORY_COUNT_KEY, originValue + 1);
+ }
+
+ public int getCacheFileCount() {
+ return fileCountCache.getOrDefault(CACHE_FILE_COUNT_KEY, 0);
+ }
+
+ public int getCacheDirectorCount() {
+ return fileCountCache.getOrDefault(CACHE_DIRECTORY_COUNT_KEY, 0);
+ }
+
+ public void updateConfig(SystemConfigDTO systemConfigCache) {
+ this.systemConfigCache = systemConfigCache;
+ }
+
+ public SystemConfigDTO getConfig() {
+ return this.systemConfigCache;
+ }
+
+ public void removeConfig() {
+ this.systemConfigCache = null;
+ }
+}
diff --git a/src/main/java/im/zhaojun/common/config/CacheConfig.java b/src/main/java/im/zhaojun/common/config/CacheConfig.java
new file mode 100644
index 0000000..c87513f
--- /dev/null
+++ b/src/main/java/im/zhaojun/common/config/CacheConfig.java
@@ -0,0 +1,21 @@
+package im.zhaojun.common.config;
+
+import im.zhaojun.common.model.dto.FileItemDTO;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+/**
+ * @author zhaojun
+ */
+@Configuration
+public class CacheConfig {
+
+ @Bean
+ public ConcurrentMap> concurrentMapCache() {
+ return new ConcurrentHashMap<>();
+ }
+}
diff --git a/src/main/java/im/zhaojun/common/config/ContentTypeTextToTextJson.java b/src/main/java/im/zhaojun/common/config/ContentTypeTextToTextJson.java
new file mode 100644
index 0000000..61a456f
--- /dev/null
+++ b/src/main/java/im/zhaojun/common/config/ContentTypeTextToTextJson.java
@@ -0,0 +1,29 @@
+package im.zhaojun.common.config;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpRequest;
+import org.springframework.http.client.ClientHttpRequestExecution;
+import org.springframework.http.client.ClientHttpRequestInterceptor;
+import org.springframework.http.client.ClientHttpResponse;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.Collections;
+
+public class ContentTypeTextToTextJson implements ClientHttpRequestInterceptor {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ContentTypeTextToTextJson.class);
+
+ @Override
+ public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
+ throws IOException {
+ URI uri = request.getURI();
+ ClientHttpResponse response = execution.execute(request, body);
+ HttpHeaders headers = response.getHeaders();
+ headers.put("Content-Type", Collections.singletonList("application/text"));
+ return response;
+ }
+
+}
\ No newline at end of file
diff --git a/src/main/java/im/zhaojun/common/config/GlobalScheduleTask.java b/src/main/java/im/zhaojun/common/config/GlobalScheduleTask.java
index 98ca2a2..8510d6e 100644
--- a/src/main/java/im/zhaojun/common/config/GlobalScheduleTask.java
+++ b/src/main/java/im/zhaojun/common/config/GlobalScheduleTask.java
@@ -1,5 +1,6 @@
package im.zhaojun.common.config;
+import im.zhaojun.common.cache.ZFileCache;
import im.zhaojun.common.model.enums.StorageTypeEnum;
import im.zhaojun.common.service.AbstractFileService;
import im.zhaojun.common.service.StorageConfigService;
@@ -23,6 +24,9 @@ import java.util.Objects;
@Slf4j
public class GlobalScheduleTask {
+ @Resource
+ private ZFileCache zFileCache;
+
@Resource
private StorageConfigService storageConfigService;
@@ -38,7 +42,7 @@ public class GlobalScheduleTask {
/**
* 项目启动 30 秒后, 每 15 分钟执行一次刷新 OneDrive Token 的定时任务.
*/
- @Scheduled(fixedRate = 1000 * 60 * 15, initialDelay = 1000 * 30)
+ @Scheduled(fixedRate = 1000 * 60 * 10, initialDelay = 1000 * 30)
public void autoRefreshOneDriveToken() {
AbstractFileService currentFileService = systemConfigService.getCurrentFileService();
@@ -54,16 +58,12 @@ public class GlobalScheduleTask {
return;
}
- try {
- refreshOneDriveToken(StorageTypeEnum.ONE_DRIVE);
- } catch (Exception e) {
- log.debug("刷新 OneDrive Token 失败.", e);
- }
+ StorageTypeEnum currentStorageTypeEnum = currentFileService.getStorageTypeEnum();
try {
- refreshOneDriveToken(StorageTypeEnum.ONE_DRIVE_CHINA);
+ refreshOneDriveToken(currentStorageTypeEnum);
} catch (Exception e) {
- log.debug("刷新 OneDrive 世纪互联 Token 失败.", e);
+ log.debug("刷新 " + currentStorageTypeEnum.getDescription() + " Token 失败.", e);
}
}
@@ -78,4 +78,5 @@ public class GlobalScheduleTask {
}
log.info("刷新 {} key 时间: {}", storageType.getDescription(), LocalDateTime.now());
}
+
}
\ No newline at end of file
diff --git a/src/main/java/im/zhaojun/common/config/ZFileConfiguration.java b/src/main/java/im/zhaojun/common/config/ZFileConfiguration.java
index 8c110ef..aee5ced 100644
--- a/src/main/java/im/zhaojun/common/config/ZFileConfiguration.java
+++ b/src/main/java/im/zhaojun/common/config/ZFileConfiguration.java
@@ -6,6 +6,7 @@ import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
+import java.util.Collections;
/**
* @author zhaojun
@@ -17,6 +18,7 @@ public class ZFileConfiguration {
public RestTemplate restTemplate(){
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
+ restTemplate.setInterceptors(Collections.singletonList(new ContentTypeTextToTextJson()));
return restTemplate;
}
diff --git a/src/main/java/im/zhaojun/common/controller/CacheController.java b/src/main/java/im/zhaojun/common/controller/CacheController.java
index f0c1300..45cdc47 100644
--- a/src/main/java/im/zhaojun/common/controller/CacheController.java
+++ b/src/main/java/im/zhaojun/common/controller/CacheController.java
@@ -1,5 +1,6 @@
package im.zhaojun.common.controller;
+import im.zhaojun.common.cache.ZFileCache;
import im.zhaojun.common.model.dto.CacheConfigDTO;
import im.zhaojun.common.model.dto.ResultBean;
import im.zhaojun.common.service.AbstractFileService;
@@ -30,6 +31,9 @@ public class CacheController {
@Resource
private FileCacheService fileCacheService;
+ @Resource
+ private ZFileCache zFileCache;
+
@PostMapping("/enable")
public ResultBean enableCache() throws Exception {
fileCacheService.enableCache();
@@ -43,16 +47,14 @@ public class CacheController {
}
@GetMapping("/config")
- public ResultBean cacheConfig() throws Exception {
+ public ResultBean cacheConfig() {
AbstractFileService fileService = systemConfigService.getCurrentFileService();
- Set cacheKeys = fileService.getCacheKeys();
-
CacheConfigDTO cacheConfigDTO = new CacheConfigDTO();
cacheConfigDTO.setEnableCache(systemConfigService.getEnableCache());
cacheConfigDTO.setCacheFinish(fileAsyncCacheService.isCacheFinish());
- cacheConfigDTO.setCacheKeys(cacheKeys);
- cacheConfigDTO.setCacheDirectoryCount(fileAsyncCacheService.getCacheDirectoryCount());
- cacheConfigDTO.setCacheFileCount(fileAsyncCacheService.getCacheFileCount());
+ cacheConfigDTO.setCacheKeys(zFileCache.keySet());
+ cacheConfigDTO.setCacheDirectoryCount(zFileCache.getCacheDirectorCount());
+ cacheConfigDTO.setCacheFileCount(zFileCache.getCacheFileCount());
return ResultBean.success(cacheConfigDTO);
}
diff --git a/src/main/java/im/zhaojun/common/service/AbstractFileService.java b/src/main/java/im/zhaojun/common/service/AbstractFileService.java
index d38428c..12e84d2 100644
--- a/src/main/java/im/zhaojun/common/service/AbstractFileService.java
+++ b/src/main/java/im/zhaojun/common/service/AbstractFileService.java
@@ -1,33 +1,20 @@
package im.zhaojun.common.service;
import cn.hutool.core.util.BooleanUtil;
-import cn.hutool.core.util.StrUtil;
-import com.alicp.jetcache.Cache;
-import com.alicp.jetcache.RefreshPolicy;
-import com.alicp.jetcache.anno.CacheRefresh;
-import com.alicp.jetcache.anno.CacheType;
-import com.alicp.jetcache.anno.Cached;
-import com.alicp.jetcache.anno.CreateCache;
+import im.zhaojun.common.cache.ZFileCache;
import im.zhaojun.common.model.constant.ZFileConstant;
import im.zhaojun.common.model.dto.FileItemDTO;
import im.zhaojun.common.model.dto.SystemConfigDTO;
-import im.zhaojun.common.model.enums.FileTypeEnum;
import im.zhaojun.common.model.enums.StorageTypeEnum;
-import im.zhaojun.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Value;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
-import java.util.ArrayDeque;
-import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
-import java.util.concurrent.TimeUnit;
-import java.util.stream.Collectors;
/**
* @author zhaojun
@@ -50,8 +37,8 @@ public abstract class AbstractFileService extends FileCacheService implements Fi
@Resource
private FileAsyncCacheService fileAsyncCacheService;
- @CreateCache(name = SYSTEM_CONFIG_CACHE_PREFIX, cacheType = CacheType.LOCAL)
- private Cache> cache;
+ @Resource
+ private ZFileCache zFileCache;
/***
* 获取指定路径下的文件及文件夹, 默认缓存 60 分钟,每隔 30 分钟刷新一次.
@@ -60,10 +47,6 @@ public abstract class AbstractFileService extends FileCacheService implements Fi
* @throws Exception 获取文件列表中出现的异常
*/
@Override
- @Cached(name = SYSTEM_CONFIG_CACHE_PREFIX,
- key = "args[0]",
- cacheType = CacheType.LOCAL, localLimit = 100000, condition = "mvel{bean('systemConfigService').enableCache}")
- @CacheRefresh(refresh = 30, timeUnit = TimeUnit.MINUTES)
public abstract List fileList(String path) throws Exception;
/**
@@ -73,11 +56,9 @@ public abstract class AbstractFileService extends FileCacheService implements Fi
* 3. 重置缓存个数
* 4. 标记为当前处于未完成缓存状态
*/
- public void clearFileCache() throws Exception {
- Set cacheKeys = getCacheKeys();
- cache.removeAll(cacheKeys);
+ public void clearFileCache() {
+ zFileCache.clear();
closeCacheAutoRefresh();
- fileAsyncCacheService.resetCacheCount();
fileAsyncCacheService.setCacheFinish(false);
}
@@ -126,59 +107,8 @@ public abstract class AbstractFileService extends FileCacheService implements Fi
* @return 包含该文件名的所有文件或文件夹
*/
public List search(String name) {
- List result = new ArrayList<>();
-
boolean searchIgnoreCase = systemConfigService.getSearchIgnoreCase();
-
- List fileItemList = selectAllFileList();
- for (FileItemDTO fileItemDTO : fileItemList) {
-
- boolean testResult;
-
- if (searchIgnoreCase) {
- testResult = StrUtil.containsIgnoreCase(fileItemDTO.getName(), name);
- } else {
- testResult = fileItemDTO.getName().contains(name);
- }
-
- if (testResult) {
- result.add(fileItemDTO);
- }
- }
-
- return result;
- }
-
- /**
- * 查询所有文件, 仅去缓存中查询.
- * @return 所有文件
- */
- public List selectAllFileList() {
- List result = new ArrayList<>();
- boolean enableCache = systemConfigService.getEnableCache();
- if (!enableCache) {
- log.debug("未开启缓存, 不支持查询所有文件.");
- return null;
- }
- String path = "/";
-
- List fileItemList = cache.get(path);
- fileItemList = fileItemList == null ? new ArrayList<>() : fileItemList;
- ArrayDeque queue = new ArrayDeque<>(fileItemList);
-
- while (!queue.isEmpty()) {
- FileItemDTO fileItemDTO = queue.pop();
- result.add(fileItemDTO);
- if (fileItemDTO.getType() == FileTypeEnum.FOLDER) {
- String filePath = StringUtils.removeDuplicateSeparator("/" + fileItemDTO.getPath() + "/" + fileItemDTO.getName() + "/");
- List cacheList = cache.get(filePath);
- if (cacheList != null && isNotEncryptedFolder(cacheList)) {
- queue.addAll(cacheList);
- }
- }
-
- }
- return result;
+ return zFileCache.find(name, searchIgnoreCase);
}
/**
@@ -202,42 +132,22 @@ public abstract class AbstractFileService extends FileCacheService implements Fi
return true;
}
- /**
- * 获取所有缓存的 Key, 仅当开启缓存, 且缓存完成时, 可获取.
- * @return 所有缓存的 Key
- */
- public Set getCacheKeys() {
- if (systemConfigService.getEnableCache() && fileAsyncCacheService.isCacheFinish()) {
- Set collect = selectAllFileList().stream().map(fileItemDTO -> {
- if (fileItemDTO.getType() == FileTypeEnum.FOLDER) {
- return StringUtils.removeDuplicateSeparator("/" + fileItemDTO.getPath() + "/" + fileItemDTO.getName() + "/");
- }
- return null;
- }).collect(Collectors.toSet());
- collect.remove(null);
- collect.add("/");
- return collect;
- } else {
- return Collections.emptySet();
- }
- }
-
/**
* 刷新缓存
*/
public void refreshCache(String key) throws Exception {
- cache.remove(key);
+ zFileCache.remove(key);
FileService currentFileService = (FileService) AopContext.currentProxy();
currentFileService.fileList(key);
}
public void closeCacheAutoRefresh() {
- cache.config().setRefreshPolicy(null);
+ // cache.config().setRefreshPolicy(null);
}
public void openCacheAutoRefresh() {
- RefreshPolicy refreshPolicy = RefreshPolicy.newPolicy(30, TimeUnit.MINUTES);
- cache.config().setRefreshPolicy(refreshPolicy);
+ // RefreshPolicy refreshPolicy = RefreshPolicy.newPolicy(30, TimeUnit.MINUTES);
+ // cache.config().setRefreshPolicy(refreshPolicy);
}
public abstract FileItemDTO getFileItem(String path);
diff --git a/src/main/java/im/zhaojun/common/service/FileAsyncCacheService.java b/src/main/java/im/zhaojun/common/service/FileAsyncCacheService.java
index 4a3432b..48edc03 100644
--- a/src/main/java/im/zhaojun/common/service/FileAsyncCacheService.java
+++ b/src/main/java/im/zhaojun/common/service/FileAsyncCacheService.java
@@ -1,9 +1,5 @@
package im.zhaojun.common.service;
-import cn.hutool.core.util.ObjectUtil;
-import com.alicp.jetcache.Cache;
-import com.alicp.jetcache.anno.CacheType;
-import com.alicp.jetcache.anno.CreateCache;
import im.zhaojun.common.config.StorageTypeFactory;
import im.zhaojun.common.model.dto.FileItemDTO;
import im.zhaojun.common.model.enums.FileTypeEnum;
@@ -26,13 +22,6 @@ public class FileAsyncCacheService {
public static final String CACHE_PROCESS_PREFIX = "zfile-process-cache:";
- public static final String CACHE_FILE_COUNT_KEY = "file-count";
-
- public static final String CACHE_DIRECTORY_COUNT_KEY = "directory-count";
-
- @CreateCache(name = "SYSTEM_CONFIG_CACHE_PREFIX", cacheType = CacheType.LOCAL)
- private Cache cache;
-
private boolean cacheFinish;
@Resource
@@ -60,15 +49,7 @@ public class FileAsyncCacheService {
return;
}
- Integer cacheDirectoryCount = cache.get(CACHE_DIRECTORY_COUNT_KEY);
- if (cacheDirectoryCount == null) {
- cacheDirectoryCount = 0;
- }
-
- Integer cacheFileCount = cache.get(CACHE_FILE_COUNT_KEY);
- if (cacheFileCount == null) {
- cacheFileCount = 0;
- }
+ Integer cacheDirectoryCount = 0;
log.info("缓存 {} 所有文件开始", storageStrategy.getDescription());
long startTime = System.currentTimeMillis();
@@ -80,19 +61,6 @@ public class FileAsyncCacheService {
while (!queue.isEmpty()) {
FileItemDTO fileItemDTO = queue.pop();
- if (FileTypeEnum.FOLDER.equals(fileItemDTO.getType())) {
- cacheDirectoryCount++;
- }
- if (FileTypeEnum.FILE.equals(fileItemDTO.getType())) {
- cacheFileCount++;
- }
-
- log.debug("已缓存 {} 个文件夹", cacheDirectoryCount);
- cache.put(CACHE_DIRECTORY_COUNT_KEY, cacheDirectoryCount);
-
- log.debug("已缓存 {} 个文件", cacheFileCount);
- cache.put(CACHE_FILE_COUNT_KEY, cacheFileCount);
-
if (fileItemDTO.getType() == FileTypeEnum.FOLDER) {
String filePath = StringUtils.removeDuplicateSeparator("/" + fileItemDTO.getPath() + "/" + fileItemDTO.getName() + "/");
@@ -100,36 +68,16 @@ public class FileAsyncCacheService {
queue.addAll(fileItems);
}
}
- cache.put(CACHE_DIRECTORY_COUNT_KEY, cacheDirectoryCount);
- cache.put(CACHE_FILE_COUNT_KEY, cacheFileCount);
} catch (Exception e) {
log.error("缓存所有文件失败", e);
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
- log.info("缓存 {} 所有文件结束, 用时: {} 秒, 文件夹共 {} 个, 文件共 {} 个",
- storageStrategy.getDescription(),
- ( (endTime - startTime) / 1000 ), cacheDirectoryCount, cacheFileCount);
+ log.info("缓存 {} 所有文件结束, 用时: {} 秒", storageStrategy.getDescription(), ((endTime - startTime) / 1000));
cacheFinish = true;
}
- /**
- * 清理缓存的文件/文件夹数量统计
- */
- public void resetCacheCount() {
- cache.remove(CACHE_DIRECTORY_COUNT_KEY);
- cache.remove(CACHE_FILE_COUNT_KEY);
- }
-
- public Integer getCacheDirectoryCount() {
- return ObjectUtil.defaultIfNull(cache.get(CACHE_DIRECTORY_COUNT_KEY), 0);
- }
-
- public Integer getCacheFileCount() {
- return ObjectUtil.defaultIfNull(cache.get(CACHE_FILE_COUNT_KEY), 0);
- }
-
public boolean isCacheFinish() {
return cacheFinish;
}
@@ -137,4 +85,5 @@ public class FileAsyncCacheService {
public void setCacheFinish(boolean cacheFinish) {
this.cacheFinish = cacheFinish;
}
+
}
diff --git a/src/main/java/im/zhaojun/common/service/FileCacheService.java b/src/main/java/im/zhaojun/common/service/FileCacheService.java
index 64e3226..cae8027 100644
--- a/src/main/java/im/zhaojun/common/service/FileCacheService.java
+++ b/src/main/java/im/zhaojun/common/service/FileCacheService.java
@@ -29,10 +29,8 @@ public class FileCacheService {
public void disableCache() throws Exception {
systemConfigService.updateCacheEnableConfig(false);
-
AbstractFileService currentFileService = systemConfigService.getCurrentFileService();
currentFileService.clearFileCache();
- fileAsyncCacheService.resetCacheCount();
}
}
diff --git a/src/main/java/im/zhaojun/common/service/SystemConfigService.java b/src/main/java/im/zhaojun/common/service/SystemConfigService.java
index 8659243..5ed3a90 100644
--- a/src/main/java/im/zhaojun/common/service/SystemConfigService.java
+++ b/src/main/java/im/zhaojun/common/service/SystemConfigService.java
@@ -3,9 +3,7 @@ package im.zhaojun.common.service;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.crypto.SecureUtil;
-import com.alicp.jetcache.Cache;
-import com.alicp.jetcache.anno.CacheType;
-import com.alicp.jetcache.anno.CreateCache;
+import im.zhaojun.common.cache.ZFileCache;
import im.zhaojun.common.config.StorageTypeFactory;
import im.zhaojun.common.model.SystemConfig;
import im.zhaojun.common.model.constant.SystemConfigConstant;
@@ -31,8 +29,8 @@ public class SystemConfigService {
public static final String SYSTEM_CONFIG_CACHE_KEY = "1";
- @CreateCache(name = SYSTEM_CONFIG_CACHE_PREFIX, cacheType = CacheType.LOCAL)
- private Cache configCache;
+ @Resource
+ private ZFileCache zFileCache;
@Resource
private SystemConfigRepository systemConfigRepository;
@@ -43,9 +41,9 @@ public class SystemConfigService {
private Class systemConfigDTOClass = SystemConfigDTO.class;
public SystemConfigDTO getSystemConfig() {
- Object cache = configCache.get(SYSTEM_CONFIG_CACHE_KEY);
- if (configCache.get(SYSTEM_CONFIG_CACHE_KEY) != null) {
- return (SystemConfigDTO) cache;
+ SystemConfigDTO cacheConfig = zFileCache.getConfig();
+ if (cacheConfig != null) {
+ return cacheConfig;
}
SystemConfigDTO systemConfigDTO = new SystemConfigDTO();
@@ -69,7 +67,7 @@ public class SystemConfigService {
}
}
- configCache.put(SYSTEM_CONFIG_CACHE_KEY, systemConfigDTO);
+ zFileCache.updateConfig(systemConfigDTO);
return systemConfigDTO;
}
@@ -94,7 +92,7 @@ public class SystemConfigService {
boolean oldEnableCache = getEnableCache();
boolean curEnableCache = BooleanUtil.isTrue(systemConfigDTO.getEnableCache());
- configCache.remove(SYSTEM_CONFIG_CACHE_KEY);
+ zFileCache.removeConfig();
systemConfigRepository.saveAll(systemConfigList);
@@ -119,7 +117,7 @@ public class SystemConfigService {
SystemConfig systemConfig = systemConfigRepository.findByKey(SystemConfigConstant.PASSWORD);
systemConfig.setValue(encryptionPassword);
- configCache.remove(SYSTEM_CONFIG_CACHE_KEY);
+ zFileCache.removeConfig();
systemConfigRepository.save(systemConfig);
}
@@ -129,7 +127,7 @@ public class SystemConfigService {
SystemConfig enableConfig = systemConfigRepository.findByKey(SystemConfigConstant.ENABLE_CACHE);
enableConfig.setValue(isEnable.toString());
systemConfigRepository.save(enableConfig);
- configCache.remove(SYSTEM_CONFIG_CACHE_KEY);
+ zFileCache.removeConfig();
}
diff --git a/src/main/java/im/zhaojun/common/service/SystemService.java b/src/main/java/im/zhaojun/common/service/SystemService.java
index a5cc377..c73a502 100644
--- a/src/main/java/im/zhaojun/common/service/SystemService.java
+++ b/src/main/java/im/zhaojun/common/service/SystemService.java
@@ -3,12 +3,14 @@ package im.zhaojun.common.service;
import im.zhaojun.common.model.constant.ZFileConstant;
import im.zhaojun.common.model.dto.FileItemDTO;
import im.zhaojun.common.model.dto.SiteConfigDTO;
+import im.zhaojun.common.model.enums.StorageTypeEnum;
import im.zhaojun.common.util.HttpUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
+import java.util.Objects;
/**
* @author zhaojun
@@ -23,12 +25,19 @@ public class SystemService {
* 构建指定路径下标题, 页面文档信息
* @param path 路径
*/
- public synchronized SiteConfigDTO getConfig(String path) throws Exception {
+ public SiteConfigDTO getConfig(String path) throws Exception {
SiteConfigDTO siteConfigDTO = new SiteConfigDTO();
AbstractFileService fileService = systemConfigService.getCurrentFileService();
- List fileItemList = new ArrayList<>(fileService.fileList(path));
+ List fileItemList;
+
+ if (Objects.equals(systemConfigService.getSystemConfig().getStorageStrategy(), StorageTypeEnum.FTP)) {
+ fileItemList = new ArrayList<>();
+ } else {
+ fileItemList = fileService.fileList(path);
+ }
+
for (FileItemDTO fileItemDTO : fileItemList) {
if (ZFileConstant.README_FILE_NAME.equalsIgnoreCase(fileItemDTO.getName())) {
siteConfigDTO.setReadme(HttpUtil.getTextContent(fileItemDTO.getUrl()));
diff --git a/src/main/java/im/zhaojun/common/util/StartupListener.java b/src/main/java/im/zhaojun/common/util/StartupListener.java
index 92e0e0d..8e1ebd7 100644
--- a/src/main/java/im/zhaojun/common/util/StartupListener.java
+++ b/src/main/java/im/zhaojun/common/util/StartupListener.java
@@ -1,9 +1,14 @@
package im.zhaojun.common.util;
import cn.hutool.core.net.NetUtil;
+import im.zhaojun.common.cache.ZFileCache;
import im.zhaojun.common.exception.InitializeException;
+import im.zhaojun.common.service.AbstractFileService;
import im.zhaojun.common.service.FileAsyncCacheService;
+import im.zhaojun.common.service.SystemConfigService;
+import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
@@ -12,6 +17,9 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.LinkedHashSet;
+import java.util.Set;
+import java.util.Timer;
+import java.util.TimerTask;
/**
* 项目启动监听器, 当项目启动时, 遍历当前对象存储的所有内容, 添加到缓存中.
@@ -27,10 +35,51 @@ public class StartupListener implements ApplicationListener keySet = zFileCache.keySet();
+ for (String key : keySet) {
+ zFileCache.remove(key);
+ AbstractFileService currentFileService = systemConfigService.getCurrentFileService();
+ currentFileService.fileList(key);
+ }
+ }
+ }, delay * 1000,interval * 1000);
+ }
}
private void printStartInfo() {