Browse Source

优化参数&字典缓存操作

RuoYi 3 years ago
parent
commit
387a3f838d

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

@@ -129,19 +129,20 @@ public class SysConfigController extends BaseController
     @ResponseBody
     public AjaxResult remove(String ids)
     {
-        return toAjax(configService.deleteConfigByIds(ids));
+        configService.deleteConfigByIds(ids);
+        return success();
     }
 
     /**
-     * 清空缓存
+     * 刷新参数缓存
      */
     @RequiresPermissions("system:config:remove")
     @Log(title = "参数管理", businessType = BusinessType.CLEAN)
-    @GetMapping("/clearCache")
+    @GetMapping("/refreshCache")
     @ResponseBody
-    public AjaxResult clearCache()
+    public AjaxResult refreshCache()
     {
-        configService.clearCache();
+        configService.resetConfigCache();
         return success();
     }
 

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

@@ -115,6 +115,7 @@ public class SysDictDataController extends BaseController
     @ResponseBody
     public AjaxResult remove(String ids)
     {
-        return toAjax(dictDataService.deleteDictDataByIds(ids));
+        dictDataService.deleteDictDataByIds(ids);
+        return success();
     }
 }

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

@@ -125,19 +125,20 @@ public class SysDictTypeController extends BaseController
     @ResponseBody
     public AjaxResult remove(String ids)
     {
-        return toAjax(dictTypeService.deleteDictTypeByIds(ids));
+        dictTypeService.deleteDictTypeByIds(ids);
+        return success();
     }
 
     /**
-     * 清空缓存
+     * 刷新字典缓存
      */
     @RequiresPermissions("system:dict:remove")
     @Log(title = "字典类型", businessType = BusinessType.CLEAN)
-    @GetMapping("/clearCache")
+    @GetMapping("/refreshCache")
     @ResponseBody
-    public AjaxResult clearCache()
+    public AjaxResult refreshCache()
     {
-        dictTypeService.clearCache();
+        dictTypeService.resetDictCache();
         return success();
     }
 

+ 5 - 5
ruoyi-admin/src/main/resources/templates/system/config/config.html

@@ -50,8 +50,8 @@
 		        <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:config:export">
 		            <i class="fa fa-download"></i> 导出
 		        </a>
-		        <a class="btn btn-danger" onclick="clearCache()" shiro:hasPermission="system:config:remove">
-		            <i class="fa fa-refresh"></i> 清理缓存
+		        <a class="btn btn-danger" onclick="refreshCache()" shiro:hasPermission="system:config:remove">
+		            <i class="fa fa-refresh"></i> 刷新缓存
 		        </a>
 	        </div>
 	        <div class="col-sm-12 select-table table-striped">
@@ -135,9 +135,9 @@
             $.table.init(options);
         });
         
-        /** 清理参数缓存 */
-        function clearCache() {
-            $.operate.get(prefix + "/clearCache");
+        /** 刷新参数缓存 */
+        function refreshCache() {
+            $.operate.get(prefix + "/refreshCache");
         }
     </script>
 </body>

+ 5 - 5
ruoyi-admin/src/main/resources/templates/system/dict/type/type.html

@@ -50,8 +50,8 @@
 		        <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:dict:export">
 		            <i class="fa fa-download"></i> 导出
 		        </a>
-		        <a class="btn btn-danger" onclick="clearCache()" shiro:hasPermission="system:dict:remove">
-		            <i class="fa fa-refresh"></i> 清理缓存
+		        <a class="btn btn-danger" onclick="refreshCache()" shiro:hasPermission="system:dict:remove">
+		            <i class="fa fa-refresh"></i> 刷新缓存
 		        </a>
 	        </div>
 	        
@@ -139,9 +139,9 @@
 		    $.modal.openTab("字典数据", url);
 		}
 		
-		/** 清理字典缓存 */
-        function clearCache() {
-            $.operate.get(prefix + "/clearCache");
+		/** 刷新字典缓存 */
+        function refreshCache() {
+            $.operate.get(prefix + "/refreshCache");
         }
 	</script>
 </body>

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

@@ -150,6 +150,16 @@ public class DictUtils
         return StringUtils.stripEnd(propertyString.toString(), separator);
     }
 
+    /**
+     * 删除指定字典缓存
+     * 
+     * @param key 字典键
+     */
+    public static void removeDictCache(String key)
+    {
+        CacheUtils.remove(getCacheName(), getCacheKey(key));
+    }
+
     /**
      * 清空字典缓存
      */

+ 8 - 0
ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysConfigMapper.java

@@ -50,6 +50,14 @@ public interface SysConfigMapper
      */
     public int updateConfig(SysConfig config);
 
