字典管理添加缓存读取

This commit is contained in:
RuoYi 2020-05-30 14:29:19 +08:00
parent 0d546e4ba4
commit 1e2058bdfb
13 changed files with 230 additions and 71 deletions

View File

@ -20,7 +20,7 @@ export function getData(dictCode) {
// 根据字典类型查询字典数据信息 // 根据字典类型查询字典数据信息
export function getDicts(dictType) { export function getDicts(dictType) {
return request({ return request({
url: '/system/dict/data/dictType/' + dictType, url: '/system/dict/data/type/' + dictType,
method: 'get' method: 'get'
}) })
} }

View File

@ -43,6 +43,14 @@ export function delType(dictId) {
}) })
} }
// 清理参数缓存
export function clearCache() {
return request({
url: '/system/dict/type/clearCache',
method: 'delete'
})
}
// 导出字典类型 // 导出字典类型
export function exportType(query) { export function exportType(query) {
return request({ return request({

View File

@ -352,7 +352,6 @@ export default {
}, },
/** 清理缓存按钮操作 */ /** 清理缓存按钮操作 */
handleClearCache() { handleClearCache() {
const queryParams = this.queryParams;
clearCache().then(response => { clearCache().then(response => {
if (response.code === 200) { if (response.code === 200) {
this.msgSuccess("清理成功"); this.msgSuccess("清理成功");

View File

@ -94,6 +94,15 @@
v-hasPermi="['system:dict:export']" v-hasPermi="['system:dict:export']"
>导出</el-button> >导出</el-button>
</el-col> </el-col>
<el-col :span="1.5">
<el-button
type="danger"
icon="el-icon-refresh"
size="mini"
@click="handleClearCache"
v-hasPermi="['system:dict:remove']"
>清理缓存</el-button>
</el-col>
</el-row> </el-row>
<el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange"> <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
@ -173,7 +182,7 @@
</template> </template>
<script> <script>
import { listType, getType, delType, addType, updateType, exportType } from "@/api/system/dict/type"; import { listType, getType, delType, addType, updateType, exportType, clearCache } from "@/api/system/dict/type";
export default { export default {
name: "Dict", name: "Dict",
@ -344,6 +353,16 @@ export default {
}).then(response => { }).then(response => {
this.download(response.msg); this.download(response.msg);
}).catch(function() {}); }).catch(function() {});
},
/** 清理缓存按钮操作 */
handleClearCache() {
clearCache().then(response => {
if (response.code === 200) {
this.msgSuccess("清理成功");
} else {
this.msgError(response.msg);
}
});
} }
} }
}; };

View File

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

View File

@ -0,0 +1,64 @@
package com.ruoyi.common.utils;
import java.util.Collection;
import java.util.List;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.project.system.domain.SysDictData;
/**
* 字典工具类
*
* @author ruoyi
*/
public class DictUtils
{
/**
* 设置字典缓存
*
* @param key 参数键
* @param dictDatas 字典数据列表
*/
public static void setDictCache(String key, List<SysDictData> dictDatas)
{
SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(key), dictDatas);
}
/**
* 获取字典缓存
*
* @param key 参数键
* @return dictDatas 字典数据列表
*/
public static List<SysDictData> getDictCache(String key)
{
Object cacheObj = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
if (StringUtils.isNotNull(cacheObj))
{
List<SysDictData> DictDatas = StringUtils.cast(cacheObj);
return DictDatas;
}
return null;
}
/**
* 清空字典缓存
*/
public static void clearDictCache()
{
Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(Constants.SYS_DICT_KEY + "*");
SpringUtils.getBean(RedisCache.class).deleteObject(keys);
}
/**
* 设置cache key
*
* @param configKey 参数键
* @return 缓存键key
*/
public static String getCacheKey(String configKey)
{
return Constants.SYS_DICT_KEY + configKey;
}
}

View File

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

View File

@ -21,6 +21,7 @@ import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo; import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysDictData; import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.service.ISysDictDataService; import com.ruoyi.project.system.service.ISysDictDataService;
import com.ruoyi.project.system.service.ISysDictTypeService;
/** /**
* 数据字典信息 * 数据字典信息
@ -34,6 +35,9 @@ public class SysDictDataController extends BaseController
@Autowired @Autowired
private ISysDictDataService dictDataService; private ISysDictDataService dictDataService;
@Autowired
private ISysDictTypeService dictTypeService;
@PreAuthorize("@ss.hasPermi('system:dict:list')") @PreAuthorize("@ss.hasPermi('system:dict:list')")
@GetMapping("/list") @GetMapping("/list")
public TableDataInfo list(SysDictData dictData) public TableDataInfo list(SysDictData dictData)
@ -66,10 +70,10 @@ public class SysDictDataController extends BaseController
/** /**
* 根据字典类型查询字典数据信息 * 根据字典类型查询字典数据信息
*/ */
@GetMapping(value = "/dictType/{dictType}") @GetMapping(value = "/type/{dictType}")
public AjaxResult dictType(@PathVariable String dictType) public AjaxResult dictType(@PathVariable String dictType)
{ {
return AjaxResult.success(dictDataService.selectDictDataByType(dictType)); return AjaxResult.success(dictTypeService.selectDictDataByType(dictType));
} }
/** /**

View File

@ -107,6 +107,18 @@ public class SysDictTypeController extends BaseController
return toAjax(dictTypeService.deleteDictTypeByIds(dictIds)); return toAjax(dictTypeService.deleteDictTypeByIds(dictIds));
} }
/**
* 清空缓存
*/
@PreAuthorize("@ss.hasPermi('system:dict:remove')")
@Log(title = "字典类型", businessType = BusinessType.CLEAN)
@DeleteMapping("/clearCache")
public AjaxResult clearCache()
{
dictTypeService.clearCache();
return AjaxResult.success();
}
/** /**
* 获取字典选择框列表 * 获取字典选择框列表
*/ */

View File

@ -18,14 +18,6 @@ public interface ISysDictDataService
*/ */
public List<SysDictData> selectDictDataList(SysDictData dictData); 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); public SysDictData selectDictDataById(Long dictCode);
/**
* 通过字典ID删除字典数据信息
*
* @param dictCode 字典数据ID
* @return 结果
*/
public int deleteDictDataById(Long dictCode);
/** /**
* 批量删除字典数据信息 * 批量删除字典数据信息
* *

View File

@ -1,6 +1,7 @@
package com.ruoyi.project.system.service; package com.ruoyi.project.system.service;
import java.util.List; import java.util.List;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.domain.SysDictType; import com.ruoyi.project.system.domain.SysDictType;
/** /**
@ -25,6 +26,14 @@ public interface ISysDictTypeService
*/ */
public List<SysDictType> selectDictTypeAll(); public List<SysDictType> selectDictTypeAll();
/**
* 根据字典类型查询字典数据
*
* @param dictType 字典类型
* @return 字典数据集合信息
*/
public List<SysDictData> selectDictDataByType(String dictType);
/** /**
* 根据字典类型ID查询信息 * 根据字典类型ID查询信息
* *
@ -41,14 +50,6 @@ public interface ISysDictTypeService
*/ */
public SysDictType selectDictTypeByType(String dictType); public SysDictType selectDictTypeByType(String dictType);
/**
* 通过字典ID删除字典信息
*
* @param dictId 字典ID
* @return 结果
*/
public int deleteDictTypeById(Long dictId);
/** /**
* 批量删除字典信息 * 批量删除字典信息
* *
@ -57,6 +58,11 @@ public interface ISysDictTypeService
*/ */
public int deleteDictTypeByIds(Long[] dictIds); public int deleteDictTypeByIds(Long[] dictIds);
/**
* 清空缓存数据
*/
public void clearCache();
/** /**
* 新增保存字典类型信息 * 新增保存字典类型信息
* *

View File

@ -3,6 +3,7 @@ package com.ruoyi.project.system.service.impl;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.project.system.domain.SysDictData; import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.mapper.SysDictDataMapper; import com.ruoyi.project.system.mapper.SysDictDataMapper;
import com.ruoyi.project.system.service.ISysDictDataService; import com.ruoyi.project.system.service.ISysDictDataService;
@ -30,18 +31,6 @@ public class SysDictDataServiceImpl implements ISysDictDataService
return dictDataMapper.selectDictDataList(dictData); return dictDataMapper.selectDictDataList(dictData);
} }
/**
* 根据字典类型查询字典数据
*
* @param dictType 字典类型
* @return 字典数据集合信息
*/
@Override
public List<SysDictData> selectDictDataByType(String dictType)
{
return dictDataMapper.selectDictDataByType(dictType);
}
/** /**
* 根据字典类型和字典键值查询字典数据信息 * 根据字典类型和字典键值查询字典数据信息
* *
@ -67,18 +56,6 @@ public class SysDictDataServiceImpl implements ISysDictDataService
return dictDataMapper.selectDictDataById(dictCode); return dictDataMapper.selectDictDataById(dictCode);
} }
/**
* 通过字典ID删除字典数据信息
*
* @param dictCode 字典数据ID
* @return 结果
*/
@Override
public int deleteDictDataById(Long dictCode)
{
return dictDataMapper.deleteDictDataById(dictCode);
}
/** /**
* 批量删除字典数据信息 * 批量删除字典数据信息
* *
@ -87,7 +64,12 @@ public class SysDictDataServiceImpl implements ISysDictDataService
*/ */
public int deleteDictDataByIds(Long[] dictCodes) public int deleteDictDataByIds(Long[] dictCodes)
{ {
return dictDataMapper.deleteDictDataByIds(dictCodes); int row = dictDataMapper.deleteDictDataByIds(dictCodes);
if (row > 0)
{
DictUtils.clearDictCache();
}
return row;
} }
/** /**
@ -99,7 +81,12 @@ public class SysDictDataServiceImpl implements ISysDictDataService
@Override @Override
public int insertDictData(SysDictData dictData) public int insertDictData(SysDictData dictData)
{ {
return dictDataMapper.insertDictData(dictData); int row = dictDataMapper.insertDictData(dictData);
if (row > 0)
{
DictUtils.clearDictCache();
}
return row;
} }
/** /**
@ -111,6 +98,11 @@ public class SysDictDataServiceImpl implements ISysDictDataService
@Override @Override
public int updateDictData(SysDictData dictData) public int updateDictData(SysDictData dictData)
{ {
return dictDataMapper.updateDictData(dictData); int row = dictDataMapper.updateDictData(dictData);
if (row > 0)
{
DictUtils.clearDictCache();
}
return row;
} }
} }

View File

@ -1,11 +1,15 @@
package com.ruoyi.project.system.service.impl; package com.ruoyi.project.system.service.impl;
import java.util.List; import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.domain.SysDictType; import com.ruoyi.project.system.domain.SysDictType;
import com.ruoyi.project.system.mapper.SysDictDataMapper; import com.ruoyi.project.system.mapper.SysDictDataMapper;
import com.ruoyi.project.system.mapper.SysDictTypeMapper; import com.ruoyi.project.system.mapper.SysDictTypeMapper;
@ -25,6 +29,20 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
@Autowired @Autowired
private SysDictDataMapper dictDataMapper; 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);
}
}
/** /**
* 根据条件分页查询字典类型 * 根据条件分页查询字典类型
* *
@ -48,6 +66,29 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
return dictTypeMapper.selectDictTypeAll(); 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查询信息 * 根据字典类型ID查询信息
* *
@ -71,18 +112,6 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
return dictTypeMapper.selectDictTypeByType(dictType); return dictTypeMapper.selectDictTypeByType(dictType);
} }
/**
* 通过字典ID删除字典信息
*
* @param dictId 字典ID
* @return 结果
*/
@Override
public int deleteDictTypeById(Long dictId)
{
return dictTypeMapper.deleteDictTypeById(dictId);
}
/** /**
* 批量删除字典类型信息 * 批量删除字典类型信息
* *
@ -91,7 +120,28 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
*/ */
public int deleteDictTypeByIds(Long[] dictIds) public int deleteDictTypeByIds(Long[] dictIds)
{ {
return dictTypeMapper.deleteDictTypeByIds(dictIds); for (Long dictId : dictIds)
{
SysDictType dictType = selectDictTypeById(dictId);
if (dictDataMapper.countDictDataByType(dictType.getDictType()) > 0)
{
throw new CustomException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
}
}
int count = dictTypeMapper.deleteDictTypeByIds(dictIds);
if (count > 0)
{
DictUtils.clearDictCache();
}
return count;
}
/**
* 清空缓存数据
*/
public void clearCache()
{
DictUtils.clearDictCache();
} }
/** /**
@ -103,7 +153,12 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
@Override @Override
public int insertDictType(SysDictType dictType) public int insertDictType(SysDictType dictType)
{ {
return dictTypeMapper.insertDictType(dictType); int row = dictTypeMapper.insertDictType(dictType);
if (row > 0)
{
DictUtils.clearDictCache();
}
return row;
} }
/** /**
@ -118,7 +173,12 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
{ {
SysDictType oldDict = dictTypeMapper.selectDictTypeById(dictType.getDictId()); SysDictType oldDict = dictTypeMapper.selectDictTypeById(dictType.getDictId());
dictDataMapper.updateDictDataType(oldDict.getDictType(), dictType.getDictType()); dictDataMapper.updateDictDataType(oldDict.getDictType(), dictType.getDictType());
return dictTypeMapper.updateDictType(dictType); int row = dictTypeMapper.updateDictType(dictType);
if (row > 0)
{
DictUtils.clearDictCache();
}
return row;
} }
/** /**