diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysConfigController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysConfigController.java index 139469d2..1937c795 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysConfigController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysConfigController.java @@ -132,6 +132,19 @@ public class SysConfigController extends BaseController return toAjax(configService.deleteConfigByIds(ids)); } + /** + * 清空缓存 + */ + @RequiresPermissions("system:config:remove") + @Log(title = "参数管理", businessType = BusinessType.CLEAN) + @GetMapping("/clearCache") + @ResponseBody + public AjaxResult clearCache() + { + configService.clearCache(); + return success(); + } + /** * 校验参数键名 */ diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysIndexController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysIndexController.java index 8c0a0eed..28d4bf06 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysIndexController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysIndexController.java @@ -10,6 +10,7 @@ import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.framework.util.ShiroUtils; import com.ruoyi.system.domain.SysMenu; import com.ruoyi.system.domain.SysUser; +import com.ruoyi.system.service.ISysConfigService; import com.ruoyi.system.service.ISysMenuService; /** @@ -23,6 +24,9 @@ public class SysIndexController extends BaseController @Autowired private ISysMenuService menuService; + @Autowired + private ISysConfigService configService; + // 系统首页 @GetMapping("/index") public String index(ModelMap mmap) @@ -33,6 +37,8 @@ public class SysIndexController extends BaseController List menus = menuService.selectMenusByUser(user); mmap.put("menus", menus); mmap.put("user", user); + mmap.put("sideTheme", configService.selectConfigByKey("sys.index.sideTheme")); + mmap.put("skinName", configService.selectConfigByKey("sys.index.skinName")); mmap.put("copyrightYear", Global.getCopyrightYear()); mmap.put("demoEnabled", Global.isDemoEnabled()); return "index"; diff --git a/ruoyi-admin/src/main/resources/ehcache/ehcache-shiro.xml b/ruoyi-admin/src/main/resources/ehcache/ehcache-shiro.xml index 0ff3c1e2..1c028892 100644 --- a/ruoyi-admin/src/main/resources/ehcache/ehcache-shiro.xml +++ b/ruoyi-admin/src/main/resources/ehcache/ehcache-shiro.xml @@ -42,6 +42,14 @@ statistics="true"> + + + + - + \ No newline at end of file diff --git a/ruoyi-admin/src/main/resources/static/ruoyi/js/ry-ui.js b/ruoyi-admin/src/main/resources/static/ruoyi/js/ry-ui.js index 4714d896..53d255f0 100644 --- a/ruoyi-admin/src/main/resources/static/ruoyi/js/ry-ui.js +++ b/ruoyi-admin/src/main/resources/static/ruoyi/js/ry-ui.js @@ -1105,7 +1105,9 @@ var table = { } else if (result.code == web_status.SUCCESS && table.options.type == table_type.bootstrapTreeTable) { $.modal.msgSuccess(result.msg); $.treeTable.refresh(); - } else if (result.code == web_status.WARNING) { + } else if (result.code == web_status.SUCCESS && table.option.type == undefined) { + $.modal.msgSuccess(result.msg) + } else if (result.code == web_status.WARNING) { $.modal.alertWarning(result.msg) } else { $.modal.alertError(result.msg); diff --git a/ruoyi-admin/src/main/resources/templates/include.html b/ruoyi-admin/src/main/resources/templates/include.html index 08fa0427..6145b550 100644 --- a/ruoyi-admin/src/main/resources/templates/include.html +++ b/ruoyi-admin/src/main/resources/templates/include.html @@ -74,7 +74,7 @@
- +
diff --git a/ruoyi-admin/src/main/resources/templates/index.html b/ruoyi-admin/src/main/resources/templates/index.html index c017d0b0..1d9eb2a9 100644 --- a/ruoyi-admin/src/main/resources/templates/index.html +++ b/ruoyi-admin/src/main/resources/templates/index.html @@ -267,10 +267,8 @@ if($.common.isNotEmpty(skin)){ $("body").addClass(skin.split('|')[0]); $("body").addClass(skin.split('|')[1]); } else { - var sideTheme = [[${@config.getKey('sys.index.sideTheme')}]]; - var skinName = [[${@config.getKey('sys.index.skinName')}]]; - $("body").addClass(sideTheme); - $("body").addClass(skinName); + $("body").addClass([[${sideTheme}]]); + $("body").addClass([[${skinName}]]); } /* 用户管理-重置密码 */ diff --git a/ruoyi-admin/src/main/resources/templates/system/config/config.html b/ruoyi-admin/src/main/resources/templates/system/config/config.html index 833c2bea..d5bf033c 100644 --- a/ruoyi-admin/src/main/resources/templates/system/config/config.html +++ b/ruoyi-admin/src/main/resources/templates/system/config/config.html @@ -50,6 +50,9 @@ 导出 + + 清理缓存 +
@@ -131,6 +134,10 @@ }; $.table.init(options); }); + + function clearCache() { + $.operate.get(prefix + "/clearCache"); + } \ No newline at end of file diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java b/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java index 5ddd4cab..98f2f62c 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java @@ -57,6 +57,16 @@ 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:"; + /** * 资源映射路径 前缀 */ diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/CacheUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/CacheUtils.java new file mode 100644 index 00000000..80dcda39 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/CacheUtils.java @@ -0,0 +1,187 @@ +package com.ruoyi.common.utils; + +import java.util.Iterator; +import java.util.Set; +import org.apache.shiro.cache.Cache; +import org.apache.shiro.cache.CacheManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.ruoyi.common.utils.spring.SpringUtils; + +/** + * Cache工具类 + * + * @author ruoyi + */ +public class CacheUtils +{ + private static Logger logger = LoggerFactory.getLogger(CacheUtils.class); + + private static CacheManager cacheManager = SpringUtils.getBean(CacheManager.class); + + private static final String SYS_CACHE = "sys-cache"; + + /** + * 获取SYS_CACHE缓存 + * + * @param key + * @return + */ + public static Object get(String key) + { + return get(SYS_CACHE, key); + } + + /** + * 获取SYS_CACHE缓存 + * + * @param key + * @param defaultValue + * @return + */ + public static Object get(String key, Object defaultValue) + { + Object value = get(key); + return value != null ? value : defaultValue; + } + + /** + * 写入SYS_CACHE缓存 + * + * @param key + * @return + */ + public static void put(String key, Object value) + { + put(SYS_CACHE, key, value); + } + + /** + * 从SYS_CACHE缓存中移除 + * + * @param key + * @return + */ + public static void remove(String key) + { + remove(SYS_CACHE, key); + } + + /** + * 获取缓存 + * + * @param cacheName + * @param key + * @return + */ + public static Object get(String cacheName, String key) + { + return getCache(cacheName).get(getKey(key)); + } + + /** + * 获取缓存 + * + * @param cacheName + * @param key + * @param defaultValue + * @return + */ + public static Object get(String cacheName, String key, Object defaultValue) + { + Object value = get(cacheName, getKey(key)); + return value != null ? value : defaultValue; + } + + /** + * 写入缓存 + * + * @param cacheName + * @param key + * @param value + */ + public static void put(String cacheName, String key, Object value) + { + getCache(cacheName).put(getKey(key), value); + } + + /** + * 从缓存中移除 + * + * @param cacheName + * @param key + */ + public static void remove(String cacheName, String key) + { + getCache(cacheName).remove(getKey(key)); + } + + /** + * 从缓存中移除所有 + * + * @param cacheName + */ + public static void removeAll(String cacheName) + { + Cache cache = getCache(cacheName); + Set keys = cache.keys(); + for (Iterator it = keys.iterator(); it.hasNext();) + { + cache.remove(it.next()); + } + logger.info("清理缓存: {} => {}", cacheName, keys); + } + + /** + * 从缓存中移除指定key + * + * @param keys + */ + public static void removeByKeys(Set keys) + { + removeByKeys(SYS_CACHE, keys); + } + + /** + * 从缓存中移除指定key + * + * @param cacheName + * @param keys + */ + public static void removeByKeys(String cacheName, Set keys) + { + for (Iterator it = keys.iterator(); it.hasNext();) + { + remove(it.next()); + } + logger.info("清理缓存: {} => {}", cacheName, keys); + } + + /** + * 获取缓存键名 + * + * @param key + * @return + */ + private static String getKey(String key) + { + return key; + } + + /** + * 获得一个Cache,没有则显示日志。 + * + * @param cacheName + * @return + */ + private static Cache getCache(String cacheName) + { + Cache cache = cacheManager.getCache(cacheName); + if (cache == null) + { + throw new RuntimeException("当前系统中没有定义“" + cacheName + "”这个缓存。"); + } + return cache; + } + +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysConfigService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysConfigService.java index 949bf44c..d27b3f51 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysConfigService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysConfigService.java @@ -58,6 +58,11 @@ public interface ISysConfigService */ public int deleteConfigByIds(String ids); + /** + * 清空缓存数据 + */ + public void clearCache(); + /** * 校验参数键名是否唯一 * diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java index 8b2a02d0..9f4189dc 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java @@ -1,10 +1,13 @@ package com.ruoyi.system.service.impl; import java.util.List; +import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import com.ruoyi.common.constant.Constants; import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.core.text.Convert; +import com.ruoyi.common.utils.CacheUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.system.domain.SysConfig; import com.ruoyi.system.mapper.SysConfigMapper; @@ -21,6 +24,19 @@ public class SysConfigServiceImpl implements ISysConfigService @Autowired private SysConfigMapper configMapper; + /** + * 项目启动时,初始化参数到缓存 + */ + @PostConstruct + public void init() + { + List configsList = configMapper.selectConfigList(new SysConfig()); + for (SysConfig config : configsList) + { + CacheUtils.put(getCacheName(), getCacheKey(config.getConfigKey()), config.getConfigValue()); + } + } + /** * 查询参数配置信息 * @@ -44,10 +60,20 @@ public class SysConfigServiceImpl implements ISysConfigService @Override public String selectConfigByKey(String configKey) { + String configValue = Convert.toStr(CacheUtils.get(getCacheName(), 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)) + { + CacheUtils.put(getCacheName(), getCacheKey(configKey), retConfig.getConfigValue()); + return retConfig.getConfigValue(); + } + return StringUtils.EMPTY; } /** @@ -71,7 +97,12 @@ public class SysConfigServiceImpl implements ISysConfigService @Override public int insertConfig(SysConfig config) { - return configMapper.insertConfig(config); + int row = configMapper.insertConfig(config); + if (row > 0) + { + CacheUtils.put(getCacheName(), getCacheKey(config.getConfigKey()), config.getConfigValue()); + } + return row; } /** @@ -83,7 +114,12 @@ public class SysConfigServiceImpl implements ISysConfigService @Override public int updateConfig(SysConfig config) { - return configMapper.updateConfig(config); + int row = configMapper.updateConfig(config); + if (row > 0) + { + CacheUtils.put(getCacheName(), getCacheKey(config.getConfigKey()), config.getConfigValue()); + } + return row; } /** @@ -95,7 +131,21 @@ public class SysConfigServiceImpl implements ISysConfigService @Override public int deleteConfigByIds(String ids) { - return configMapper.deleteConfigByIds(Convert.toStrArray(ids)); + int count = configMapper.deleteConfigByIds(Convert.toStrArray(ids)); + if (count > 0) + { + + CacheUtils.removeAll(getCacheName()); + } + return count; + } + + /** + * 清空缓存数据 + */ + public void clearCache() + { + CacheUtils.removeAll(getCacheName()); } /** @@ -115,4 +165,25 @@ public class SysConfigServiceImpl implements ISysConfigService } return UserConstants.CONFIG_KEY_UNIQUE; } + + /** + * 获取cache name + * + * @return 缓存名 + */ + private String getCacheName() + { + return Constants.SYS_CONFIG_CACHE; + } + + /** + * 设置cache key + * + * @param configKey 参数键 + * @return 缓存键key + */ + private String getCacheKey(String configKey) + { + return Constants.SYS_CONFIG_KEY + configKey; + } }