From de947e510cce6069a94b8542988fbf500cfdbabf Mon Sep 17 00:00:00 2001 From: zhaojun1998 Date: Thu, 2 Jan 2020 18:17:11 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E7=BC=93=E5=AD=98=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E4=BC=98=E5=8C=96,=20=E4=BF=AE=E6=94=B9=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E5=90=8E=E8=87=AA=E5=8A=A8=E4=BF=AE=E6=94=B9=E7=BC=93?= =?UTF-8?q?=E5=AD=98,=20=E4=B8=94=E7=BC=93=E5=AD=98=E6=9C=AA=E5=AE=8C?= =?UTF-8?q?=E6=88=90,=20=E6=90=9C=E7=B4=A2=E5=8A=9F=E8=83=BD=E6=9A=82?= =?UTF-8?q?=E6=97=B6=E7=A6=81=E7=94=A8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aspect/StorageStrategyInitCheckAspect.java | 6 +++++- .../common/controller/AdminController.java | 4 ++-- .../zhaojun/common/controller/FileController.java | 7 +++++++ .../common/controller/InstallController.java | 2 +- .../common/service/FileAsyncCacheService.java | 15 ++++++++++++--- .../common/service/SystemConfigService.java | 8 ++++++++ 6 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/main/java/im/zhaojun/common/aspect/StorageStrategyInitCheckAspect.java b/src/main/java/im/zhaojun/common/aspect/StorageStrategyInitCheckAspect.java index a0103e6..a500069 100644 --- a/src/main/java/im/zhaojun/common/aspect/StorageStrategyInitCheckAspect.java +++ b/src/main/java/im/zhaojun/common/aspect/StorageStrategyInitCheckAspect.java @@ -19,9 +19,13 @@ public class StorageStrategyInitCheckAspect { public void logStart() { SystemConfigService systemConfigService = SpringContextHolder.getBean(SystemConfigService.class); AbstractFileService currentFileService = systemConfigService.getCurrentFileService(); - if (currentFileService == null || !currentFileService.getIsInitialized()) { + if (currentFileService == null) { + throw new StorageStrategyUninitializedException("存储策略尚未初始化, 请联系管理员!"); + } + if (!currentFileService.getIsInitialized()) { throw new StorageStrategyUninitializedException("存储策略异常, 请联系管理员!"); } + } } diff --git a/src/main/java/im/zhaojun/common/controller/AdminController.java b/src/main/java/im/zhaojun/common/controller/AdminController.java index ac367dd..a2bdf69 100644 --- a/src/main/java/im/zhaojun/common/controller/AdminController.java +++ b/src/main/java/im/zhaojun/common/controller/AdminController.java @@ -59,7 +59,7 @@ public class AdminController { * 更新系统配置 */ @PostMapping("/config") - public ResultBean updateConfig(SystemConfigDTO systemConfigDTO) { + public ResultBean updateConfig(SystemConfigDTO systemConfigDTO) throws Exception { StorageTypeEnum currentStorageStrategy = systemConfigService.getCurrentStorageStrategy(); systemConfigDTO.setId(1); @@ -82,7 +82,7 @@ public class AdminController { /** * 清理当前启用的存储引擎的缓存 */ - @GetMapping("/clear-cache") + @PostMapping("/clear-cache") public ResultBean clearCache() { AbstractFileService fileService = systemConfigService.getCurrentFileService(); fileService.clearCache(); diff --git a/src/main/java/im/zhaojun/common/controller/FileController.java b/src/main/java/im/zhaojun/common/controller/FileController.java index 3be8af6..fe0ccf0 100644 --- a/src/main/java/im/zhaojun/common/controller/FileController.java +++ b/src/main/java/im/zhaojun/common/controller/FileController.java @@ -11,6 +11,7 @@ import im.zhaojun.common.model.dto.SiteConfigDTO; import im.zhaojun.common.model.dto.SystemConfigDTO; import im.zhaojun.common.model.enums.StorageTypeEnum; import im.zhaojun.common.service.AbstractFileService; +import im.zhaojun.common.service.FileAsyncCacheService; import im.zhaojun.common.service.StorageConfigService; import im.zhaojun.common.service.SystemConfigService; import im.zhaojun.common.service.SystemService; @@ -44,6 +45,9 @@ public class FileController { @Resource private StorageConfigService storageConfigService; + @Resource + private FileAsyncCacheService fileAsyncCacheService; + /** * 滚动加载每页条数. */ @@ -133,6 +137,9 @@ public class FileController { if (!systemConfigDTO.getSearchEnable()) { throw new SearchDisableException("搜索功能未开启"); } + if (!fileAsyncCacheService.isCacheFinish()) { + throw new SearchDisableException("搜索功能缓存预热中, 请稍后再试"); + } return ResultBean.success(fileService.search(URLUtil.decode(name))); } diff --git a/src/main/java/im/zhaojun/common/controller/InstallController.java b/src/main/java/im/zhaojun/common/controller/InstallController.java index ede7be9..7188ed0 100644 --- a/src/main/java/im/zhaojun/common/controller/InstallController.java +++ b/src/main/java/im/zhaojun/common/controller/InstallController.java @@ -48,7 +48,7 @@ public class InstallController { @PostMapping("/install") @ResponseBody - public ResultBean install(InstallModelDTO installModelDTO) { + public ResultBean install(InstallModelDTO installModelDTO) throws Exception { SystemConfigDTO systemConfigDTO = systemConfigService.getSystemConfig(); if (systemConfigDTO.getStorageStrategy() != null) { diff --git a/src/main/java/im/zhaojun/common/service/FileAsyncCacheService.java b/src/main/java/im/zhaojun/common/service/FileAsyncCacheService.java index 3c0333a..738fbca 100644 --- a/src/main/java/im/zhaojun/common/service/FileAsyncCacheService.java +++ b/src/main/java/im/zhaojun/common/service/FileAsyncCacheService.java @@ -2,8 +2,7 @@ package im.zhaojun.common.service; import im.zhaojun.common.config.StorageTypeFactory; import im.zhaojun.common.model.enums.StorageTypeEnum; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @@ -12,10 +11,11 @@ import javax.annotation.Resource; /** * @author zhaojun */ +@Slf4j @Service public class FileAsyncCacheService { - private static final Logger log = LoggerFactory.getLogger(FileAsyncCacheService.class); + private boolean cacheFinish; @Resource private SystemConfigService systemConfigService; @@ -42,5 +42,14 @@ public class FileAsyncCacheService { } long endTime = System.currentTimeMillis(); log.info("缓存 {} 所有文件结束, 用时: {} 秒", storageStrategy.getDescription(), ( (endTime - startTime) / 1000 )); + cacheFinish = true; + } + + public boolean isCacheFinish() { + return cacheFinish; + } + + public void setCacheFinish(boolean cacheFinish) { + this.cacheFinish = cacheFinish; } } diff --git a/src/main/java/im/zhaojun/common/service/SystemConfigService.java b/src/main/java/im/zhaojun/common/service/SystemConfigService.java index de90fd3..b58f763 100644 --- a/src/main/java/im/zhaojun/common/service/SystemConfigService.java +++ b/src/main/java/im/zhaojun/common/service/SystemConfigService.java @@ -87,6 +87,9 @@ public class SystemConfigService { searchIgnoreCaseSystemConfig.setValue(systemConfigDTO.getSearchIgnoreCase() ? "true" : "false"); systemConfigList.add(searchIgnoreCaseSystemConfig); + boolean oldEnableCache = getEnableCache(); + Boolean curEnableCache = systemConfigDTO.getEnableCache(); + SystemConfig enableCacheSystemConfig = systemConfigRepository.findByKey(SystemConfigConstant.ENABLE_CACHE); enableCacheSystemConfig.setValue(systemConfigDTO.getEnableCache() ? "true" : "false"); systemConfigList.add(enableCacheSystemConfig); @@ -108,6 +111,11 @@ public class SystemConfigService { } systemConfigRepository.saveAll(systemConfigList); + + if (!oldEnableCache && curEnableCache) { + log.debug("检测到开启了缓存, 开启预热缓存"); + fileAsyncCacheService.cacheGlobalFile(); + } } public void updateUsernameAndPwd(String username, String password) {