作者 RuoYi

参数管理支持缓存操作

@@ -51,6 +51,14 @@ export function delConfig(configId) { @@ -51,6 +51,14 @@ export function delConfig(configId) {
51 }) 51 })
52 } 52 }
53 53
  54 +// 清理参数缓存
  55 +export function clearCache() {
  56 + return request({
  57 + url: '/system/config/clearCache',
  58 + method: 'delete'
  59 + })
  60 +}
  61 +
54 // 导出参数 62 // 导出参数
55 export function exportConfig(query) { 63 export function exportConfig(query) {
56 return request({ 64 return request({
@@ -88,6 +88,15 @@ @@ -88,6 +88,15 @@
88 v-hasPermi="['system:config:export']" 88 v-hasPermi="['system:config:export']"
89 >导出</el-button> 89 >导出</el-button>
90 </el-col> 90 </el-col>
  91 + <el-col :span="1.5">
  92 + <el-button
  93 + type="danger"
  94 + icon="el-icon-refresh"
  95 + size="mini"
  96 + @click="handleClearCache"
  97 + v-hasPermi="['system:config:remove']"
  98 + >清理缓存</el-button>
  99 + </el-col>
91 </el-row> 100 </el-row>
92 101
93 <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange"> 102 <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
@@ -165,7 +174,7 @@ @@ -165,7 +174,7 @@
165 </template> 174 </template>
166 175
167 <script> 176 <script>
168 -import { listConfig, getConfig, delConfig, addConfig, updateConfig, exportConfig } from "@/api/system/config"; 177 +import { listConfig, getConfig, delConfig, addConfig, updateConfig, exportConfig, clearCache } from "@/api/system/config";
169 178
170 export default { 179 export default {
171 name: "Config", 180 name: "Config",
@@ -340,6 +349,17 @@ export default { @@ -340,6 +349,17 @@ export default {
340 }).then(response => { 349 }).then(response => {
341 this.download(response.msg); 350 this.download(response.msg);
342 }).catch(function() {}); 351 }).catch(function() {});
  352 + },
  353 + /** 清理缓存按钮操作 */
  354 + handleClearCache() {
  355 + const queryParams = this.queryParams;
  356 + clearCache().then(response => {
  357 + if (response.code === 200) {
  358 + this.msgSuccess("清理成功");
  359 + } else {
  360 + this.msgError(response.msg);
  361 + }
  362 + });
343 } 363 }
344 } 364 }
345 }; 365 };
@@ -110,6 +110,11 @@ public class Constants @@ -110,6 +110,11 @@ public class Constants
110 public static final String JWT_AUTHORITIES = "authorities"; 110 public static final String JWT_AUTHORITIES = "authorities";
111 111
112 /** 112 /**
  113 + * 参数管理 cache key
  114 + */
  115 + public static final String SYS_CONFIG_KEY = "sys_config:";
  116 +
  117 + /**
113 * 资源映射路径 前缀 118 * 资源映射路径 前缀
114 */ 119 */
115 public static final String RESOURCE_PREFIX = "/profile"; 120 public static final String RESOURCE_PREFIX = "/profile";
@@ -118,4 +118,16 @@ public class SysConfigController extends BaseController @@ -118,4 +118,16 @@ public class SysConfigController extends BaseController
118 { 118 {
119 return toAjax(configService.deleteConfigByIds(configIds)); 119 return toAjax(configService.deleteConfigByIds(configIds));
120 } 120 }
  121 +
  122 + /**
  123 + * 清空缓存
  124 + */
  125 + @PreAuthorize("@ss.hasPermi('system:config:remove')")
  126 + @Log(title = "参数管理", businessType = BusinessType.CLEAN)
  127 + @DeleteMapping("/clearCache")
  128 + public AjaxResult clearCache()
  129 + {
  130 + configService.clearCache();
  131 + return AjaxResult.success();
  132 + }
