字典管理添加缓存读取

pull/6/head
RuoYi 5 years ago
parent d5dd5b6b7a
commit b0af4a1c04

@ -86,6 +86,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:";
/** /**
* *
*/ */

@ -23,6 +23,7 @@ import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils; import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.SysDictData; import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.service.ISysDictDataService; import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
/** /**
* *
@ -35,6 +36,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")
@ -68,10 +72,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));
} }
/** /**

@ -109,6 +109,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();
}
/** /**
* *
*/ */

@ -1,7 +1,6 @@
package com.ruoyi.system.service; package com.ruoyi.system.service;
import java.util.List; import java.util.List;
import com.ruoyi.system.domain.SysDictData; import com.ruoyi.system.domain.SysDictData;
/** /**
@ -19,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);
/** /**
* *
* *
@ -44,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);
/** /**
* *
* *

@ -1,7 +1,7 @@
package com.ruoyi.system.service; package com.ruoyi.system.service;
import java.util.List; import java.util.List;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.domain.SysDictType; import com.ruoyi.system.domain.SysDictType;
/** /**
@ -26,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
* *
@ -42,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);
/** /**
* *
* *
@ -58,6 +58,11 @@ public interface ISysDictTypeService
*/ */
public int deleteDictTypeByIds(Long[] dictIds); public int deleteDictTypeByIds(Long[] dictIds);
/**
*
*/
public void clearCache();
/** /**
* *
* *

@ -1,13 +1,12 @@
package com.ruoyi.system.service.impl; package com.ruoyi.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.system.domain.SysDictData; import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.mapper.SysDictDataMapper; import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.ISysDictDataService; import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.utils.DictUtils;
/** /**
* *
@ -32,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);
}
/** /**
* *
* *
@ -69,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);
}
/** /**
* *
* *
@ -89,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;
} }
/** /**
@ -101,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;
} }
/** /**
@ -113,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;
} }
} }

@ -1,17 +1,19 @@
package com.ruoyi.system.service.impl; package com.ruoyi.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.core.constant.UserConstants; import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.domain.SysDictType; import com.ruoyi.system.domain.SysDictType;
import com.ruoyi.system.mapper.SysDictDataMapper; import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysDictTypeMapper; import com.ruoyi.system.mapper.SysDictTypeMapper;
import com.ruoyi.system.service.ISysDictTypeService; import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.system.utils.DictUtils;
/** /**
* *
@ -27,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);
}
}
/** /**
* *
* *
@ -50,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
* *
@ -74,26 +113,35 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
} }
/** /**
* ID *
* *
* @param dictId ID * @param dictIds ID
* @return * @return
*/ */
@Override public int deleteDictTypeByIds(Long[] dictIds)
public int deleteDictTypeById(Long dictId)
{ {
return dictTypeMapper.deleteDictTypeById(dictId); 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;
} }
/** /**
* *
*
* @param dictIds ID
* @return
*/ */
public int deleteDictTypeByIds(Long[] dictIds) public void clearCache()
{ {
return dictTypeMapper.deleteDictTypeByIds(dictIds); DictUtils.clearDictCache();
} }
/** /**
@ -105,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;
} }
/** /**
@ -120,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;
} }
/** /**

@ -0,0 +1,65 @@
package com.ruoyi.system.utils;
import java.util.Collection;
import java.util.List;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.domain.SysDictData;
/**
*
*
* @author ruoyi
*/
public class DictUtils
{
/**
*
*
* @param key
* @param dictDatas
*/
public static void setDictCache(String key, List<SysDictData> dictDatas)
{
SpringUtils.getBean(RedisService.class).setCacheObject(getCacheKey(key), dictDatas);
}
/**
*
*
* @param key
* @return dictDatas
*/
public static List<SysDictData> getDictCache(String key)
{
Object cacheObj = SpringUtils.getBean(RedisService.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(RedisService.class).keys(Constants.SYS_DICT_KEY + "*");
SpringUtils.getBean(RedisService.class).deleteObject(keys);
}
/**
* cache key
*
* @param configKey
* @return key
*/
public static String getCacheKey(String configKey)
{
return Constants.SYS_DICT_KEY + configKey;
}
}

@ -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'
}) })
} }

@ -43,6 +43,14 @@ export function delType(dictId) {
}) })
} }
// 清理参数缓存
export function clearCache() {
return request({
url: '/system/dict/type/clearCache',
method: 'delete'
})
}
// 获取字典选择框列表 // 获取字典选择框列表
export function optionselect() { export function optionselect() {
return request({ return request({

@ -341,7 +341,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("清理成功");

@ -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 } from "@/api/system/dict/type"; import { listType, getType, delType, addType, updateType, clearCache } from "@/api/system/dict/type";
export default { export default {
name: "Dict", name: "Dict",
@ -333,6 +342,16 @@ export default {
this.download('system/dict/type/export', { this.download('system/dict/type/export', {
...this.queryParams ...this.queryParams
}, `type_${new Date().getTime()}.xlsx`) }, `type_${new Date().getTime()}.xlsx`)
},
/** 清理缓存按钮操作 */
handleClearCache() {
clearCache().then(response => {
if (response.code === 200) {
this.msgSuccess("清理成功");
} else {
this.msgError(response.msg);
}
});
} }
} }
}; };

Loading…
Cancel
Save