本文最后更新于 2026年6月13日 晚上
获取后台配置接口的实现
获取后台配置接口的需求
先从缓存读取后台配置
只获取后台配置
如果缓存没有配置就从数据库中获取
代码实现
在 ConfigController 中添加接口
1 2 3 4 5 6 7
| @GetMapping("/site") @SystemLog(businessName = "获取后台系统站点配置接口") @ApiOperation(value = "获取后台系统站点配置接口", notes = "获取博客后台站点的基本配置") public ResponseResult getSiteConfig() { return adminConfigService.getSiteConfig(); }
|
由于是前后台两个接口共用一个vo,所以这里不用新建
在 AdminConfigServiceImpl 中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
@Service("configService") public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements ConfigService { private static final String CONFIG_CACHE_PREFIX = "config:";
@Autowired private RedisTemplate<Object, Object> redisTemplate;
@Override @SuppressWarnings("unchecked") public ResponseResult getBlogConfig() { Set<Object> keys = redisTemplate.keys(RedisKeyConstants.CONFIG_KEY_PREFIX + "*"); List<ConfigVo> configVos = new ArrayList<>();
if (keys != null && !keys.isEmpty()) { keys.forEach(key -> { Object value = redisTemplate.opsForValue().get(key); if (value instanceof ConfigCacheVo configCacheVo && SystemConstants.CONFIG_TYPE_IS_BLOG.equals(configCacheVo.getConfigType())) { configVos.add(new ConfigVo(configCacheVo.getConfigKey(), configCacheVo.getConfigValue())); } }); }
if (configVos.isEmpty()) { LambdaQueryWrapper<Config> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.select(Config::getConfigKey, Config::getConfigValue) .eq(Config::getConfigType, SystemConstants.CONFIG_TYPE_IS_BLOG) .orderByAsc(Config::getId);
List<Config> configList = list(queryWrapper); configVos.addAll(BeanCopyUtils.copyBeanList(configList, ConfigVo.class)); }
return ResponseResult.okResult(configVos); } }
|
给获取后台配置接口放行
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/