+    /**
+     * 删除参数配置
+     * 
+     * @param configId 参数主键
+     * @return 结果
+     */
+    public int deleteConfigById(Long configId);
+
     /**
      * 批量删除参数配置
      * 

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

@@ -56,12 +56,22 @@ public interface ISysConfigService
      * @param ids 需要删除的数据ID
      * @return 结果
      */
-    public int deleteConfigByIds(String ids);
+    public void deleteConfigByIds(String ids);
 
     /**
-     * 清空缓存数据
+     * 加载参数缓存数据
      */
-    public void clearCache();
+    public void loadingConfigCache();
+
+    /**
+     * 清空参数缓存数据
+     */
+    public void clearConfigCache();
+
+    /**
+     * 重置参数缓存数据
+     */
+    public void resetConfigCache();
 
     /**
      * 校验参数键名是否唯一

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

@@ -41,7 +41,7 @@ public interface ISysDictDataService
      * @param ids 需要删除的数据
      * @return 结果
      */
-    public int deleteDictDataByIds(String ids);
+    public void deleteDictDataByIds(String ids);
 
     /**
      * 新增保存字典数据信息

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

@@ -58,12 +58,22 @@ public interface ISysDictTypeService
      * @return 结果
      * @throws Exception 异常
      */
-    public int deleteDictTypeByIds(String ids);
+    public void deleteDictTypeByIds(String ids);
 
     /**
-     * 清空缓存数据
+     * 加载字典缓存数据
      */
-    public void clearCache();
+    public void loadingDictCache();
+
+    /**
+     * 清空字典缓存数据
+     */
+    public void clearDictCache();
+
+    /**
+     * 重置字典缓存数据
+     */
+    public void resetDictCache();
 
     /**
      * 新增保存字典类型信息

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

@@ -31,11 +31,7 @@ public class SysConfigServiceImpl implements ISysConfigService
     @PostConstruct
     public void init()
     {
-        List<SysConfig> configsList = configMapper.selectConfigList(new SysConfig());
-        for (SysConfig config : configsList)
-        {
-            CacheUtils.put(getCacheName(), getCacheKey(config.getConfigKey()), config.getConfigValue());
-        }
+        loadingConfigCache();
     }
 
     /**
@@ -130,7 +126,7 @@ public class SysConfigServiceImpl implements ISysConfigService
      * @return 结果
      */
     @Override
-    public int deleteConfigByIds(String ids)
+    public void deleteConfigByIds(String ids)
     {
         Long[] configIds = Convert.toLongArray(ids);
         for (Long configId : configIds)
@@ -140,25 +136,43 @@ public class SysConfigServiceImpl implements ISysConfigService
             {
                 throw new BusinessException(String.format("内置参数【%1$s】不能删除 ", config.getConfigKey()));
             }
+            configMapper.deleteConfigById(configId);
+            CacheUtils.remove(getCacheName(), getCacheKey(config.getConfigKey()));
         }
-        int count = configMapper.deleteConfigByIds(Convert.toStrArray(ids));
-        if (count > 0)
-        {
+    }
 
-            CacheUtils.removeAll(getCacheName());
+    /**
+     * 加载参数缓存数据
+     */
+    @Override
+    public void loadingConfigCache()
+    {
+        List<SysConfig> configsList = configMapper.selectConfigList(new SysConfig());
+        for (SysConfig config : configsList)
+        {
+            CacheUtils.put(getCacheName(), getCacheKey(config.getConfigKey()), config.getConfigValue());
         }
-        return count;
     }
 
     /**
-     * 清空缓存数据
+     * 清空参数缓存数据
      */
     @Override
-    public void clearCache()
+    public void clearConfigCache()
     {
         CacheUtils.removeAll(getCacheName());
     }
 
+    /**
+     * 重置参数缓存数据
+     */
+    @Override
+    public void resetConfigCache()
+    {
+        clearConfigCache();
+        loadingConfigCache();
+    }
+
     /**
      * 校验参数键名是否唯一
      * 

+ 15 - 11
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictDataServiceImpl.java

@@ -64,14 +64,16 @@ public class SysDictDataServiceImpl implements ISysDictDataService
      * @return 结果
      */
     @Override
-    public int deleteDictDataByIds(String ids)
+    public void deleteDictDataByIds(String ids)
     {
-        int row = dictDataMapper.deleteDictDataByIds(Convert.toStrArray(ids));
-        if (row > 0)
+        Long[] dictCodes = Convert.toLongArray(ids);
+        for (Long dictCode : dictCodes)
         {
-            DictUtils.clearDictCache();
+            SysDictData data = selectDictDataById(dictCode);
+            dictDataMapper.deleteDictDataById(dictCode);
+            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
+            DictUtils.setDictCache(data.getDictType(), dictDatas);
         }
-        return row;
     }
 
     /**
@@ -81,12 +83,13 @@ public class SysDictDataServiceImpl implements ISysDictDataService
      * @return 结果
      */
     @Override