121 } 133 }
@@ -51,14 +51,6 @@ public interface ISysConfigService @@ -51,14 +51,6 @@ public interface ISysConfigService
51 public int updateConfig(SysConfig config); 51 public int updateConfig(SysConfig config);
52 52
53 /** 53 /**
54 - * 删除参数配置信息  
55 - *  
56 - * @param configId 参数ID  
57 - * @return 结果  
58 - */  
59 - public int deleteConfigById(Long configId);  
60 -  
61 - /**  
62 * 批量删除参数信息 54 * 批量删除参数信息
63 * 55 *
64 * @param configIds 需要删除的参数ID 56 * @param configIds 需要删除的参数ID
@@ -67,6 +59,11 @@ public interface ISysConfigService @@ -67,6 +59,11 @@ public interface ISysConfigService
67 public int deleteConfigByIds(Long[] configIds); 59 public int deleteConfigByIds(Long[] configIds);
68 60
69 /** 61 /**
  62 + * 清空缓存数据
  63 + */
  64 + public void clearCache();
  65 +
  66 + /**
70 * 校验参数键名是否唯一 67 * 校验参数键名是否唯一
71 * 68 *
72 * @param config 参数信息 69 * @param config 参数信息
1 package com.ruoyi.project.system.service.impl; 1 package com.ruoyi.project.system.service.impl;
2 2
  3 +import java.util.Collection;
3 import java.util.List; 4 import java.util.List;
  5 +import javax.annotation.PostConstruct;
4 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.beans.factory.annotation.Autowired;
5 import org.springframework.stereotype.Service; 7 import org.springframework.stereotype.Service;
  8 +import com.ruoyi.common.constant.Constants;
6 import com.ruoyi.common.constant.UserConstants; 9 import com.ruoyi.common.constant.UserConstants;
  10 +import com.ruoyi.common.core.text.Convert;
7 import com.ruoyi.common.utils.StringUtils; 11 import com.ruoyi.common.utils.StringUtils;
  12 +import com.ruoyi.framework.redis.RedisCache;
8 import com.ruoyi.project.system.domain.SysConfig; 13 import com.ruoyi.project.system.domain.SysConfig;
9 import com.ruoyi.project.system.mapper.SysConfigMapper; 14 import com.ruoyi.project.system.mapper.SysConfigMapper;
10 import com.ruoyi.project.system.service.ISysConfigService; 15 import com.ruoyi.project.system.service.ISysConfigService;
@@ -20,6 +25,22 @@ public class SysConfigServiceImpl implements ISysConfigService @@ -20,6 +25,22 @@ public class SysConfigServiceImpl implements ISysConfigService
20 @Autowired 25 @Autowired
21 private SysConfigMapper configMapper; 26 private SysConfigMapper configMapper;
22 27
  28 + @Autowired
  29 + private RedisCache redisCache;
  30 +
  31 + /**
  32 + * 项目启动时,初始化参数到缓存
  33 + */
  34 + @PostConstruct
  35 + public void init()
  36 + {
  37 + List<SysConfig> configsList = configMapper.selectConfigList(new SysConfig());
  38 + for (SysConfig config : configsList)
  39 + {
  40 + redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
  41 + }
  42 + }
  43 +
