瀏覽代碼

参数管理支持缓存操作

RuoYi 5 年之前
父節點
當前提交
d5dd5b6b7a

+ 5 - 20
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/Constants.java

@@ -72,26 +72,6 @@ public class Constants
72 72
     public static final String IS_ASC = "isAsc";
73 73
 
74 74
     /**
75
-     * 参数管理 cache name
76
-     */
77
-    public static final String SYS_CONFIG_CACHE = "sys-config";
78
-
79
-    /**
80
-     * 参数管理 cache key
81
-     */
82
-    public static final String SYS_CONFIG_KEY = "sys_config:";
83
-
84
-    /**
85
-     * 字典管理 cache name
86
-     */
87
-    public static final String SYS_DICT_CACHE = "sys-dict";
88
-
89
-    /**
90
-     * 字典管理 cache key
91
-     */
92
-    public static final String SYS_DICT_KEY = "sys_dict:";
93
-
94
-    /**
95 75
      * 验证码 redis key
96 76
      */
97 77
     public static final String CAPTCHA_CODE_KEY = "captcha_codes:";
@@ -102,6 +82,11 @@ public class Constants
102 82
     public static final Integer CAPTCHA_EXPIRATION = 2;
103 83
 
104 84
     /**
85
+     * 参数管理 cache key
86
+     */
87
+    public static final String SYS_CONFIG_KEY = "sys_config:";
88
+
89
+    /**
105 90
      * 资源映射路径 前缀
106 91
      */
107 92
     public static final String RESOURCE_PREFIX = "/profile";

+ 2 - 0
ruoyi-common/ruoyi-common-redis/src/main/java/com/ruoyi/common/redis/configure/RedisConfig.java

@@ -1,5 +1,6 @@
1 1
 package com.ruoyi.common.redis.configure;
2 2
 
3
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3 4
 import org.springframework.cache.annotation.CachingConfigurerSupport;
4 5
 import org.springframework.cache.annotation.EnableCaching;
5 6
 import org.springframework.context.annotation.Bean;
@@ -21,6 +22,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
21 22
 public class RedisConfig extends CachingConfigurerSupport
22 23
 {
23 24
     @Bean
25
+    @ConditionalOnMissingBean(name = "redisTemplate")
24 26
     @SuppressWarnings(value = { "unchecked", "rawtypes", "deprecation" })
25 27
     public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
26 28
     {

+ 1 - 0
ruoyi-common/ruoyi-common-redis/src/main/resources/META-INF/spring.factories

@@ -1,4 +1,5 @@
1 1
 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2
+  com.ruoyi.common.redis.configure.RedisConfig,\
2 3
   com.ruoyi.common.redis.service.RedisService
3 4
 
4 5
   

+ 6 - 0
ruoyi-modules/ruoyi-system/pom.xml

@@ -77,6 +77,12 @@
77 77
             <groupId>com.ruoyi</groupId>
78 78
             <artifactId>ruoyi-common-swagger</artifactId>
79 79
         </dependency>
80
+        
81
+        <!-- RuoYi Common Redis-->
82
+        <dependency>
83
+            <groupId>com.ruoyi</groupId>
84
+            <artifactId>ruoyi-common-redis</artifactId>
85
+        </dependency>
80 86
 
81 87
     </dependencies>
82 88
 

+ 13 - 1
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysConfigController.java

@@ -48,7 +48,7 @@ public class SysConfigController extends BaseController
48 48
         List<SysConfig> list = configService.selectConfigList(config);
49 49
         return getDataTable(list);
50 50
     }
51
-    
51
+
52 52
     @Log(title = "参数管理", businessType = BusinessType.EXPORT)
53 53
     @PreAuthorize("@ss.hasPermi('system:config:export')")
54 54
     @PostMapping("/export")
@@ -120,4 +120,16 @@ public class SysConfigController extends BaseController
120 120
     {
121 121
         return toAjax(configService.deleteConfigByIds(configIds));
122 122
     }
123
+
124
+    /**
125
+     * 清空缓存
126
+     */
127
+    @PreAuthorize("@ss.hasPermi('system:config:remove')")
128
+    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
129
+    @DeleteMapping("/clearCache")
130
+    public AjaxResult clearCache()
131
+    {
132
+        configService.clearCache();
133
+        return AjaxResult.success();
134
+    }
123 135
 }