-    public int insertDictData(SysDictData dictData)
+    public int insertDictData(SysDictData data)
     {
-        int row = dictDataMapper.insertDictData(dictData);
+        int row = dictDataMapper.insertDictData(data);
         if (row > 0)
         {
-            DictUtils.clearDictCache();
+            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
+            DictUtils.setDictCache(data.getDictType(), dictDatas);
         }
         return row;
     }
@@ -98,12 +101,13 @@ public class SysDictDataServiceImpl implements ISysDictDataService
      * @return 结果
      */
     @Override
-    public int updateDictData(SysDictData dictData)
+    public int updateDictData(SysDictData data)
     {
-        int row = dictDataMapper.updateDictData(dictData);
+        int row = dictDataMapper.updateDictData(data);
         if (row > 0)
         {
-            DictUtils.clearDictCache();
+            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
+            DictUtils.setDictCache(data.getDictType(), dictDatas);
         }
         return row;
     }

+ 35 - 22
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictTypeServiceImpl.java

@@ -38,12 +38,7 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
     @PostConstruct
     public void init()
     {
-        List<SysDictType> dictTypeList = dictTypeMapper.selectDictTypeAll();
-        for (SysDictType dictType : dictTypeList)
-        {
-            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(dictType.getDictType());
-            DictUtils.setDictCache(dictType.getDictType(), dictDatas);
-        }
+        loadingDictCache();
     }
 
     /**
@@ -123,7 +118,7 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
      * @return 结果
      */
     @Override
-    public int deleteDictTypeByIds(String ids)
+    public void deleteDictTypeByIds(String ids)
     {
         Long[] dictIds = Convert.toLongArray(ids);
         for (Long dictId : dictIds)
@@ -133,24 +128,41 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
             {
                 throw new BusinessException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
             }
+            dictTypeMapper.deleteDictTypeById(dictId);
+            DictUtils.removeDictCache(dictType.getDictType());
         }
-        int count = dictTypeMapper.deleteDictTypeByIds(dictIds);
-        if (count > 0)
+    }
+
+    /**
+     * 加载字典缓存数据
+     */
+    public void loadingDictCache()
+    {
+        List<SysDictType> dictTypeList = dictTypeMapper.selectDictTypeAll();
+        for (SysDictType dict : dictTypeList)
         {
-            DictUtils.clearDictCache();
+            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(dict.getDictType());
+            DictUtils.setDictCache(dict.getDictType(), dictDatas);
         }
-        return count;
     }
 
     /**
-     * 清空缓存数据
+     * 清空字典缓存数据
      */
-    @Override
-    public void clearCache()
+    public void clearDictCache()
     {
         DictUtils.clearDictCache();
     }
 
+    /**
+     * 重置字典缓存数据
+     */
+    public void resetDictCache()
+    {
+        clearDictCache();
+        loadingDictCache();
+    }
+
     /**
      * 新增保存字典类型信息
      * 
@@ -158,12 +170,12 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
      * @return 结果
      */
     @Override
-    public int insertDictType(SysDictType dictType)
+    public int insertDictType(SysDictType dict)
     {
-        int row = dictTypeMapper.insertDictType(dictType);
+        int row = dictTypeMapper.insertDictType(dict);
         if (row > 0)
         {
-            DictUtils.clearDictCache();
+            DictUtils.setDictCache(dict.getDictType(), null);
         }
         return row;
     }
@@ -176,14 +188,15 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
      */
     @Override
     @Transactional
-    public int updateDictType(SysDictType dictType)
+    public int updateDictType(SysDictType dict)
     {
-        SysDictType oldDict = dictTypeMapper.selectDictTypeById(dictType.getDictId());
-        dictDataMapper.updateDictDataType(oldDict.getDictType(), dictType.getDictType());
-        int row = dictTypeMapper.updateDictType(dictType);
+        SysDictType oldDict = dictTypeMapper.selectDictTypeById(dict.getDictId());
+        dictDataMapper.updateDictDataType(oldDict.getDictType(), dict.getDictType());
+        int row = dictTypeMapper.updateDictType(dict);
         if (row > 0)
         {
-            DictUtils.clearDictCache();
+            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(dict.getDictType());
+            DictUtils.setDictCache(dict.getDictType(), dictDatas);
         }
         return row;
     }

+ 4 - 0
ruoyi-system/src/main/resources/mapper/system/SysConfigMapper.xml

@@ -98,6 +98,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         where config_id = #{configId}
     </update>
 	
+	<delete id="deleteConfigById" parameterType="Long">
+		delete from sys_config where config_id = #{configId}
+	</delete>
+	
     <delete id="deleteConfigByIds" parameterType="String">
         delete from sys_config where config_id in 
         <foreach item="configId" collection="array" open="(" separator="," close=")">