『博客开发日记-后台』之刷新配置缓存接口的实现

本文最后更新于 2026年5月31日 下午

刷新配置缓存接口的实现


刷新配置缓存接口的需求

获取配置列表

清理 Redis 中 config:* 的旧缓存

将最新配置重新写入缓存


代码实现

在 ConfigController 中添加接口

1
2
3
4
5
6
7
8
@PutMapping("/refresh")
@PreAuthorize("@ps.hasPermission('sys:config:update')")
@SystemLog(businessName = "刷新配置缓存")
@ApiOperation(value = "刷新配置缓存接口", notes = "刷新配置缓存", response = String.class)
public ResponseResult refreshConfigCache()
{
return adminConfigService.refreshConfigCache();
}

给刷新配置缓存接口放行


在 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
private static final String CONFIG_CACHE_PREFIX = "config:";

//刷新配置缓存
@Override
public ResponseResult refreshConfigCache()
{
//获取配置列表
List<Config> configList = adminConfigService.list();

//清理 Redis 中 config:* 的旧缓存
Set<Object> keys = redisTemplate.keys(CONFIG_CACHE_PREFIX + "*");
if (keys != null && !keys.isEmpty()) {
redisTemplate.delete(keys);
}

//将最新配置重新写入缓存
if (configList != null && !configList.isEmpty()) {
Map<Object, Object> configCacheMap = configList.stream()
.filter(config -> StringUtils.hasText(config.getConfigKey()))
.collect(Collectors.toMap(
config -> getConfigCacheKey(config.getConfigKey()),
Config::getConfigValue,
(oldValue, newValue) -> newValue
));
redisTemplate.opsForValue().multiSet(configCacheMap);
}

return ResponseResult.okResult();
}




PS:该系列只做为作者学习开发项目做的笔记用

不一定符合读者来学习,仅供参考


预告

后续会记录博客的开发过程

每次学习会做一份笔记来进行发表

“一花一世界,一叶一菩提”


版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/


『博客开发日记-后台』之刷新配置缓存接口的实现
http://example.com/2026/05/30/『博客开发日记-后台』之刷新配置缓存接口的实现/
作者
云梦泽
发布于
2026年5月30日
更新于
2026年5月31日
许可协议