Skip to content

Commit a5fe167

Browse files
committed
refactor:重构缓存逻辑
1 parent 03e4319 commit a5fe167

File tree

9 files changed

+74
-72
lines changed

9 files changed

+74
-72
lines changed

src/main/java/com/xiaozhi/communication/common/ConfigManager.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/main/java/com/xiaozhi/communication/common/MessageHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.xiaozhi.entity.SysDevice;
1717
import com.xiaozhi.entity.SysRole;
1818
import com.xiaozhi.enums.ListenState;
19+
import com.xiaozhi.service.SysConfigService;
1920
import com.xiaozhi.service.SysDeviceService;
2021
import com.xiaozhi.service.SysRoleService;
2122
import jakarta.annotation.Resource;
@@ -52,7 +53,7 @@ public class MessageHandler {
5253
private SessionManager sessionManager;
5354

5455
@Resource
55-
private ConfigManager configManager;
56+
private SysConfigService configService;
5657

5758
@Resource
5859
private DialogueService dialogueService;
@@ -111,13 +112,13 @@ public void afterConnection(ChatSession chatSession, String deviceIdAuth) {
111112
SysRole role = roleService.selectRoleById(device.getRoleId());
112113

113114
if (role.getSttId() != null) {
114-
SysConfig sttConfig = configManager.getConfig(role.getSttId());
115+
SysConfig sttConfig = configService.selectConfigById(role.getSttId());
115116
if (sttConfig != null) {
116117
sttFactory.getSttService(sttConfig);// 提前初始化,加速后续使用
117118
}
118119
}
119120
if (role.getTtsId() != null) {
120-
SysConfig ttsConfig = configManager.getConfig(role.getTtsId());
121+
SysConfig ttsConfig = configService.selectConfigById(role.getTtsId());
121122
if (ttsConfig != null) {
122123
ttsFactory.getTtsService(ttsConfig, role.getVoiceName());// 提前初始化,加速后续使用
123124
}

src/main/java/com/xiaozhi/controller/ConfigController.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.github.pagehelper.PageInfo;
77
import com.xiaozhi.common.web.AjaxResult;
88
import com.xiaozhi.common.web.PageFilter;
9-
import com.xiaozhi.communication.common.ConfigManager;
109
import com.xiaozhi.dialogue.stt.factory.SttServiceFactory;
1110
import com.xiaozhi.dialogue.tts.factory.TtsServiceFactory;
1211
import com.xiaozhi.entity.SysConfig;
@@ -40,9 +39,6 @@ public class ConfigController extends BaseController {
4039
@Resource
4140
private SysConfigService configService;
4241

43-
@Resource
44-
private ConfigManager configManager;
45-
4642
@Resource
4743
private TtsServiceFactory ttsServiceFactory;
4844

@@ -84,7 +80,6 @@ public AjaxResult update(SysConfig config) {
8480
SysConfig oldSysConfig = configService.selectConfigById(config.getConfigId());
8581
int rows = configService.update(config);
8682
if (rows > 0) {
87-
configManager.getConfig(config.getConfigId());// 更新缓存
8883
if (oldSysConfig != null) {
8984
if ("stt".equals(oldSysConfig.getConfigType())
9085
&& !oldSysConfig.getApiKey().equals(config.getApiKey())) {

src/main/java/com/xiaozhi/dialogue/llm/factory/ChatModelFactory.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,23 @@ public ChatModel takeChatModel(ChatSession session) {
7373
}
7474

7575
public ChatModel takeVisionModel() {
76-
SysConfig config = configService.query(new SysConfig().setConfigType("llm").setModelType("vision"), null).get(0);
76+
SysConfig config = configService.selectModelType("vision");
7777
Assert.notNull(config, "未配置多模态模型");
7878
return createChatModel(config, new SysRole());
7979
}
8080

81+
public ChatModel takeIntentModel() {
82+
SysConfig config = configService.selectModelType("intent");
83+
Assert.notNull(config, "未配置意图识别模型");
84+
return createChatModel(config, new SysRole());
85+
}
86+
87+
public ChatModel takeEmbeddingModel() {
88+
SysConfig config = configService.selectModelType("embedding");
89+
Assert.notNull(config, "未配置向量模型");
90+
return createChatModel(config, new SysRole());
91+
}
92+
8193
/**
8294
* 创建ChatModel
8395
*

src/main/java/com/xiaozhi/dialogue/llm/providers/CozeChatModel.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
*/
3131
public class CozeChatModel implements ChatModel {
3232

33-
private final TokenAuth authCli;
3433
private final CozeAPI coze;
3534
private final String botId;
3635

@@ -46,7 +45,7 @@ public class CozeChatModel implements ChatModel {
4645
public CozeChatModel(String apiSecret, String model) {
4746

4847
// 使用apiSecret作为access_token
49-
this.authCli = new TokenAuth(apiSecret);
48+
TokenAuth authCli = new TokenAuth(apiSecret);
5049

5150
// 使用endpoint或默认的Coze API地址
5251
String baseUrl = "https://api.coze.cn";

src/main/java/com/xiaozhi/dialogue/service/DialogueService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.xiaozhi.dialogue.service;
22

33
import com.xiaozhi.communication.common.ChatSession;
4-
import com.xiaozhi.communication.common.ConfigManager;
54
import com.xiaozhi.communication.common.SessionManager;
65
import com.xiaozhi.dialogue.llm.ChatService;
76
import com.xiaozhi.dialogue.service.VadService.VadStatus;
@@ -12,6 +11,7 @@
1211
import com.xiaozhi.entity.SysDevice;
1312
import com.xiaozhi.entity.SysRole;
1413
import com.xiaozhi.event.ChatSessionCloseEvent;
14+
import com.xiaozhi.service.SysConfigService;
1515
import com.xiaozhi.service.SysRoleService;
1616
import com.xiaozhi.utils.AudioUtils;
1717
import com.xiaozhi.utils.EmojiUtils;
@@ -75,7 +75,7 @@ public class DialogueService implements ApplicationListener<ChatSessionCloseEven
7575
private SessionManager sessionManager;
7676

7777
@Resource
78-
private ConfigManager configManager;
78+
private SysConfigService configService;
7979

8080
@Resource
8181
private SysRoleService roleService;
@@ -282,7 +282,7 @@ public void processAudioData(ChatSession session, byte[] opusData) {
282282
}
283283
SysRole role = roleService.selectRoleById(device.getRoleId());
284284
// 获取STT和TTS配置
285-
SysConfig sttConfig = role.getSttId() != null ? configManager.getConfig(role.getSttId())
285+
SysConfig sttConfig = role.getSttId() != null ? configService.selectConfigById(role.getSttId())
286286
: null;
287287

288288
// 处理VAD
@@ -490,7 +490,7 @@ private void handleSentence(
490490
// 新增加的设备很有可能没有配置TTS,采用默认Edge需要传递null
491491
final SysConfig ttsConfig;
492492
if (role.getTtsId() != null) {
493-
ttsConfig = configManager.getConfig(role.getTtsId());
493+
ttsConfig = configService.selectConfigById(role.getTtsId());
494494
} else {
495495
ttsConfig = null;
496496
}

src/main/java/com/xiaozhi/service/SysConfigService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ public interface SysConfigService {
4444
* @return
4545
*/
4646
SysConfig selectConfigById(Integer configId);
47+
48+
/**
49+
* 查询默认配置
50+
*/
51+
SysConfig selectModelType(String modelType);
4752
}

src/main/java/com/xiaozhi/service/impl/SysConfigServiceImpl.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import jakarta.annotation.Resource;
1111
import org.springframework.cache.annotation.CacheEvict;
1212
import org.springframework.cache.annotation.Cacheable;
13+
import org.springframework.cache.annotation.Caching;
1314
import org.springframework.stereotype.Service;
1415
import org.springframework.transaction.annotation.Transactional;
1516

@@ -62,7 +63,10 @@ public int add(SysConfig config) {
6263
*/
6364
@Override
6465
@Transactional(transactionManager = "transactionManager")
65-
@CacheEvict(value = CACHE_NAME, key = "#config.configId")
66+
@Caching(evict = {
67+
@CacheEvict(value = CACHE_NAME, key = "#config.configId"),
68+
@CacheEvict(value = CACHE_NAME, key = "#config.modelType", condition = "#config.modelType != null")
69+
})
6670
public int update(SysConfig config) {
6771
// 如果当前配置被设置为默认,则将同类型同用户的其他配置设置为非默认
6872
if (config.getIsDefault() != null && config.getIsDefault().equals("1")) {
@@ -73,7 +77,6 @@ public int update(SysConfig config) {
7377
sttServiceFactory.removeCache(config);
7478
ttsServiceFactory.removeCache(config);
7579
}
76-
// TODO 还需处理缓存的默认视觉模型或者意图识别模型等
7780
return rows;
7881
}
7982

@@ -124,4 +127,28 @@ public SysConfig selectConfigById(Integer configId) {
124127
return configMapper.selectConfigById(configId);
125128
}
126129

130+
/**
131+
* 查询默认配置
132+
*
133+
* @param modelType
134+
* @return 配置
135+
*/
136+
@Override
137+
@Cacheable(value = CACHE_NAME, key ="#modelType", unless = "#result == null")
138+
public SysConfig selectModelType(String modelType) {
139+
SysConfig queryConfig = new SysConfig();
140+
queryConfig.setModelType(modelType);
141+
142+
List<SysConfig> modelConfigs = configMapper.query(queryConfig);
143+
144+
for (SysConfig config : modelConfigs) {
145+
if ("1".equals(config.getIsDefault())) {
146+
return config; // 找到默认配置就返回
147+
}
148+
}
149+
150+
// 没有默认配置,返回第一个即可
151+
return modelConfigs.isEmpty() ? null : modelConfigs.getFirst();
152+
}
153+
127154
}

src/main/java/com/xiaozhi/service/impl/SysDeviceServiceImpl.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.github.pagehelper.PageHelper;
44
import com.xiaozhi.common.web.PageFilter;
55
import com.xiaozhi.communication.common.ChatSession;
6-
import com.xiaozhi.communication.common.ConfigManager;
76
import com.xiaozhi.communication.common.SessionManager;
87
import com.xiaozhi.dao.ConfigMapper;
98
import com.xiaozhi.dao.DeviceMapper;
@@ -57,9 +56,6 @@ public class SysDeviceServiceImpl extends BaseServiceImpl implements SysDeviceSe
5756
@Resource
5857
private SessionManager sessionManager;
5958

60-
@Resource
61-
private ConfigManager configManager;
62-
6359
/**
6460
* 添加设备
6561
*
@@ -81,18 +77,26 @@ public int add(SysDevice device) throws NotFoundException {
8177
queryRole.setUserId(device.getUserId());
8278
List<SysRole> roles = roleMapper.query(queryRole);
8379

84-
if (roles.size() > 0) {
85-
// 优先绑定默认角色,否则随便绑定一个
86-
for (SysRole role: roles) {
87-
device.setRoleId(role.getRoleId());
88-
if (role.getIsDefault().equals("1")) {
89-
break;
90-
}
91-
}
92-
return deviceMapper.add(device);
93-
} else {
80+
if (roles.isEmpty()) {
9481
throw new NotFoundException("没有配置角色");
9582
}
83+
84+
SysRole selectedRole = null;
85+
86+
// 优先绑定默认角色
87+
for (SysRole role: roles) {
88+
if (("1").equals(role.getIsDefault())) {
89+
selectedRole = role;
90+
break;
91+
}
92+
}
93+
if (selectedRole == null) {
94+
selectedRole = roles.getFirst();
95+
}
96+
97+
device.setRoleId(selectedRole.getRoleId());
98+
return deviceMapper.add(device);
99+
96100
}
97101

98102
/**

0 commit comments

Comments
 (0)