From ab7e25243dd384172e40784782ed26f98037df48 Mon Sep 17 00:00:00 2001 From: Quan HL Date: Mon, 12 Aug 2024 20:10:43 +0700 Subject: [PATCH] improve on check precache --- lib/config.js | 2 ++ lib/synth-audio.js | 35 +++++++++++++++++------------------ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/config.js b/lib/config.js index 003901b..ea6f61a 100644 --- a/lib/config.js +++ b/lib/config.js @@ -1,6 +1,7 @@ const JAMBONES_TTS_TRIM_SILENCE = process.env.JAMBONES_TTS_TRIM_SILENCE; const JAMBONES_DISABLE_TTS_STREAMING = process.env.JAMBONES_DISABLE_TTS_STREAMING; const JAMBONES_DISABLE_AZURE_TTS_STREAMING = process.env.JAMBONES_DISABLE_AZURE_TTS_STREAMING; +const JAMBONES_EAGERLY_PRE_CACHE_AUDIO = process.env.JAMBONES_EAGERLY_PRE_CACHE_AUDIO; const JAMBONES_HTTP_PROXY_IP = process.env.JAMBONES_HTTP_PROXY_IP; const JAMBONES_HTTP_PROXY_PORT = process.env.JAMBONES_HTTP_PROXY_PORT; @@ -18,6 +19,7 @@ module.exports = { JAMBONES_HTTP_PROXY_IP, JAMBONES_HTTP_PROXY_PORT, JAMBONES_TTS_CACHE_DURATION_MINS, + JAMBONES_EAGERLY_PRE_CACHE_AUDIO, TMP_FOLDER, HTTP_TIMEOUT }; diff --git a/lib/synth-audio.js b/lib/synth-audio.js index 0e3b274..d916af9 100644 --- a/lib/synth-audio.js +++ b/lib/synth-audio.js @@ -44,6 +44,7 @@ const { JAMBONES_HTTP_PROXY_IP, JAMBONES_HTTP_PROXY_PORT, JAMBONES_TTS_CACHE_DURATION_MINS, + JAMBONES_EAGERLY_PRE_CACHE_AUDIO, } = require('./config'); const EXPIRES = JAMBONES_TTS_CACHE_DURATION_MINS; const OpenAI = require('openai'); @@ -158,16 +159,6 @@ async function synthAudio(client, createHash, retrieveHash, logger, stats, { acc } - const preCachekey = makeSynthKey({ - account_sid, - vendor, - language: language || '', - voice: voice || deploymentId, - engine, - text, - renderForCaching: true - }); - const key = makeSynthKey({ account_sid, vendor, @@ -182,14 +173,22 @@ async function synthAudio(client, createHash, retrieveHash, logger, stats, { acc debug(`synth key is ${key}`); let cached; if (!disableTtsCache) { - // 1. check if precache audio - // 2. if there is no precache, check key - cached = await client.get(preCachekey); - if (cached) { - // Precache audio is available update filpath with precache file extension. - filePath = makeFilePath({vendor, key, salt, renderForCaching: true}); - } else { - cached = await client.get(key); + cached = await client.get(key); + if (!cached && JAMBONES_EAGERLY_PRE_CACHE_AUDIO) { + const preCachekey = makeSynthKey({ + account_sid, + vendor, + language: language || '', + voice: voice || deploymentId, + engine, + text, + renderForCaching: true + }); + cached = await client.get(preCachekey); + if (cached) { + // Precache audio is available update filpath with precache file extension. + filePath = makeFilePath({vendor, key, salt, renderForCaching: true}); + } } } if (cached) {