Просмотр исходного кода

字典管理添加缓存读取

RuoYi 5 лет назад
Родитель
Сommit
05b5b314ee

+ 14 - 8
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysDictTypeController.java

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

+ 17 - 1
ruoyi-admin/src/main/resources/ehcache/ehcache-shiro.xml

@@ -41,7 +41,15 @@
            timeToIdleSeconds="0"
            statistics="true">
     </cache>
-
+    
+    <!-- 系统缓存 -->
+    <cache name="sys-cache"
+           maxEntriesLocalHeap="1000"
+           eternal="true"
+           overflowToDisk="true"
+           statistics="true">
+    </cache>
+    
     <!-- 系统参数缓存 -->
     <cache name="sys-config"
            maxEntriesLocalHeap="1000"
@@ -50,6 +58,14 @@
            statistics="true">
     </cache>
     
+    <!-- 系统字典缓存 -->
+    <cache name="sys-dict"
+           maxEntriesLocalHeap="1000"
+           eternal="true"
+           overflowToDisk="true"
+           statistics="true">
+    </cache>
+    
     <!-- 系统会话缓存 -->
     <cache name="shiro-activeSessionCache"
            maxElementsInMemory="10000"

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

@@ -135,6 +135,7 @@
             $.table.init(options);
         });
         
+        /** 清理参数缓存 */
         function clearCache() {
             $.operate.get(prefix + "/clearCache");
         }

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

@@ -50,6 +50,9 @@
 		        <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>
 	        </div>
 	        
 	        <div class="col-sm-12 select-table table-striped">
@@ -132,6 +135,11 @@
 		    var url = prefix + '/detail/' + dictId;
 		    $.modal.openTab("字典数据", url);
 		}
+		
+		/** 清理字典缓存 */
+        function clearCache() {
+            $.operate.get(prefix + "/clearCache");
+        }
 	</script>
 </body>
 </html>

+ 10 - 0
ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java

@@ -67,6 +67,16 @@ public class Constants
      */
     public static final String SYS_CONFIG_KEY = "sys_config:";
 
+    /**
+     * 字典管理 cache name
+     */
+    public static final String SYS_DICT_CACHE = "sys-dict";
+
+    /**
+     * 字典管理 cache key
+     */
+    public static final String SYS_DICT_KEY = "sys_dict:";
+
     /**
      * 资源映射路径 前缀
      */

+ 6 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/StringUtils.java

@@ -395,4 +395,10 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
         }
         return sb.toString();
     }
+
+    @SuppressWarnings("unchecked")
+    public static <T> T cast(Object obj)
+    {
+        return (T) obj;
+    }
 }

+ 5 - 1
ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/DictService.java

@@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import com.ruoyi.system.domain.SysDictData;
 import com.ruoyi.system.service.ISysDictDataService;
+import com.ruoyi.system.service.ISysDictTypeService;
 
 /**
  * RuoYi首创 html调用 thymeleaf 实现字典读取
@@ -14,6 +15,9 @@ import com.ruoyi.system.service.ISysDictDataService;
 @Service("dict")
 public class DictService
 {
+    @Autowired
+    private ISysDictTypeService dictTypeService;
+
     @Autowired
     private ISysDictDataService dictDataService;
 
@@ -25,7 +29,7 @@ public class DictService
      */
     public List<SysDictData> getType(String dictType)
     {
-        return dictDataService.selectDictDataByType(dictType);
+        return dictTypeService.selectDictDataByType(dictType);
     }
 
     /**

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

@@ -18,14 +18,6 @@ public interface ISysDictDataService
      */
     public List<SysDictData> selectDictDataList(SysDictData dictData);
 
-    /**
-     * 根据字典类型查询字典数据
-     * 
-     * @param dictType 字典类型
-     * @return 字典数据集合信息
-     */
-    public List<SysDictData> selectDictDataByType(String dictType);
-
     /**
      * 根据字典类型和字典键值查询字典数据信息
      * 
@@ -43,14 +35,6 @@ public interface ISysDictDataService
      */
     public SysDictData selectDictDataById(Long dictCode);
 
-    /**
-     * 通过字典ID删除字典数据信息
-     * 
-     * @param dictCode 字典数据ID
-     * @return 结果
-     */
-    public int deleteDictDataById(Long dictCode);
-
     /**
      * 批量删除字典数据
      * 

+ 15 - 9
ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDictTypeService.java

@@ -2,6 +2,7 @@ package com.ruoyi.system.service;
 
 import java.util.List;
 import com.ruoyi.common.core.domain.Ztree;
+import com.ruoyi.system.domain.SysDictData;
 import com.ruoyi.system.domain.SysDictType;
 
 /**
@@ -26,6 +27,14 @@ public interface ISysDictTypeService
      */
     public List<SysDictType> selectDictTypeAll();
 
