字典管理添加缓存读取

dep_wangsr
RuoYi 5 years ago committed by Limy
parent bfb435b728
commit 1ebb3075ba

@ -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();
}
/**

@ -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"

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

@ -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>

@ -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:";
/**
*
*/

@ -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,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);
}
/**

@ -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);
/**
*
*

@ -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();
/**
*

@ -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;
}
}

@ -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;
}
/**

@ -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;
}
}
Loading…
Cancel
Save