优化参数管理,缓存中不存在,则从数据库读取

This commit is contained in:
阿沐 2023-05-29 12:49:58 +08:00
parent 1a97405a3c
commit e796e8ac1c
2 changed files with 18 additions and 5 deletions

View File

@ -15,6 +15,10 @@ import org.apache.ibatis.annotations.Mapper;
public interface SysParamsDao extends BaseDao<SysParamsEntity> {
default boolean isExist(String paramKey) {
return this.exists(new QueryWrapper<SysParamsEntity>().eq("param_key" , paramKey));
return this.exists(new QueryWrapper<SysParamsEntity>().eq("param_key", paramKey));
}
default SysParamsEntity get(String paramKey) {
return this.selectOne(new QueryWrapper<SysParamsEntity>().eq("param_key", paramKey));
}
}

View File

@ -7,9 +7,9 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.PostConstruct;
import lombok.AllArgsConstructor;
import net.maku.framework.common.exception.ServerException;
import net.maku.framework.common.utils.JsonUtils;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.framework.common.utils.JsonUtils;
import net.maku.system.cache.SysParamsCache;
import net.maku.system.convert.SysParamsConvert;
import net.maku.system.dao.SysParamsDao;
@ -116,11 +116,20 @@ public class SysParamsServiceImpl extends BaseServiceImpl<SysParamsDao, SysParam
@Override
public String getString(String paramKey) {
String value = sysParamsCache.get(paramKey);
if (StrUtil.isBlank(value)) {
throw new ServerException("参数不能为空paramKey" + paramKey);
if (StrUtil.isNotBlank(value)) {
return value;
}
return value;
// 如果缓存没有则查询数据库
SysParamsEntity entity = baseMapper.get(paramKey);
if (entity == null) {
throw new ServerException("参数值不存在paramKey" + paramKey);
}
// 保存到缓存
sysParamsCache.save(entity.getParamKey(), entity.getParamValue());
return entity.getParamValue();
}
@Override