【8.3.3】【dict】【cache】更新对typeId+dictCode的缓存支持

dev-8.3.3
stylefeng 2025-01-10 16:59:45 +08:00
parent 4e1bf45852
commit 2e59d68520
2 changed files with 87 additions and 23 deletions

View File

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

View File

@ -82,6 +82,9 @@ public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> implements
@Resource(name = "dictInfoCache")
private CacheOperatorApi<DictDetail> dictInfoCache;
@Resource(name = "dictNameMixedCache")
private CacheOperatorApi<String> dictNameMixedCache;
@Override
public List<TreeDictInfo> getTreeDictList(DictRequest dictRequest) {
@ -129,10 +132,21 @@ public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> 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<DictMapper, SysDict> 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<DictMapper, SysDict> 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<DictMapper, SysDict> implements
// 查询字典类型下的所有字典
LambdaQueryWrapper<SysDict> sysDictLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysDictLambdaQueryWrapper.eq(SysDict::getDictTypeId, dictTypeId);
sysDictLambdaQueryWrapper.select(SysDict::getDictId);
sysDictLambdaQueryWrapper.select(SysDict::getDictId, SysDict::getDictTypeId, SysDict::getDictCode);
List<SysDict> 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<DictMapper, SysDict> 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<SysDict> 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<DictMapper, SysDict> 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<DictMapper, SysDict> implements
// 更新字典
this.edit(dictRequest);
// 删除字典详情缓存
this.dictInfoCache.remove(String.valueOf(dictRequest.getDictId()));
}
@Override
@ -561,12 +612,25 @@ public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> implements
@Override
public void batchDelete(DictRequest dictRequest) {
this.removeBatchByIds(dictRequest.getDictIdList());
// 循环删除缓存
for (Long dictId : dictRequest.getDictIdList()) {
this.dictInfoCache.remove(dictId.toString());
// 获取这一批字典的详情
List<SysDict> 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());
}
}