Browse Source

字典管理添加缓存读取

tags/v2.0
RuoYi 5 years ago
parent
commit
b0af4a1c04
  1. 5
      ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/Constants.java
  2. 8
      ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysDictDataController.java
  3. 12
      ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysDictTypeController.java
  4. 17
      ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDictDataService.java
  5. 23
      ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDictTypeService.java
  6. 48
      ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictDataServiceImpl.java
  7. 88
      ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictTypeServiceImpl.java
  8. 65
      ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/utils/DictUtils.java
  9. 2
      ruoyi-ui/src/api/system/dict/data.js
  10. 8
      ruoyi-ui/src/api/system/dict/type.js
  11. 1
      ruoyi-ui/src/views/system/config/index.vue
  12. 21
      ruoyi-ui/src/views/system/dict/index.vue

5
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/Constants.java

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

8
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysDictDataController.java

@ -23,6 +23,7 @@ import com.ruoyi.common.log.enums.BusinessType; @@ -23,6 +23,7 @@ import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
/**
* 数据字典信息
@ -35,6 +36,9 @@ public class SysDictDataController extends BaseController @@ -35,6 +36,9 @@ public class SysDictDataController extends BaseController
{
@Autowired
private ISysDictDataService dictDataService;
@Autowired
private ISysDictTypeService dictTypeService;
@PreAuthorize("@ss.hasPermi('system:dict:list')")
@GetMapping("/list")
@ -68,10 +72,10 @@ public class SysDictDataController extends BaseController @@ -68,10 +72,10 @@ public class SysDictDataController extends BaseController
/**
* 根据字典类型查询字典数据信息
*/
@GetMapping(value = "/dictType/{dictType}")
@GetMapping(value = "/type/{dictType}")
public AjaxResult dictType(@PathVariable String dictType)
{
return AjaxResult.success(dictDataService.selectDictDataByType(dictType));
return AjaxResult.success(dictTypeService.selectDictDataByType(dictType));
}
/**

12
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysDictTypeController.java

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

17
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDictDataService.java

@ -1,7 +1,6 @@ @@ -1,7 +1,6 @@
package com.ruoyi.system.service;
import java.util.List;
import com.ruoyi.system.domain.SysDictData;
/**
@ -20,14 +19,6 @@ public interface ISysDictDataService @@ -20,14 +19,6 @@ public interface ISysDictDataService
public List<SysDictData> selectDictDataList(SysDictData dictData);
/**
* 根据字典类型查询字典数据
*
* @param dictType 字典类型
* @return 字典数据集合信息
*/
public List<SysDictData> selectDictDataByType(String dictType);
/**
* 根据字典类型和字典键值查询字典数据信息
*
* @param dictType 字典类型
@ -45,14 +36,6 @@ public interface ISysDictDataService @@ -45,14 +36,6 @@ public interface ISysDictDataService
public SysDictData selectDictDataById(Long dictCode);
/**
* 通过字典ID删除字典数据信息
*
* @param dictCode 字典数据ID
* @return 结果
*/
public int deleteDictDataById(Long dictCode);
/**
* 批量删除字典数据信息
*
* @param dictCodes 需要删除的字典数据ID

23
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDictTypeService.java

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

48
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictDataServiceImpl.java

@ -1,13 +1,12 @@ @@ -1,13 +1,12 @@
package com.ruoyi.system.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.utils.DictUtils;
/**
* 字典 业务层处理
@ -33,18 +32,6 @@ public class SysDictDataServiceImpl implements ISysDictDataService @@ -33,18 +32,6 @@ public class SysDictDataServiceImpl implements ISysDictDataService
}
/**
* 根据字典类型查询字典数据
*
* @param dictType 字典类型
* @return 字典数据集合信息
*/
@Override
public List<SysDictData> selectDictDataByType(String dictType)
{
return dictDataMapper.selectDictDataByType(dictType);
}
/**
* 根据字典类型和字典键值查询字典数据信息
*
* @param dictType 字典类型
@ -70,18 +57,6 @@ public class SysDictDataServiceImpl implements ISysDictDataService @@ -70,18 +57,6 @@ public class SysDictDataServiceImpl implements ISysDictDataService
}
/**
* 通过字典ID删除字典数据信息
*
* @param dictCode 字典数据ID
* @return 结果
*/
@Override
public int deleteDictDataById(Long dictCode)
{
return dictDataMapper.deleteDictDataById(dictCode);
}
/**
* 批量删除字典数据信息
*
* @param dictCodes 需要删除的字典数据ID
@ -89,7 +64,12 @@ public class SysDictDataServiceImpl implements ISysDictDataService @@ -89,7 +64,12 @@ public class SysDictDataServiceImpl implements ISysDictDataService
*/
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 @@ -101,7 +81,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 +98,11 @@ public class SysDictDataServiceImpl implements ISysDictDataService @@ -113,6 +98,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;
}
}

88
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictTypeServiceImpl.java

@ -1,17 +1,19 @@ @@ -1,17 +1,19 @@
package com.ruoyi.system.service.impl;
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;
import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.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;
/**
* 字典 业务层处理
@ -28,6 +30,20 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService @@ -28,6 +30,20 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
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);
}
}
/**
* 根据条件分页查询字典类型
*
* @param dictType 字典类型信息
@ -51,6 +67,29 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService @@ -51,6 +67,29 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
}
/**
* 根据字典类型查询字典数据
*
* @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查询信息
*
* @param dictId 字典类型ID
@ -74,26 +113,35 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService @@ -74,26 +113,35 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
}
/**
* 通过字典ID删除字典信息
* 批量删除字典类型信息
*
* @param dictId 字典ID
* @param dictIds 需要删除的字典ID
* @return 结果
*/
@Override
public int deleteDictTypeById(Long dictId)
public int deleteDictTypeByIds(Long[] dictIds)
{
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 @@ -105,7 +153,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;
}
/**
@ -120,7 +173,12 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService @@ -120,7 +173,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;
}
/**

65
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/utils/DictUtils.java

@ -0,0 +1,65 @@ @@ -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;
}
}

2
ruoyi-ui/src/api/system/dict/data.js

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

8
ruoyi-ui/src/api/system/dict/type.js

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

1
ruoyi-ui/src/views/system/config/index.vue

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

21
ruoyi-ui/src/views/system/dict/index.vue

@ -94,6 +94,15 @@ @@ -94,6 +94,15 @@
v-hasPermi="['system:dict:export']"
>导出</el-button>
</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-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
@ -173,7 +182,7 @@ @@ -173,7 +182,7 @@
</template>
<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 {
name: "Dict",
@ -333,6 +342,16 @@ export default { @@ -333,6 +342,16 @@ export default {
this.download('system/dict/type/export', {
...this.queryParams
}, `type_${new Date().getTime()}.xlsx`)
},
/** 清理缓存按钮操作 */
handleClearCache() {
clearCache().then(response => {
if (response.code === 200) {
this.msgSuccess("清理成功");
} else {
this.msgError(response.msg);
}
});
}
}
};

Loading…
Cancel
Save