浏览代码

优化参数&字典缓存操作

RuoYi 4 年之前
父节点
当前提交
67feb9947e

+ 6 - 5
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysConfigController.java

@@ -117,18 +117,19 @@ public class SysConfigController extends BaseController
117 117
     @DeleteMapping("/{configIds}")
118 118
     public AjaxResult remove(@PathVariable Long[] configIds)
119 119
     {
120
-        return toAjax(configService.deleteConfigByIds(configIds));
120
+        configService.deleteConfigByIds(configIds);
121
+        return success();
121 122
     }
122 123
 
123 124
     /**
124
-     * 清空缓存
125
+     * 刷新参数缓存
125 126
      */
126 127
     @PreAuthorize(hasPermi = "system:config:remove")
127 128
     @Log(title = "参数管理", businessType = BusinessType.CLEAN)
128
-    @DeleteMapping("/clearCache")
129
-    public AjaxResult clearCache()
129
+    @DeleteMapping("/refreshCache")
130
+    public AjaxResult refreshCache()
130 131
     {
131
-        configService.clearCache();
132
+        configService.resetConfigCache();
132 133
         return AjaxResult.success();
133 134
     }
134 135
 }

+ 2 - 1
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysDictDataController.java

@@ -117,6 +117,7 @@ public class SysDictDataController extends BaseController
117 117
     @DeleteMapping("/{dictCodes}")
118 118
     public AjaxResult remove(@PathVariable Long[] dictCodes)
119 119
     {
120
-        return toAjax(dictDataService.deleteDictDataByIds(dictCodes));
120
+        dictDataService.deleteDictDataByIds(dictCodes);
121
+        return success();
121 122
     }
122 123
 }

+ 6 - 5
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysDictTypeController.java

@@ -106,18 +106,19 @@ public class SysDictTypeController extends BaseController
106 106
     @DeleteMapping("/{dictIds}")
107 107
     public AjaxResult remove(@PathVariable Long[] dictIds)
108 108
     {
109
-        return toAjax(dictTypeService.deleteDictTypeByIds(dictIds));
109
+        dictTypeService.deleteDictTypeByIds(dictIds);
110
+        return success();
110 111
     }
111 112
 
112 113
     /**
113
-     * 清空缓存
114
+     * 刷新字典缓存
114 115
      */
115 116
     @PreAuthorize(hasPermi = "system:dict:remove")
116 117
     @Log(title = "字典类型", businessType = BusinessType.CLEAN)
117
-    @DeleteMapping("/clearCache")
118
-    public AjaxResult clearCache()
118
+    @DeleteMapping("/refreshCache")
119
+    public AjaxResult refreshCache()
119 120
     {
120
-        dictTypeService.clearCache();
121
+        dictTypeService.resetDictCache();
121 122
         return AjaxResult.success();
122 123
     }
123 124
 

+ 13 - 3
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysConfigService.java

@@ -57,12 +57,22 @@ public interface ISysConfigService
57 57
      * @param configIds 需要删除的参数ID
58 58
      * @return 结果
59 59
      */
60
-    public int deleteConfigByIds(Long[] configIds);
60
+    public void deleteConfigByIds(Long[] configIds);
61 61
 
62 62
     /**
63
-     * 清空缓存数据
63
+     * 加载参数缓存数据
64 64
      */
65
-    public void clearCache();
65
+    public void loadingConfigCache();
66
+
67
+    /**
68
+     * 清空参数缓存数据
69
+     */
70
+    public void clearConfigCache();
71
+
72
+    /**
73
+     * 重置参数缓存数据
74
+     */
75
+    public void resetConfigCache();
66 76
 
67 77
     /**
68 78
      * 校验参数键名是否唯一

+ 1 - 1
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDictDataService.java

@@ -41,7 +41,7 @@ public interface ISysDictDataService
41 41
      * @param dictCodes 需要删除的字典数据ID
42 42
      * @return 结果
43 43
      */
44
-    public int deleteDictDataByIds(Long[] dictCodes);
44
+    public void deleteDictDataByIds(Long[] dictCodes);
45 45
 
46 46
     /**
47 47
      * 新增保存字典数据信息

+ 13 - 3
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDictTypeService.java

@@ -56,12 +56,22 @@ public interface ISysDictTypeService
56 56
      * @param dictIds 需要删除的字典ID
57 57
      * @return 结果
58 58
      */
59
-    public int deleteDictTypeByIds(Long[] dictIds);
59
+    public void deleteDictTypeByIds(Long[] dictIds);
60 60
 
61 61
     /**
62
-     * 清空缓存数据
62
+     * 加载字典缓存数据
63 63
      */