+    /**
+     * 根据字典类型查询字典数据
+     * 
+     * @param dictType 字典类型
+     * @return 字典数据集合信息
+     */
+    public List<SysDictData> selectDictDataByType(String dictType);
+
     /**
      * 根据字典类型ID查询信息
      * 
@@ -42,14 +51,6 @@ public interface ISysDictTypeService
      */
     public SysDictType selectDictTypeByType(String dictType);
 
-    /**
-     * 通过字典ID删除字典信息
-     * 
-     * @param dictId 字典ID
-     * @return 结果
-     */
-    public int deleteDictTypeById(Long dictId);
-
     /**
      * 批量删除字典类型
      * 
@@ -57,7 +58,12 @@ public interface ISysDictTypeService
      * @return 结果
      * @throws Exception 异常
      */
-    public int deleteDictTypeByIds(String ids) throws Exception;
+    public int deleteDictTypeByIds(String ids);
+
+    /**
+     * 清空缓存数据
+     */
+    public void clearCache();
 
     /**
      * 新增保存字典类型信息

+ 19 - 27
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictDataServiceImpl.java

@@ -7,6 +7,7 @@ import com.ruoyi.common.core.text.Convert;
 import com.ruoyi.system.domain.SysDictData;
 import com.ruoyi.system.mapper.SysDictDataMapper;
 import com.ruoyi.system.service.ISysDictDataService;
+import com.ruoyi.system.utils.DictUtils;
 
 /**
  * 字典 业务层处理
@@ -31,18 +32,6 @@ public class SysDictDataServiceImpl implements ISysDictDataService
         return dictDataMapper.selectDictDataList(dictData);
     }
 
-    /**
-     * 根据字典类型查询字典数据
-     * 
-     * @param dictType 字典类型
-     * @return 字典数据集合信息
-     */
-    @Override
-    public List<SysDictData> selectDictDataByType(String dictType)
-    {
-        return dictDataMapper.selectDictDataByType(dictType);
-    }
-
     /**
      * 根据字典类型和字典键值查询字典数据信息
      * 
@@ -68,18 +57,6 @@ public class SysDictDataServiceImpl implements ISysDictDataService
         return dictDataMapper.selectDictDataById(dictCode);
     }
 
-    /**
-     * 通过字典ID删除字典数据信息
-     * 
-     * @param dictCode 字典数据ID
-     * @return 结果
-     */
-    @Override
-    public int deleteDictDataById(Long dictCode)
-    {
-        return dictDataMapper.deleteDictDataById(dictCode);
-    }
-
     /**
      * 批量删除字典数据
      * 
@@ -89,7 +66,12 @@ public class SysDictDataServiceImpl implements ISysDictDataService
     @Override
     public int deleteDictDataByIds(String ids)
     {
-        return dictDataMapper.deleteDictDataByIds(Convert.toStrArray(ids));
+        int row = dictDataMapper.deleteDictDataByIds(Convert.toStrArray(ids));
+        if (row > 0)
+        {
+            DictUtils.clearDictCache();
+        }
+        return row;
     }
 
     /**
@@ -101,7 +83,12 @@ public class SysDictDataServiceImpl implements ISysDictDataService
     @Override
     public int insertDictData(SysDictData dictData)
     {
-        return dictDataMapper.insertDictData(dictData);
+        int row = dictDataMapper.insertDictData(dictData);
+        if (row > 0)
+        {
+            DictUtils.clearDictCache();
+        }
+        return row;
     }
 
     /**
@@ -113,6 +100,11 @@ public class SysDictDataServiceImpl implements ISysDictDataService
     @Override
     public int updateDictData(SysDictData dictData)
     {
-        return dictDataMapper.updateDictData(dictData);
+        int row = dictDataMapper.updateDictData(dictData);
+        if (row > 0)
+        {
+            DictUtils.clearDictCache();
+        }
+        return row;
     }
 }

+ 66 - 16
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictTypeServiceImpl.java

@@ -2,6 +2,7 @@ package com.ruoyi.system.service.impl;
 
 import java.util.ArrayList;
 import java.util.List;
+import javax.annotation.PostConstruct;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -10,10 +11,12 @@ import com.ruoyi.common.core.domain.Ztree;
 import com.ruoyi.common.core.text.Convert;
 import com.ruoyi.common.exception.BusinessException;
 import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.system.domain.SysDictData;
 import com.ruoyi.system.domain.SysDictType;
 import com.ruoyi.system.mapper.SysDictDataMapper;
 import com.ruoyi.system.mapper.SysDictTypeMapper;
 import com.ruoyi.system.service.ISysDictTypeService;
+import com.ruoyi.system.utils.DictUtils;
 
 /**
  * 字典 业务层处理
@@ -29,6 +32,20 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
     @Autowired
     private SysDictDataMapper dictDataMapper;
 
+    /**
+     * 项目启动时,初始化字典到缓存
+     */
+    @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);
+        }
+    }
+
     /**
      * 根据条件分页查询字典类型
      * 
@@ -52,6 +69,29 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
         return dictTypeMapper.selectDictTypeAll();
     }
 
+    /**
+     * 根据字典类型查询字典数据
+     * 
+     * @param dictType 字典类型
+     * @return 字典数据集合信息
+     */
+    @Override
+    public List<SysDictData> selectDictDataByType(String dictType)
+    {
+        List<SysDictData> dictDatas = DictUtils.getDictCache(dictType);
+        if (StringUtils.isNotNull(dictDatas))
+        {
+            return dictDatas;
+        }
+        dictDatas = dictDataMapper.selectDictDataByType(dictType);
+        if (StringUtils.isNotNull(dictDatas))
+        {
+            DictUtils.setDictCache(dictType, dictDatas);
+            return dictDatas;
+        }
+        return null;
+    }
+
     /**
      * 根据字典类型ID查询信息
      * 
@@ -75,18 +115,6 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
         return dictTypeMapper.selectDictTypeByType(dictType);
     }
 
-    /**
-     * 通过字典ID删除字典信息
-     * 
-     * @param dictId 字典ID
-     * @return 结果
-     */
-    @Override
-    public int deleteDictTypeById(Long dictId)
-    {
-        return dictTypeMapper.deleteDictTypeById(dictId);
-    }
-
     /**
      * 批量删除字典类型
      * 
@@ -94,7 +122,7 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
      * @return 结果
      */
     @Override
