diff --git a/kernel-s-dict/dict-api/src/main/java/cn/stylefeng/roses/kernel/dict/api/format/DictFormatProcess.java b/kernel-s-dict/dict-api/src/main/java/cn/stylefeng/roses/kernel/dict/api/format/DictFormatProcess.java index de2645c28..76f1b681b 100644 --- a/kernel-s-dict/dict-api/src/main/java/cn/stylefeng/roses/kernel/dict/api/format/DictFormatProcess.java +++ b/kernel-s-dict/dict-api/src/main/java/cn/stylefeng/roses/kernel/dict/api/format/DictFormatProcess.java @@ -22,9 +22,9 @@ public class DictFormatProcess extends BaseSimpleFieldFormatProcess { @Override public Object simpleItemFormat(Object businessId) { - Long orgId = Convert.toLong(businessId); + Long dictId = Convert.toLong(businessId); DictApi bean = SpringUtil.getBean(DictApi.class); - return bean.getDictNameByDictId(orgId); + return bean.getDictNameByDictId(dictId); } } diff --git a/kernel-s-dict/dict-business/src/main/java/cn/stylefeng/roses/kernel/dict/modular/service/impl/DictServiceImpl.java b/kernel-s-dict/dict-business/src/main/java/cn/stylefeng/roses/kernel/dict/modular/service/impl/DictServiceImpl.java index 122152a8d..a8842d0c9 100644 --- a/kernel-s-dict/dict-business/src/main/java/cn/stylefeng/roses/kernel/dict/modular/service/impl/DictServiceImpl.java +++ b/kernel-s-dict/dict-business/src/main/java/cn/stylefeng/roses/kernel/dict/modular/service/impl/DictServiceImpl.java @@ -82,6 +82,9 @@ public class DictServiceImpl extends ServiceImpl implements @Resource(name = "dictInfoCache") private CacheOperatorApi dictInfoCache; + @Resource(name = "dictNameMixedCache") + private CacheOperatorApi dictNameMixedCache; + @Override public List getTreeDictList(DictRequest dictRequest) { @@ -129,10 +132,21 @@ public class DictServiceImpl extends ServiceImpl implements @Override public void del(DictRequest dictRequest) { - this.removeById(dictRequest.getDictId()); - // 删除字典缓存 + // 获取字典id的详情 + SysDict sysDict = this.getById(dictRequest.getDictId()); + if (sysDict == null) { + return; + } + + // 删除字典id为key的缓存 this.dictInfoCache.remove(String.valueOf(dictRequest.getDictId())); + + // 删除字典类型id+字典编码为缓存key的缓存 + this.dictNameMixedCache.remove(sysDict.getDictTypeId() + sysDict.getDictCode()); + + // 真实删除字典 + this.removeById(dictRequest.getDictId()); } @Override @@ -142,6 +156,14 @@ public class DictServiceImpl extends ServiceImpl implements this.validateRepeat(dictRequest, true); SysDict sysDict = this.querySysDict(dictRequest); + + // 删除旧的字典的缓存,删除字典类型id+字典编码为缓存key的缓存 + this.dictNameMixedCache.remove(sysDict.getDictTypeId() + sysDict.getDictCode()); + + // 删除字典名称缓存 + this.dictInfoCache.remove(String.valueOf(sysDict.getDictId())); + + // copy前端的属性 BeanUtil.copyProperties(dictRequest, sysDict); // 不能修改字典类型、编码和字典的上下级关系(上下级关系和顺序,通过更新字典树接口更方便) @@ -153,9 +175,6 @@ public class DictServiceImpl extends ServiceImpl implements sysDict.setDictNamePinyin(pinYinApi.parseEveryPinyinFirstLetter(sysDict.getDictName())); this.updateById(sysDict); - - // 删除字典名称缓存 - this.dictInfoCache.remove(String.valueOf(sysDict.getDictId())); } @Override @@ -185,14 +204,17 @@ public class DictServiceImpl extends ServiceImpl implements // 查询字典类型下的所有字典 LambdaQueryWrapper sysDictLambdaQueryWrapper = new LambdaQueryWrapper<>(); sysDictLambdaQueryWrapper.eq(SysDict::getDictTypeId, dictTypeId); - sysDictLambdaQueryWrapper.select(SysDict::getDictId); + sysDictLambdaQueryWrapper.select(SysDict::getDictId, SysDict::getDictTypeId, SysDict::getDictCode); List list = this.list(sysDictLambdaQueryWrapper); if (ObjectUtil.isEmpty(list)) { return; } - // 删除所有字典的缓存 list.forEach(sysDict -> { + // 删除旧的字典的缓存,删除字典类型id+字典编码为缓存key的缓存 + this.dictNameMixedCache.remove(sysDict.getDictTypeId() + sysDict.getDictCode()); + + // 删除字典名称缓存,key是字典id this.dictInfoCache.remove(String.valueOf(sysDict.getDictId())); }); @@ -225,18 +247,35 @@ public class DictServiceImpl extends ServiceImpl implements } @Override - @Cacheable(value = "dicts", key = "#dictTypeId+#dictCode", unless = "#result.length() == 0") public String getDictName(Long dictTypeId, String dictCode) { + + if (ObjectUtil.isEmpty(dictTypeId) || ObjectUtil.isEmpty(dictCode)) { + return ""; + } + + // 拼接缓存key + String cacheKey = dictTypeId + dictCode; + + // 获取缓存中是否有值 + String dictName = this.dictNameMixedCache.get(cacheKey); + if (dictName != null) { + return dictName; + } + LambdaQueryWrapper sysDictLambdaQueryWrapper = new LambdaQueryWrapper<>(); sysDictLambdaQueryWrapper.eq(SysDict::getDictTypeId, dictTypeId); sysDictLambdaQueryWrapper.eq(SysDict::getDictCode, dictCode); sysDictLambdaQueryWrapper.select(SysDict::getDictName); SysDict sysDict = this.getOne(sysDictLambdaQueryWrapper, false); - if (ObjectUtil.isEmpty(sysDict)) { - return ""; - } - return sysDict.getDictName(); + // 添加结果到缓存,然后再返回 + if (ObjectUtil.isEmpty(sysDict)) { + this.dictNameMixedCache.put(cacheKey, StrUtil.EMPTY); + return ""; + } else { + this.dictNameMixedCache.put(cacheKey, sysDict.getDictName()); + return sysDict.getDictName(); + } } @Override @@ -304,10 +343,25 @@ public class DictServiceImpl extends ServiceImpl implements @Override public void deleteByDictId(Long dictId) { - this.removeById(dictId); - // 删除字典详情缓存 + if (dictId == null) { + return; + } + + // 获取字典详情 + SysDict sysDict = this.getById(dictId); + if (sysDict == null) { + return; + } + + // 删除字典详情缓存,key是字典id this.dictInfoCache.remove(dictId.toString()); + + // 删除旧的字典的缓存,key是字典类型id+字典编码 + this.dictNameMixedCache.remove(sysDict.getDictTypeId() + sysDict.getDictCode()); + + // 删除字典记录 + this.removeById(dictId); } @Override @@ -379,9 +433,6 @@ public class DictServiceImpl extends ServiceImpl implements // 更新字典 this.edit(dictRequest); - - // 删除字典详情缓存 - this.dictInfoCache.remove(String.valueOf(dictRequest.getDictId())); } @Override @@ -561,12 +612,25 @@ public class DictServiceImpl extends ServiceImpl implements @Override public void batchDelete(DictRequest dictRequest) { - this.removeBatchByIds(dictRequest.getDictIdList()); - // 循环删除缓存 - for (Long dictId : dictRequest.getDictIdList()) { - this.dictInfoCache.remove(dictId.toString()); + // 获取这一批字典的详情 + List dictList = this.listByIds(dictRequest.getDictIdList()); + if (ObjectUtil.isEmpty(dictList)) { + return; } + + // 删除这一批字典的缓存 + for (SysDict sysDict : dictList) { + + // 删除key为字典id的缓存 + this.dictInfoCache.remove(sysDict.getDictId().toString()); + + // 删除旧的字典的缓存,key是字典类型id+字典编码 + this.dictNameMixedCache.remove(sysDict.getDictTypeId() + sysDict.getDictCode()); + } + + // 删除字典 + this.removeBatchByIds(dictRequest.getDictIdList()); } }