perf(notify): 优化微信订阅消息配置查询性能

- 为微信订阅消息配置接口添加 Redis 缓存支持
- 在 WechatSubscribeNotifyConfigRepository 中实现缓存读写和清除机制
- 修改 Controller 层接口添加 @IgnoreToken 注解支持匿名访问
- 优化查询逻辑,添加 memberId 为空时的提前返回处理
- 在管理服务中添加缓存清除逻辑,确保配置变更时缓存同步更新
- 实现批量缓存清除功能,支持按景区和全局范围清除缓存
This commit is contained in:
2026-01-07 17:28:51 +08:00
parent 3291371dd7
commit e896f58d82
4 changed files with 403 additions and 98 deletions

View File

@@ -1,21 +1,14 @@
package com.ycwl.basic.service.notify;
import com.ycwl.basic.model.pc.notify.entity.WechatSubscribeEventTemplateEntity;
import com.ycwl.basic.model.pc.notify.entity.WechatSubscribeSceneTemplateEntity;
import com.ycwl.basic.model.pc.notify.entity.WechatSubscribeTemplateConfigEntity;
import com.ycwl.basic.repository.WechatSubscribeNotifyConfigRepository;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
/**
* 微信订阅消息配置查询服务(仅负责配置解析,不包含授权/发送逻辑)
* 微信订阅消息配置查询服务(仅负责"配置解析",不包含授权/发送逻辑)
* 底层通过 Repository 实现 Redis 缓存
*
* @Author: System
* @Date: 2025/12/31
@@ -29,53 +22,53 @@ public class WechatSubscribeNotifyConfigService {
this.configRepository = configRepository;
}
/**
* 获取场景下的模板配置列表(带缓存)
*
* @param scenicId 景区ID
* @param sceneKey 场景标识
* @return 启用的模板配置列表
*/
public List<WechatSubscribeTemplateConfigEntity> listSceneTemplateConfigs(Long scenicId, String sceneKey) {
List<WechatSubscribeSceneTemplateEntity> mappings =
configRepository.listEffectiveSceneTemplateMappings(scenicId, sceneKey);
if (CollectionUtils.isEmpty(mappings)) {
return new ArrayList<>();
}
Set<String> templateKeys = mappings.stream()
.map(WechatSubscribeSceneTemplateEntity::getTemplateKey)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
Map<String, WechatSubscribeTemplateConfigEntity> configMap =
configRepository.getEffectiveTemplateConfigs(scenicId, templateKeys);
List<WechatSubscribeTemplateConfigEntity> result = new ArrayList<>();
for (WechatSubscribeSceneTemplateEntity mapping : mappings) {
WechatSubscribeTemplateConfigEntity cfg = configMap.get(mapping.getTemplateKey());
if (cfg == null || !Objects.equals(cfg.getEnabled(), 1)) {
continue;
}
result.add(cfg);
}
return result;
return configRepository.getSceneTemplateConfigsCached(scenicId, sceneKey);
}
/**
* 获取事件下的模板配置列表(带缓存)
*
* @param scenicId 景区ID
* @param eventKey 事件标识
* @return 启用的模板配置列表
*/
public List<WechatSubscribeTemplateConfigEntity> listEventTemplateConfigs(Long scenicId, String eventKey) {
List<WechatSubscribeEventTemplateEntity> mappings =
configRepository.listEffectiveEventTemplateMappings(scenicId, eventKey);
if (CollectionUtils.isEmpty(mappings)) {
return new ArrayList<>();
}
return configRepository.getEventTemplateConfigsCached(scenicId, eventKey);
}
Set<String> templateKeys = mappings.stream()
.map(WechatSubscribeEventTemplateEntity::getTemplateKey)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
Map<String, WechatSubscribeTemplateConfigEntity> configMap =
configRepository.getEffectiveTemplateConfigs(scenicId, templateKeys);
/**
* 清除指定场景的配置缓存
*/
public void clearSceneConfigsCache(Long scenicId, String sceneKey) {
configRepository.clearSceneTemplateConfigsCache(scenicId, sceneKey);
}
List<WechatSubscribeTemplateConfigEntity> result = new ArrayList<>();
for (WechatSubscribeEventTemplateEntity mapping : mappings) {
WechatSubscribeTemplateConfigEntity cfg = configMap.get(mapping.getTemplateKey());
if (cfg == null || !Objects.equals(cfg.getEnabled(), 1)) {
continue;
}
result.add(cfg);
}
return result;
/**
* 清除指定事件的配置缓存
*/
public void clearEventConfigsCache(Long scenicId, String eventKey) {
configRepository.clearEventTemplateConfigsCache(scenicId, eventKey);
}
/**
* 清除景区下所有订阅消息配置缓存
*/
public void clearAllConfigsCacheByScenic(Long scenicId) {
configRepository.clearAllConfigsCacheByScenic(scenicId);
}
/**
* 清除所有订阅消息配置缓存(全局配置变更时调用)
*/
public void clearAllConfigsCache() {
configRepository.clearAllConfigsCache();
}
}