+ 5 - 8
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysConfigService.java

@@ -52,14 +52,6 @@ public interface ISysConfigService
52 52
     public int updateConfig(SysConfig config);
53 53
 
54 54
     /**
55
-     * 删除参数配置信息
56
-     * 
57
-     * @param configId 参数ID
58
-     * @return 结果
59
-     */
60
-    public int deleteConfigById(Long configId);
61
-
62
-    /**
63 55
      * 批量删除参数信息
64 56
      * 
65 57
      * @param configIds 需要删除的参数ID
@@ -68,6 +60,11 @@ public interface ISysConfigService
68 60
     public int deleteConfigByIds(Long[] configIds);
69 61
 
70 62
     /**
63
+     * 清空缓存数据
64
+     */
65
+    public void clearCache();
66
+
67
+    /**
71 68
      * 校验参数键名是否唯一
72 69
      * 
73 70
      * @param config 参数信息

+ 69 - 16
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java

@@ -1,12 +1,15 @@
1 1
 package com.ruoyi.system.service.impl;
2 2
 
3
+import java.util.Collection;
3 4
 import java.util.List;
4
-
5
+import javax.annotation.PostConstruct;
5 6
 import org.springframework.beans.factory.annotation.Autowired;
6 7
 import org.springframework.stereotype.Service;
7
-
8
+import com.ruoyi.common.core.constant.Constants;
8 9
 import com.ruoyi.common.core.constant.UserConstants;
10
+import com.ruoyi.common.core.text.Convert;
9 11
 import com.ruoyi.common.core.utils.StringUtils;
12
+import com.ruoyi.common.redis.service.RedisService;
10 13
 import com.ruoyi.system.domain.SysConfig;
11 14
 import com.ruoyi.system.mapper.SysConfigMapper;
12 15
 import com.ruoyi.system.service.ISysConfigService;
@@ -22,6 +25,22 @@ public class SysConfigServiceImpl implements ISysConfigService
22 25
     @Autowired
23 26
     private SysConfigMapper configMapper;
24 27
 
28
+    @Autowired
29
+    private RedisService redisService;
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
+            redisService.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
41
+        }
42
+    }
43
+
25 44
     /**
26 45
      * 查询参数配置信息
27 46
      * 
@@ -45,10 +64,20 @@ public class SysConfigServiceImpl implements ISysConfigService
45 64
     @Override
46 65
     public String selectConfigByKey(String configKey)
47 66
     {
67
+        String configValue = Convert.toStr(redisService.getCacheObject(getCacheKey(configKey)));
68
+        if (StringUtils.isNotEmpty(configValue))
69
+        {
70
+            return configValue;
71
+        }
48 72
         SysConfig config = new SysConfig();
49 73
         config.setConfigKey(configKey);
50 74
         SysConfig retConfig = configMapper.selectConfig(config);
51
-        return StringUtils.isNotNull(retConfig) ? retConfig.getConfigValue() : "";
75
+        if (StringUtils.isNotNull(retConfig))
76
+        {
77
+            redisService.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue());
78
+            return retConfig.getConfigValue();
79
+        }
80
+        return StringUtils.EMPTY;
52 81
     }
53 82
 
54 83
     /**
@@ -72,7 +101,12 @@ public class SysConfigServiceImpl implements ISysConfigService
72 101
     @Override
73 102
     public int insertConfig(SysConfig config)
74 103
     {
75
-        return configMapper.insertConfig(config);
104
+        int row = configMapper.insertConfig(config);
105
+        if (row > 0)
106
+        {
107
+            redisService.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
108
+        }
109
+        return row;
76 110
     }
77 111
 
78 112
     /**
@@ -84,31 +118,39 @@ public class SysConfigServiceImpl implements ISysConfigService
84 118
     @Override
85 119
     public int updateConfig(SysConfig config)
86 120
     {
87
-        return configMapper.updateConfig(config);
121
+        int row = configMapper.updateConfig(config);
122
+        if (row > 0)
123
+        {
124
+            redisService.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
125
+        }
126
+        return row;
88 127
     }
89 128
 
90 129
     /**
91
-     * 删除参数配置信息
130
+     * 批量删除参数信息
92 131
      * 
93
-     * @param configId 参数ID
132
+     * @param configIds 需要删除的参数ID
94 133
      * @return 结果
95 134
      */