-    public int deleteDictTypeByIds(String ids) throws BusinessException
+    public int deleteDictTypeByIds(String ids)
     {
         Long[] dictIds = Convert.toLongArray(ids);
         for (Long dictId : dictIds)
@@ -105,8 +133,20 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
                 throw new BusinessException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
             }
         }
+        int count = dictTypeMapper.deleteDictTypeByIds(dictIds);
+        if (count > 0)
+        {
+            DictUtils.clearDictCache();
+        }
+        return count;
+    }
 
-        return dictTypeMapper.deleteDictTypeByIds(dictIds);
+    /**
+     * 清空缓存数据
+     */
+    public void clearCache()
+    {
+        DictUtils.clearDictCache();
     }
 
     /**
@@ -118,7 +158,12 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
     @Override
     public int insertDictType(SysDictType dictType)
     {
-        return dictTypeMapper.insertDictType(dictType);
+        int row = dictTypeMapper.insertDictType(dictType);
+        if (row > 0)
+        {
+            DictUtils.clearDictCache();
+        }
+        return row;
     }
 
     /**
@@ -133,7 +178,12 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
     {
         SysDictType oldDict = dictTypeMapper.selectDictTypeById(dictType.getDictId());
         dictDataMapper.updateDictDataType(oldDict.getDictType(), dictType.getDictType());
-        return dictTypeMapper.updateDictType(dictType);
+        int row = dictTypeMapper.updateDictType(dictType);
+        if (row > 0)
+        {
+            DictUtils.clearDictCache();
+        }
+        return row;
     }
 
     /**

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

@@ -0,0 +1,72 @@
+package com.ruoyi.system.utils;
+
+import java.util.List;
+import com.ruoyi.common.constant.Constants;
+import com.ruoyi.common.utils.CacheUtils;
+import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.system.domain.SysDictData;
+
+/**
+ * 字典工具类
+ * 
+ * @author ruoyi
+ */
+public class DictUtils
+{
+    /**
+     * 设置字典缓存
+     * 
+     * @param key 参数键
+     * @param dictDatas 字典数据列表
+     */
+    public static void setDictCache(String key, List<SysDictData> dictDatas)
+    {
+        CacheUtils.put(getCacheName(), getCacheKey(key), dictDatas);
+    }
+
+    /**
+     * 获取字典缓存
+     * 
+     * @param key 参数键
+     * @return dictDatas 字典数据列表
+     */
+    public static List<SysDictData> getDictCache(String key)
+    {
+        Object cacheObj = CacheUtils.get(getCacheName(), getCacheKey(key));
+        if (StringUtils.isNotNull(cacheObj))
+        {
+            List<SysDictData> DictDatas = StringUtils.cast(cacheObj);
+            return DictDatas;
+        }
+        return null;
+    }
+
+    /**
+     * 清空字典缓存
+     */
+    public static void clearDictCache()
+    {
+        CacheUtils.removeAll(getCacheName());
+    }
+
+    /**
+     * 获取cache name
+     * 
+     * @return 缓存名
+     */
+    public static String getCacheName()
+    {
+        return Constants.SYS_DICT_CACHE;
+    }
+
+    /**
+     * 设置cache key
+     * 
+     * @param configKey 参数键
+     * @return 缓存键key
+     */
+    public static String getCacheKey(String configKey)
+    {
+        return Constants.SYS_DICT_KEY + configKey;
+    }
+}