64
-    public void clearCache();
64
+    public void loadingDictCache();
65
+
66
+    /**
67
+     * 清空字典缓存数据
68
+     */
69
+    public void clearDictCache();
70
+
71
+    /**
72
+     * 重置字典缓存数据
73
+     */
74
+    public void resetDictCache();
65 75
 
66 76
     /**
67 77
      * 新增保存字典类型信息

+ 27 - 13
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java

@@ -35,11 +35,7 @@ public class SysConfigServiceImpl implements ISysConfigService
35 35
     @PostConstruct
36 36
     public void init()
37 37
     {
38
-        List<SysConfig> configsList = configMapper.selectConfigList(new SysConfig());
39
-        for (SysConfig config : configsList)
40
-        {
41
-            redisService.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
42
-        }
38
+        loadingConfigCache();
43 39
     }
44 40
 
45 41
     /**
@@ -134,7 +130,7 @@ public class SysConfigServiceImpl implements ISysConfigService
134 130
      * @return 结果
135 131
      */
136 132
     @Override
137
-    public int deleteConfigByIds(Long[] configIds)
133
+    public void deleteConfigByIds(Long[] configIds)
138 134
     {
139 135
         for (Long configId : configIds)
140 136
         {
@@ -143,27 +139,45 @@ public class SysConfigServiceImpl implements ISysConfigService
143 139
             {
144 140
                 throw new CustomException(String.format("内置参数【%1$s】不能删除 ", config.getConfigKey()));
145 141
             }
142
+            configMapper.deleteConfigById(configId);
143
+            redisService.deleteObject(getCacheKey(config.getConfigKey()));
146 144
         }
147
-        int count = configMapper.deleteConfigByIds(configIds);
148
-        if (count > 0)
145
+    }
146
+
147
+    /**
148
+     * 加载参数缓存数据
149
+     */
150
+    @Override
151
+    public void loadingConfigCache()
152
+    {
153
+        List<SysConfig> configsList = configMapper.selectConfigList(new SysConfig());
154
+        for (SysConfig config : configsList)
149 155
         {
150
-            Collection<String> keys = redisService.keys(Constants.SYS_CONFIG_KEY + "*");
151
-            redisService.deleteObject(keys);
156
+            redisService.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
152 157
         }
153
-        return count;
154 158
     }
155 159
 
156 160
     /**
157
-     * 清空缓存数据
161
+     * 清空参数缓存数据
158 162
      */
159 163
     @Override
160
-    public void clearCache()
164
+    public void clearConfigCache()
161 165
     {
162 166
         Collection<String> keys = redisService.keys(Constants.SYS_CONFIG_KEY + "*");
163 167
         redisService.deleteObject(keys);
164 168
     }
165 169
 
166 170
     /**
171
+     * 重置参数缓存数据
172
+     */
173
+    @Override
174
+    public void resetConfigCache()
175
+    {
176
+        clearConfigCache();
177
+        loadingConfigCache();
178
+    }
179
+
180
+    /**
167 181
      * 校验参数键名是否唯一
168 182
      * 
169 183
      * @param config 参数配置信息

+ 16 - 13
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictDataServiceImpl.java

@@ -63,29 +63,31 @@ public class SysDictDataServiceImpl implements ISysDictDataService
63 63
      * @return 结果
64 64
      */
65 65
     @Override
66
-    public int deleteDictDataByIds(Long[] dictCodes)
66
+    public void deleteDictDataByIds(Long[] dictCodes)
67 67
     {
68
-        int row = dictDataMapper.deleteDictDataByIds(dictCodes);
69
-        if (row > 0)
68
+        for (Long dictCode : dictCodes)
70 69
         {
71
-            DictUtils.clearDictCache();
70
+            SysDictData data = selectDictDataById(dictCode);
71
+            dictDataMapper.deleteDictDataById(dictCode);
72
+            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
73
+            DictUtils.setDictCache(data.getDictType(), dictDatas);
72 74
         }
73
-        return row;
74 75
     }
75 76
 
76 77
     /**
77 78
      * 新增保存字典数据信息
78 79
      * 
79
-     * @param dictData 字典数据信息
80
+     * @param data 字典数据信息
80 81
      * @return 结果
81 82
      */
82 83
     @Override
83
-    public int insertDictData(SysDictData dictData)
84
+    public int insertDictData(SysDictData data)
84 85
     {
85
-        int row = dictDataMapper.insertDictData(dictData);
86
+        int row = dictDataMapper.insertDictData(data);
86 87
         if (row > 0)
87 88
         {
88
-            DictUtils.clearDictCache();
89
+            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
90
+            DictUtils.setDictCache(data.getDictType(), dictDatas);
89 91
         }
90 92
         return row;
91 93
     }