23 /** 44 /**
24 * 查询参数配置信息 45 * 查询参数配置信息
25 * 46 *
@@ -43,10 +64,20 @@ public class SysConfigServiceImpl implements ISysConfigService @@ -43,10 +64,20 @@ public class SysConfigServiceImpl implements ISysConfigService
43 @Override 64 @Override
44 public String selectConfigByKey(String configKey) 65 public String selectConfigByKey(String configKey)
45 { 66 {
  67 + String configValue = Convert.toStr(redisCache.getCacheObject(getCacheKey(configKey)));
  68 + if (StringUtils.isNotEmpty(configValue))
  69 + {
  70 + return configValue;
  71 + }
46 SysConfig config = new SysConfig(); 72 SysConfig config = new SysConfig();
47 config.setConfigKey(configKey); 73 config.setConfigKey(configKey);
48 SysConfig retConfig = configMapper.selectConfig(config); 74 SysConfig retConfig = configMapper.selectConfig(config);
49 - return StringUtils.isNotNull(retConfig) ? retConfig.getConfigValue() : ""; 75 + if (StringUtils.isNotNull(retConfig))
  76 + {
  77 + redisCache.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue());
  78 + return retConfig.getConfigValue();
  79 + }
  80 + return StringUtils.EMPTY;
50 } 81 }
51 82
52 /** 83 /**
@@ -70,7 +101,12 @@ public class SysConfigServiceImpl implements ISysConfigService @@ -70,7 +101,12 @@ public class SysConfigServiceImpl implements ISysConfigService
70 @Override 101 @Override
71 public int insertConfig(SysConfig config) 102 public int insertConfig(SysConfig config)
72 { 103 {
73 - return configMapper.insertConfig(config); 104 + int row = configMapper.insertConfig(config);
  105 + if (row > 0)
  106 + {
  107 + redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
  108 + }
  109 + return row;
74 } 110 }
75 111
76 /** 112 /**
@@ -82,31 +118,39 @@ public class SysConfigServiceImpl implements ISysConfigService @@ -82,31 +118,39 @@ public class SysConfigServiceImpl implements ISysConfigService
82 @Override 118 @Override
83 public int updateConfig(SysConfig config) 119 public int updateConfig(SysConfig config)
84 { 120 {
85 - return configMapper.updateConfig(config); 121 + int row = configMapper.updateConfig(config);
  122 + if (row > 0)
  123 + {
  124 + redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
  125 + }
  126 + return row;
86 } 127 }
87 128
88 /** 129 /**
89 - * 删除参数配置信息 130 + * 批量删除参数信息
90 * 131 *
91 - * @param configId 参数ID 132 + * @param configIds 需要删除的参数ID
92 * @return 结果 133 * @return 结果
93 */ 134 */
94 @Override 135 @Override
95 - public int deleteConfigById(Long configId) 136 + public int deleteConfigByIds(Long[] configIds)
96 { 137 {
97 - return configMapper.deleteConfigById(configId); 138 + int count = configMapper.deleteConfigByIds(configIds);
  139 + if (count > 0)
  140 + {
  141 + Collection<String> keys = redisCache.keys(Constants.SYS_CONFIG_KEY + "*");
  142 + redisCache.deleteObject(keys);
  143 + }
  144 + return count;
98 } 145 }
99 146
100 /** 147 /**
101 - * 批量删除参数信息  
102 - *  
103 - * @param configIds 需要删除的参数ID  
104 - * @return 结果 148 + * 清空缓存数据
105 */ 149 */
106 - @Override  
107 - public int deleteConfigByIds(Long[] configIds) 150 + public void clearCache()
108 { 151 {
109 - return configMapper.deleteConfigByIds(configIds); 152 + Collection<String> keys = redisCache.keys(Constants.SYS_CONFIG_KEY + "*");
  153 + redisCache.deleteObject(keys);
110 } 154 }
111 155
112 /** 156 /**
@@ -126,4 +170,15 @@ public class SysConfigServiceImpl implements ISysConfigService @@ -126,4 +170,15 @@ public class SysConfigServiceImpl implements ISysConfigService
126 } 170 }
127 return UserConstants.UNIQUE; 171 return UserConstants.UNIQUE;
128 } 172 }
  173 +
  174 + /**
  175 + * 设置cache key
  176 + *
  177 + * @param configKey 参数键
  178 + * @return 缓存键key
  179 + */
  180 + private String getCacheKey(String configKey)
  181 + {
  182 + return Constants.SYS_CONFIG_KEY + configKey;
  183 + }
129 } 184 }