diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/Constants.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/Constants.java index cf80049..1d58d1d 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/Constants.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/Constants.java @@ -72,26 +72,6 @@ public class Constants public static final String IS_ASC = "isAsc"; /** - * 参数管理 cache name - */ - public static final String SYS_CONFIG_CACHE = "sys-config"; - - /** - * 参数管理 cache key - */ - public static final String SYS_CONFIG_KEY = "sys_config:"; - - /** - * 字典管理 cache name - */ - public static final String SYS_DICT_CACHE = "sys-dict"; - - /** - * 字典管理 cache key - */ - public static final String SYS_DICT_KEY = "sys_dict:"; - - /** * 验证码 redis key */ public static final String CAPTCHA_CODE_KEY = "captcha_codes:"; @@ -102,6 +82,11 @@ public class Constants public static final Integer CAPTCHA_EXPIRATION = 2; /** + * 参数管理 cache key + */ + public static final String SYS_CONFIG_KEY = "sys_config:"; + + /** * 资源映射路径 前缀 */ public static final String RESOURCE_PREFIX = "/profile"; diff --git a/ruoyi-common/ruoyi-common-redis/src/main/java/com/ruoyi/common/redis/configure/RedisConfig.java b/ruoyi-common/ruoyi-common-redis/src/main/java/com/ruoyi/common/redis/configure/RedisConfig.java index 0499feb..8d8223d 100644 --- a/ruoyi-common/ruoyi-common-redis/src/main/java/com/ruoyi/common/redis/configure/RedisConfig.java +++ b/ruoyi-common/ruoyi-common-redis/src/main/java/com/ruoyi/common/redis/configure/RedisConfig.java @@ -1,5 +1,6 @@ package com.ruoyi.common.redis.configure; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; @@ -21,6 +22,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; public class RedisConfig extends CachingConfigurerSupport { @Bean + @ConditionalOnMissingBean(name = "redisTemplate") @SuppressWarnings(value = { "unchecked", "rawtypes", "deprecation" }) public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { diff --git a/ruoyi-common/ruoyi-common-redis/src/main/resources/META-INF/spring.factories b/ruoyi-common/ruoyi-common-redis/src/main/resources/META-INF/spring.factories index 2f752bf..60d1d47 100644 --- a/ruoyi-common/ruoyi-common-redis/src/main/resources/META-INF/spring.factories +++ b/ruoyi-common/ruoyi-common-redis/src/main/resources/META-INF/spring.factories @@ -1,4 +1,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.ruoyi.common.redis.configure.RedisConfig,\ com.ruoyi.common.redis.service.RedisService diff --git a/ruoyi-modules/ruoyi-system/pom.xml b/ruoyi-modules/ruoyi-system/pom.xml index bfb644b..9c110ed 100644 --- a/ruoyi-modules/ruoyi-system/pom.xml +++ b/ruoyi-modules/ruoyi-system/pom.xml @@ -77,6 +77,12 @@ com.ruoyi ruoyi-common-swagger + + + + com.ruoyi + ruoyi-common-redis + diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysConfigController.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysConfigController.java index 37c633d..92a08c8 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysConfigController.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysConfigController.java @@ -48,7 +48,7 @@ public class SysConfigController extends BaseController List list = configService.selectConfigList(config); return getDataTable(list); } - + @Log(title = "参数管理", businessType = BusinessType.EXPORT) @PreAuthorize("@ss.hasPermi('system:config:export')") @PostMapping("/export") @@ -120,4 +120,16 @@ public class SysConfigController extends BaseController { return toAjax(configService.deleteConfigByIds(configIds)); } + + /** + * 清空缓存 + */ + @PreAuthorize("@ss.hasPermi('system:config:remove')") + @Log(title = "参数管理", businessType = BusinessType.CLEAN) + @DeleteMapping("/clearCache") + public AjaxResult clearCache() + { + configService.clearCache(); + return AjaxResult.success(); + } } diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysConfigService.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysConfigService.java index 5f06257..0239b23 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysConfigService.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysConfigService.java @@ -52,14 +52,6 @@ public interface ISysConfigService public int updateConfig(SysConfig config); /** - * 删除参数配置信息 - * - * @param configId 参数ID - * @return 结果 - */ - public int deleteConfigById(Long configId); - - /** * 批量删除参数信息 * * @param configIds 需要删除的参数ID @@ -68,6 +60,11 @@ public interface ISysConfigService public int deleteConfigByIds(Long[] configIds); /** + * 清空缓存数据 + */ + public void clearCache(); + + /** * 校验参数键名是否唯一 * * @param config 参数信息 diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java index 78e1e0e..bd9f4d9 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java @@ -1,12 +1,15 @@ package com.ruoyi.system.service.impl; +import java.util.Collection; import java.util.List; - +import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; - +import com.ruoyi.common.core.constant.Constants; import com.ruoyi.common.core.constant.UserConstants; +import com.ruoyi.common.core.text.Convert; import com.ruoyi.common.core.utils.StringUtils; +import com.ruoyi.common.redis.service.RedisService; import com.ruoyi.system.domain.SysConfig; import com.ruoyi.system.mapper.SysConfigMapper; import com.ruoyi.system.service.ISysConfigService; @@ -22,6 +25,22 @@ public class SysConfigServiceImpl implements ISysConfigService @Autowired private SysConfigMapper configMapper; + @Autowired + private RedisService redisService; + + /** + * 项目启动时,初始化参数到缓存 + */ + @PostConstruct + public void init() + { + List configsList = configMapper.selectConfigList(new SysConfig()); + for (SysConfig config : configsList) + { + redisService.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue()); + } + } + /** * 查询参数配置信息 * @@ -45,10 +64,20 @@ public class SysConfigServiceImpl implements ISysConfigService @Override public String selectConfigByKey(String configKey) { + String configValue = Convert.toStr(redisService.getCacheObject(getCacheKey(configKey))); + if (StringUtils.isNotEmpty(configValue)) + { + return configValue; + } SysConfig config = new SysConfig(); config.setConfigKey(configKey); SysConfig retConfig = configMapper.selectConfig(config); - return StringUtils.isNotNull(retConfig) ? retConfig.getConfigValue() : ""; + if (StringUtils.isNotNull(retConfig)) + { + redisService.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue()); + return retConfig.getConfigValue(); + } + return StringUtils.EMPTY; } /** @@ -72,7 +101,12 @@ public class SysConfigServiceImpl implements ISysConfigService @Override public int insertConfig(SysConfig config) { - return configMapper.insertConfig(config); + int row = configMapper.insertConfig(config); + if (row > 0) + { + redisService.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue()); + } + return row; } /** @@ -84,31 +118,39 @@ public class SysConfigServiceImpl implements ISysConfigService @Override public int updateConfig(SysConfig config) { - return configMapper.updateConfig(config); + int row = configMapper.updateConfig(config); + if (row > 0) + { + redisService.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue()); + } + return row; } /** - * 删除参数配置信息 + * 批量删除参数信息 * - * @param configId 参数ID + * @param configIds 需要删除的参数ID * @return 结果 */ @Override - public int deleteConfigById(Long configId) + public int deleteConfigByIds(Long[] configIds) { - return configMapper.deleteConfigById(configId); + int count = configMapper.deleteConfigByIds(configIds); + if (count > 0) + { + Collection keys = redisService.keys(Constants.SYS_CONFIG_KEY + "*"); + redisService.deleteObject(keys); + } + return count; } /** - * 批量删除参数信息 - * - * @param configIds 需要删除的参数ID - * @return 结果 + * 清空缓存数据 */ - @Override - public int deleteConfigByIds(Long[] configIds) + public void clearCache() { - return configMapper.deleteConfigByIds(configIds); + Collection keys = redisService.keys(Constants.SYS_CONFIG_KEY + "*"); + redisService.deleteObject(keys); } /** @@ -128,4 +170,15 @@ public class SysConfigServiceImpl implements ISysConfigService } return UserConstants.UNIQUE; } + + /** + * 设置cache key + * + * @param configKey 参数键 + * @return 缓存键key + */ + private String getCacheKey(String configKey) + { + return Constants.SYS_CONFIG_KEY + configKey; + } } diff --git a/ruoyi-ui/src/api/system/config.js b/ruoyi-ui/src/api/system/config.js index ba38d1c..00c96d5 100644 --- a/ruoyi-ui/src/api/system/config.js +++ b/ruoyi-ui/src/api/system/config.js @@ -50,3 +50,11 @@ export function delConfig(configId) { method: 'delete' }) } + +// 清理参数缓存 +export function clearCache() { + return request({ + url: '/system/config/clearCache', + method: 'delete' + }) +} diff --git a/ruoyi-ui/src/views/system/config/index.vue b/ruoyi-ui/src/views/system/config/index.vue index ed6ece3..657e85b 100644 --- a/ruoyi-ui/src/views/system/config/index.vue +++ b/ruoyi-ui/src/views/system/config/index.vue @@ -88,6 +88,15 @@ v-hasPermi="['system:config:export']" >导出 + + 清理缓存 + @@ -165,7 +174,7 @@