@@ -93,16 +95,17 @@ public class SysDictDataServiceImpl implements ISysDictDataService
93 95
     /**
94 96
      * 修改保存字典数据信息
95 97
      * 
96
-     * @param dictData 字典数据信息
98
+     * @param data 字典数据信息
97 99
      * @return 结果
98 100
      */
99 101
     @Override
100
-    public int updateDictData(SysDictData dictData)
102
+    public int updateDictData(SysDictData data)
101 103
     {
102
-        int row = dictDataMapper.updateDictData(dictData);
104
+        int row = dictDataMapper.updateDictData(data);
103 105
         if (row > 0)
104 106
         {
105
-            DictUtils.clearDictCache();
107
+            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
108
+            DictUtils.setDictCache(data.getDictType(), dictDatas);
106 109
         }
107 110
         return row;
108 111
     }

+ 37 - 24
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictTypeServiceImpl.java

@@ -35,12 +35,7 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
35 35
     @PostConstruct
36 36
     public void init()
37 37
     {
38
-        List<SysDictType> dictTypeList = dictTypeMapper.selectDictTypeAll();
39
-        for (SysDictType dictType : dictTypeList)
40
-        {
41
-            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(dictType.getDictType());
42
-            DictUtils.setDictCache(dictType.getDictType(), dictDatas);
43
-        }
38
+        loadingDictCache();
44 39
     }
45 40
 
46 41
     /**
@@ -120,7 +115,7 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
120 115
      * @return 结果
121 116
      */
122 117
     @Override
123
-    public int deleteDictTypeByIds(Long[] dictIds)
118
+    public void deleteDictTypeByIds(Long[] dictIds)
124 119
     {
125 120
         for (Long dictId : dictIds)
126 121
         {
@@ -129,37 +124,54 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
129 124
             {
130 125
                 throw new CustomException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
131 126
             }
127
+            dictTypeMapper.deleteDictTypeById(dictId);
128
+            DictUtils.removeDictCache(dictType.getDictType());
132 129
         }
133
-        int count = dictTypeMapper.deleteDictTypeByIds(dictIds);
134
-        if (count > 0)
130
+    }
131
+
132
+    /**
133
+     * 加载字典缓存数据
134
+     */
135
+    public void loadingDictCache()
136
+    {
137
+        List<SysDictType> dictTypeList = dictTypeMapper.selectDictTypeAll();
138
+        for (SysDictType dictType : dictTypeList)
135 139
         {
136
-            DictUtils.clearDictCache();
140
+            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(dictType.getDictType());
141
+            DictUtils.setDictCache(dictType.getDictType(), dictDatas);
137 142
         }
138
-        return count;
139 143
     }
140 144
 
141 145
     /**
142
-     * 清空缓存数据
146
+     * 清空字典缓存数据
143 147
      */
144
-    @Override
145
-    public void clearCache()
148
+    public void clearDictCache()
146 149
     {
147 150
         DictUtils.clearDictCache();
148 151
     }
149 152
 
150 153
     /**
154
+     * 重置字典缓存数据
155
+     */
156
+    public void resetDictCache()
157
+    {
158
+        clearDictCache();
159
+        loadingDictCache();
160
+    }
161
+
162
+    /**
151 163
      * 新增保存字典类型信息
152 164
      * 
153
-     * @param dictType 字典类型信息
165
+     * @param dict 字典类型信息
154 166
      * @return 结果
155 167
      */
156 168
     @Override
157
-    public int insertDictType(SysDictType dictType)
169
+    public int insertDictType(SysDictType dict)
158 170
     {
159
-        int row = dictTypeMapper.insertDictType(dictType);
171
+        int row = dictTypeMapper.insertDictType(dict);
160 172
         if (row > 0)
161 173
         {
162
-            DictUtils.clearDictCache();
174
+            DictUtils.setDictCache(dict.getDictType(), null);
163 175
         }
164 176
         return row;
165 177
     }
@@ -167,19 +179,20 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
167 179
     /**
168 180
      * 修改保存字典类型信息
169 181
      * 
170
-     * @param dictType 字典类型信息
182
+     * @param dict 字典类型信息
171 183
      * @return 结果
172 184
      */
173 185
     @Override
174 186
     @Transactional
