@@ -22,7 +22,7 @@ public class TtsServiceFactory {
2222 private final Map <String , TtsService > serviceCache = new ConcurrentHashMap <>();
2323
2424 // 语音生成文件保存地址
25- private static final String outputPath = "audio/" ;
25+ private static final String OUT_PUT_PATH = "audio/" ;
2626
2727 // 默认服务提供商名称
2828 private static final String DEFAULT_PROVIDER = "edge" ;
@@ -35,13 +35,29 @@ public class TtsServiceFactory {
3535 */
3636 public TtsService getDefaultTtsService () {
3737 // 如果缓存中没有默认服务,则创建一个
38- TtsService edgeService = new EdgeTtsService (DEFAULT_VOICE , outputPath );
39-
40- return edgeService ;
38+ return getTtsService (DEFAULT_VOICE );
4139 }
4240
4341 public TtsService getTtsService () {
44- return new EdgeTtsService (DEFAULT_VOICE , outputPath );
42+ return getTtsService (DEFAULT_VOICE );
43+ }
44+
45+ private TtsService getTtsService (String voiceName ) {
46+ TtsService ttsService = new EdgeTtsService (voiceName , OUT_PUT_PATH );
47+ return ttsService ;
48+ }
49+
50+ private String createCacheKey (SysConfig config ,String provider ){
51+ // 对于API服务,使用"provider:configId"作为缓存键,确保每个配置使用独立的服务实例
52+ String configIdStr ;
53+ if (config == null ) {
54+ configIdStr = "default" ;
55+ }else {
56+ Integer configId = config .getConfigId ();
57+ configIdStr = configId != null ? String .valueOf (configId ) : "default" ;
58+ }
59+ String cacheKey = provider + ":" + configIdStr ;
60+ return cacheKey ;
4561 }
4662
4763 /**
@@ -56,30 +72,34 @@ public TtsService getTtsService(SysConfig config, String voiceName) {
5672 } else {
5773 provider = config .getProvider ();
5874 }
59- // 如果是默认提供商且尚未初始化,则初始化
60- if (DEFAULT_PROVIDER .equals (provider )) {
61- TtsService edgeService = new EdgeTtsService (StringUtils .hasText (voiceName ) ? voiceName : DEFAULT_VOICE , outputPath );
62- return edgeService ;
63- }
64-
65- // 对于API服务,使用"provider:configId"作为缓存键,确保每个配置使用独立的服务实例
66- Integer configId = config .getConfigId ();
67- String cacheKey = provider + ":" + (configId != null ? configId : "default" );
68-
75+ String cacheKey = createCacheKey (config ,provider );
6976 // 检查是否已有该配置的服务实例
7077 if (serviceCache .containsKey (cacheKey )) {
7178 return serviceCache .get (cacheKey );
72- }
73-
74- // 创建新的服务实例
75- try {
76- TtsService service ;
77- // 创建其他API服务
78- service = createApiService (config , voiceName , outputPath );
79- return service ;
80- } catch (Exception e ) {
81- logger .error ("创建{}服务失败" , provider , e );
82- return getDefaultTtsService (); // 失败时返回默认服务
79+ }else {
80+ // 如果是默认提供商且尚未初始化,则初始化
81+ if (DEFAULT_PROVIDER .equals (provider )) {
82+ if (StringUtils .hasText (voiceName )){
83+ TtsService ttsService = getTtsService (voiceName );
84+ serviceCache .put (cacheKey , ttsService );
85+ return ttsService ;
86+ }else {
87+ TtsService ttsService = getTtsService ();
88+ serviceCache .put (cacheKey , ttsService );
89+ return ttsService ;
90+ }
91+ }
92+ // 创建新的服务实例
93+ try {
94+ TtsService service ;
95+ // 创建其他API服务
96+ service = createApiService (config , voiceName , OUT_PUT_PATH );
97+ serviceCache .put (cacheKey , service );
98+ return service ;
99+ } catch (Exception e ) {
100+ logger .error ("创建{}服务失败" , provider , e );
101+ return getDefaultTtsService (); // 失败时返回默认服务
102+ }
83103 }
84104 }
85105
0 commit comments