【8.3.3】【dict】【cache】增加对字典typeId+字典编码的缓存

dev-8.3.3
stylefeng 2025-01-10 17:55:41 +08:00
parent 2e59d68520
commit 2d6e1349a4
2 changed files with 22 additions and 3 deletions

View File

@ -67,4 +67,9 @@ public interface CacheConstants {
*/ */
String DEFAULT_STRING_CACHE_PREFIX = "DEFAULT:STRINGS:"; String DEFAULT_STRING_CACHE_PREFIX = "DEFAULT:STRINGS:";
/**
* 60
*/
Long DEFAULT_EXP_SECONDS = 60L * 60L;
} }

View File

@ -29,6 +29,7 @@ import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.cache.api.constants.CacheConstants;
import cn.stylefeng.roses.kernel.dict.api.exception.DictException; import cn.stylefeng.roses.kernel.dict.api.exception.DictException;
import cn.stylefeng.roses.kernel.dict.api.exception.enums.DictExceptionEnum; import cn.stylefeng.roses.kernel.dict.api.exception.enums.DictExceptionEnum;
import cn.stylefeng.roses.kernel.dict.api.pojo.DictDetail; import cn.stylefeng.roses.kernel.dict.api.pojo.DictDetail;
@ -53,7 +54,6 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -279,9 +279,19 @@ public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> implements
} }
@Override @Override
@Cacheable(value = "dicts", key = "#dictTypeCode+#dictCode", unless = "#result.length() == 0")
public String getDictName(String dictTypeCode, String dictCode) { public String getDictName(String dictTypeCode, String dictCode) {
if (ObjectUtil.isEmpty(dictTypeCode) || ObjectUtil.isEmpty(dictCode)) {
return StrUtil.EMPTY;
}
// 先获取缓存中是否有
String cacheKey = dictTypeCode + dictCode;
String dictName = this.dictNameMixedCache.get(cacheKey);
if (dictName != null) {
return dictName;
}
// 获取字典类型编码对应的字典类型id // 获取字典类型编码对应的字典类型id
Long dictTypeId = dictTypeService.getDictTypeIdByDictTypeCode(dictTypeCode); Long dictTypeId = dictTypeService.getDictTypeIdByDictTypeCode(dictTypeCode);
@ -294,16 +304,20 @@ public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> implements
// 如果查询不到字典,则返回空串 // 如果查询不到字典,则返回空串
if (list.isEmpty()) { if (list.isEmpty()) {
this.dictNameMixedCache.put(cacheKey, StrUtil.EMPTY, CacheConstants.DEFAULT_EXP_SECONDS);
return StrUtil.EMPTY; return StrUtil.EMPTY;
} }
// 字典code存在多个重复的返回空串并打印错误日志 // 字典code存在多个重复的返回空串并打印错误日志
if (list.size() > 1) { if (list.size() > 1) {
log.error(DictExceptionEnum.DICT_CODE_REPEAT.getUserTip(), "", dictCode); log.error(DictExceptionEnum.DICT_CODE_REPEAT.getUserTip(), "", dictCode);
this.dictNameMixedCache.put(cacheKey, StrUtil.EMPTY, CacheConstants.DEFAULT_EXP_SECONDS);
return StrUtil.EMPTY; return StrUtil.EMPTY;
} }
return list.get(0).getDictName(); String dictNameResult = list.get(0).getDictName();
this.dictNameMixedCache.put(cacheKey, dictNameResult, CacheConstants.DEFAULT_EXP_SECONDS);
return dictNameResult;
} }
@Override @Override