175
-    public int updateDictType(SysDictType dictType)
187
+    public int updateDictType(SysDictType dict)
176 188
     {
177
-        SysDictType oldDict = dictTypeMapper.selectDictTypeById(dictType.getDictId());
178
-        dictDataMapper.updateDictDataType(oldDict.getDictType(), dictType.getDictType());
179
-        int row = dictTypeMapper.updateDictType(dictType);
189
+        SysDictType oldDict = dictTypeMapper.selectDictTypeById(dict.getDictId());
190
+        dictDataMapper.updateDictDataType(oldDict.getDictType(), dict.getDictType());
191
+        int row = dictTypeMapper.updateDictType(dict);
180 192
         if (row > 0)
181 193
         {
182
-            DictUtils.clearDictCache();
194
+            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(dict.getDictType());
195
+            DictUtils.setDictCache(dict.getDictType(), dictDatas);
183 196
         }
184 197
         return row;
185 198
     }

+ 10 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/utils/DictUtils.java

@@ -44,6 +44,16 @@ public class DictUtils
44 44
     }
45 45
 
46 46
     /**
47
+     * 删除指定字典缓存
48
+     * 
49
+     * @param key 字典键
50
+     */
51
+    public static void removeDictCache(String key)
52
+    {
53
+        SpringUtils.getBean(RedisService.class).deleteObject(getCacheKey(key));
54
+    }
55
+
56
+    /**
47 57
      * 清空字典缓存
48 58
      */
49 59
     public static void clearDictCache()

+ 3 - 3
ruoyi-ui/src/api/system/config.js

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

+ 3 - 3
ruoyi-ui/src/api/system/dict/type.js

@@ -43,10 +43,10 @@ export function delType(dictId) {
43 43
   })
44 44
 }
45 45
 
46
-// 清理参数缓存
47
-export function clearCache() {
46
+// 刷新字典缓存
47
+export function refreshCache() {
48 48
   return request({
49
-    url: '/system/dict/type/clearCache',
49
+    url: '/system/dict/type/refreshCache',
50 50
     method: 'delete'
51 51
   })
52 52
 }

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

@@ -98,9 +98,9 @@
98 98
           plain
99 99
           icon="el-icon-refresh"
100 100
           size="mini"
101
-          @click="handleClearCache"
101
+          @click="handleRefreshCache"
102 102
           v-hasPermi="['system:config:remove']"
103
-        >清理缓存</el-button>
103
+        >刷新缓存</el-button>
104 104
       </el-col>
105 105
       <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
106 106
     </el-row>
@@ -180,7 +180,7 @@
180 180
 </template>
181 181
 
182 182
 <script>
183
-import { listConfig, getConfig, delConfig, addConfig, updateConfig, clearCache } from "@/api/system/config";
183
+import { listConfig, getConfig, delConfig, addConfig, updateConfig, refreshCache } from "@/api/system/config";
184 184
 
185 185
 export default {
186 186
   name: "Config",
@@ -343,10 +343,10 @@ export default {
343 343
         ...this.queryParams
344 344
       }, `config_${new Date().getTime()}.xlsx`)
345 345
     },
346
-    /** 清理缓存按钮操作 */
347
-    handleClearCache() {
348
-      clearCache().then(response => {
349
-        this.msgSuccess("清理成功");
346
+    /** 刷新缓存按钮操作 */
347
+    handleRefreshCache() {
348
+      refreshCache().then(() => {
349
+        this.msgSuccess("刷新成功");
350 350
       });
351 351
     }
352 352
   }

+ 7 - 7
ruoyi-ui/src/views/system/dict/index.vue

@@ -104,9 +104,9 @@
104 104
           plain
105 105
           icon="el-icon-refresh"
106 106
           size="mini"
107
-          @click="handleClearCache"
107
+          @click="handleRefreshCache"
108 108
           v-hasPermi="['system:dict:remove']"
109
-        >清理缓存</el-button>
109
+        >刷新缓存</el-button>
110 110
       </el-col>
111 111
       <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
112 112
     </el-row>
@@ -188,7 +188,7 @@
188 188
 </template>
189 189
 
190 190
 <script>
191
-import { listType, getType, delType, addType, updateType, clearCache } from "@/api/system/dict/type";
191
+import { listType, getType, delType, addType, updateType, refreshCache } from "@/api/system/dict/type";
192 192
 
193 193
 export default {
194 194
   name: "Dict",
@@ -347,10 +347,10 @@ export default {
347 347
         ...this.queryParams
348 348
       }, `type_${new Date().getTime()}.xlsx`)
349 349
     },
350
-    /** 清理缓存按钮操作 */
351
-    handleClearCache() {
352
-      clearCache().then(response => {
353
-        this.msgSuccess("清理成功");
350
+    /** 刷新缓存按钮操作 */
351
+    handleRefreshCache() {
352
+      refreshCache().then(() => {
353
+        this.msgSuccess("刷新成功");
354 354
       });
355 355
     }
356 356
   }