96 135
     @Override
97
-    public int deleteConfigById(Long configId)
136
+    public int deleteConfigByIds(Long[] configIds)
98 137
     {
99
-        return configMapper.deleteConfigById(configId);
138
+        int count = configMapper.deleteConfigByIds(configIds);
139
+        if (count > 0)
140
+        {
141
+            Collection<String> keys = redisService.keys(Constants.SYS_CONFIG_KEY + "*");
142
+            redisService.deleteObject(keys);
143
+        }
144
+        return count;
100 145
     }
101 146
 
102 147
     /**
103
-     * 批量删除参数信息
104
-     * 
105
-     * @param configIds 需要删除的参数ID
106
-     * @return 结果
148
+     * 清空缓存数据
107 149
      */
108
-    @Override
109
-    public int deleteConfigByIds(Long[] configIds)
150
+    public void clearCache()
110 151
     {
111
-        return configMapper.deleteConfigByIds(configIds);
152
+        Collection<String> keys = redisService.keys(Constants.SYS_CONFIG_KEY + "*");
153
+        redisService.deleteObject(keys);
112 154
     }
113 155
 
114 156
     /**
@@ -128,4 +170,15 @@ public class SysConfigServiceImpl implements ISysConfigService
128 170
         }
129 171
         return UserConstants.UNIQUE;
130 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
+    }
131 184
 }

+ 8 - 0
ruoyi-ui/src/api/system/config.js

@@ -50,3 +50,11 @@ export function delConfig(configId) {
50 50
     method: 'delete'
51 51
   })
52 52
 }
53
+
54
+// 清理参数缓存
55
+export function clearCache() {
56
+  return request({
57
+    url: '/system/config/clearCache',
58
+    method: 'delete'
59
+  })
60
+}

+ 21 - 1
ruoyi-ui/src/views/system/config/index.vue

@@ -88,6 +88,15 @@
88 88
           v-hasPermi="['system:config:export']"
89 89
         >导出</el-button>
90 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 100
     </el-row>
92 101
 
93 102
     <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
@@ -165,7 +174,7 @@
165 174
 </template>
166 175
 
167 176
 <script>
168
-import { listConfig, getConfig, delConfig, addConfig, updateConfig } from "@/api/system/config";
177
+import { listConfig, getConfig, delConfig, addConfig, updateConfig, clearCache } from "@/api/system/config";
169 178
 
170 179
 export default {
171 180
   name: "Config",
@@ -329,6 +338,17 @@ export default {
329 338
       this.download('system/config/export', {
330 339
         ...this.queryParams
331 340
       }, `config_${new Date().getTime()}.xlsx`)
341
+    },
342
+    /** 清理缓存按钮操作 */
343
+    handleClearCache() {
344
+      const queryParams = this.queryParams;
345
+      clearCache().then(response => {
346
+        if (response.code === 200) {
347
+          this.msgSuccess("清理成功");
348
+        } else {
349
+          this.msgError(response.msg);
350
+        }
351
+      });
332 352
     }
333 353
   }
334 354
 };

File diff suppressed because it is too large
+ 1 - 1
sql/ry_config.sql