Переглянути джерело

今正豆包声音复刻优化成缓存获取speakerId

lk 6 днів тому
батько
коміт
6e956cf69d

+ 21 - 1
fs-service/src/main/java/com/fs/wxwork/service/WxWorkServiceImpl.java

@@ -370,7 +370,27 @@ public class WxWorkServiceImpl implements WxWorkService {
 
     @Override
     public WxwSilkVoceDTO getSilkVoiceDoubao(String content, Long companyUserId, QwUser user) {
-        VcCompanyUser vcCompanyUser = companyUserMapper.selectVcCompanyUserByCompanyUserId(companyUserId);
+        String cacheKey = "voice:clone:company:user:id" + companyUserId;
+        VcCompanyUser vcCompanyUser = redisCache.getCacheObject(cacheKey);
+        if (vcCompanyUser == null) {
+            // 使用双重检查锁,防止缓存击穿
+            synchronized (this) {
+                // 再次检查缓存,防止在等待锁的过程中已经被其他线程加载
+                vcCompanyUser = redisCache.getCacheObject(cacheKey);
+                if (vcCompanyUser == null) {
+                    // 查询数据库
+                    vcCompanyUser = companyUserMapper.selectVcCompanyUserByCompanyUserId(companyUserId);
+                    if (vcCompanyUser == null) {
+                        // 缓存空对象,防止缓存穿透
+                        redisCache.setCacheObject(cacheKey, new VcCompanyUser(), 3600, TimeUnit.SECONDS);
+                        throw new RuntimeException("用户不存在");
+                    }
+                    // 设置缓存
+                    redisCache.setCacheObject(cacheKey, vcCompanyUser, 3600, TimeUnit.SECONDS);
+                }
+            }
+        }
+//        VcCompanyUser vcCompanyUser = companyUserMapper.selectVcCompanyUserByCompanyUserId(companyUserId);
         try {
             if (vcCompanyUser == null)throw new RuntimeException("用户不存在");
         }catch (Exception e){

+ 38 - 6
fs-user-app/src/main/java/com/fs/app/controller/CompanyUserController.java

@@ -68,6 +68,7 @@ import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.util.EntityUtils;
+import org.redisson.api.RedissonClient;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeanUtils;
@@ -131,6 +132,8 @@ public class CompanyUserController extends AppBaseController {
     @Autowired
     private QwUserMapper qwUserMapper;
 
+    private static final String VOICE_CLONE_CACHE_KEY = "voice:clone:company:user:id";
+
     @PostMapping("/login")
     public R Login(@RequestBody CompanyUserLoginParam param, HttpServletRequest request) {
         try {
@@ -326,8 +329,9 @@ public class CompanyUserController extends AppBaseController {
             JSONObject vcConfig = configUtil.generateConfigByKey(SysConfigEnum.VS_CONFIG.getKey());
             if (vcConfig != null && !vcConfig.isEmpty() &&
                     "2".equals(vcConfig.getString("type"))) {
-                VcCompanyUser vcCompanyUser = companyUserMapper.selectVcCompanyUserByCompanyUserId(companyUserId);
-                if (vcCompanyUser == null) throw new RuntimeException("用户不存在");
+                VcCompanyUser vcCompanyUser = getVcCompanyUser(companyUserId);
+//                VcCompanyUser vcCompanyUser = companyUserMapper.selectVcCompanyUserByCompanyUserId(companyUserId);
+//                if (vcCompanyUser == null) throw new RuntimeException("用户不存在");
                 audioVO = ttsServiceImpl.textToSpeech(new TtsRequest(null, null, vcCompanyUser.getSpeakerId(), qwSopTempVoice.getVoiceTxt().replace(" ", "")));
                 QwUser qwUser = new QwUser();
                 qwUser.setCompanyUserId(companyUserId);
@@ -355,6 +359,33 @@ public class CompanyUserController extends AppBaseController {
         return R.ok().put("data", audioVO);
     }
 
+    private static final Integer CACHE_TTL = 3600; // 缓存过期时间,单位秒
+
+    public VcCompanyUser getVcCompanyUser(Long companyUserId) {
+        String cacheKey = VOICE_CLONE_CACHE_KEY + companyUserId;
+        VcCompanyUser vcCompanyUser = redisCache.getCacheObject(cacheKey);
+        if (vcCompanyUser != null) {
+            return vcCompanyUser;
+        }
+        // 使用双重检查锁,防止缓存击穿
+        synchronized (this) {
+            // 再次检查缓存,防止在等待锁的过程中已经被其他线程加载
+            vcCompanyUser = redisCache.getCacheObject(cacheKey);
+            if (vcCompanyUser != null) {
+                return vcCompanyUser;
+            }
+            // 查询数据库
+            vcCompanyUser = companyUserMapper.selectVcCompanyUserByCompanyUserId(companyUserId);
+            if (vcCompanyUser == null) {
+                // 缓存空对象,防止缓存穿透
+                redisCache.setCacheObject(cacheKey, new VcCompanyUser(), CACHE_TTL,TimeUnit.SECONDS);
+                throw new RuntimeException("用户不存在");
+            }
+            // 设置缓存
+            redisCache.setCacheObject(cacheKey, vcCompanyUser, CACHE_TTL,TimeUnit.SECONDS);
+            return vcCompanyUser;
+        }
+    }
     /**
      * 当只有user_voice_url时,生成表中对应条的voice_url
      *
@@ -380,8 +411,9 @@ public class CompanyUserController extends AppBaseController {
             JSONObject vcConfig = configUtil.generateConfigByKey(SysConfigEnum.VS_CONFIG.getKey());
             if (vcConfig != null && !vcConfig.isEmpty() &&
                     "2".equals(vcConfig.getString("type"))) {
-                VcCompanyUser vcCompanyUser = companyUserMapper.selectVcCompanyUserByCompanyUserId(companyUserId);
-                if (vcCompanyUser == null) throw new RuntimeException("用户不存在");
+                VcCompanyUser vcCompanyUser = getVcCompanyUser(companyUserId);
+//                VcCompanyUser vcCompanyUser = companyUserMapper.selectVcCompanyUserByCompanyUserId(companyUserId);
+//                if (vcCompanyUser == null) throw new RuntimeException("用户不存在");
                 audioVO = ttsServiceImpl.textToSpeech(new TtsRequest(null, null, vcCompanyUser.getSpeakerId(), qwSopTempVoice.getVoiceTxt().replace(" ", "")));
                 QwUser qwUser = new QwUser();
                 qwUser.setCompanyUserId(companyUserId);
@@ -816,8 +848,8 @@ public class CompanyUserController extends AppBaseController {
             HttpServletRequest httpRequest) throws Exception {
         if (companyUserId == null) companyUserId = 123L;
 //                getCompanyUserId();
-
-        VcCompanyUser vcCompanyUser = companyUserMapper.selectVcCompanyUserByCompanyUserId(companyUserId);
+        VcCompanyUser vcCompanyUser = getVcCompanyUser(companyUserId);
+//        VcCompanyUser vcCompanyUser = companyUserMapper.selectVcCompanyUserByCompanyUserId(companyUserId);
         if (vcCompanyUser == null) throw new RuntimeException("用户不存在");
         AudioVO audioVO = voiceCloneController.synthesizeSimple(text, vcCompanyUser.getSpeakerId(), "mp3", 1);
         vcCompanyUser.setLatestTextToSpeechUrl(audioVO.getUrl());