mirror of
https://github.com/jambonz/speech-utils.git
synced 2025-12-19 03:37:49 +00:00
feat: add account_sid to tts hash key and confiurable TTL for cached file
This commit is contained in:
@@ -11,7 +11,8 @@ const debug = require('debug')('jambonz:realtimedb-helpers');
|
||||
* @param {string} opts.text - text or ssml to synthesize
|
||||
* @returns {object} result - {error, purgedCount}
|
||||
*/
|
||||
async function purgeTtsCache(client, logger, {all, vendor, language, voice, deploymentId, engine, text} = {all: true}) {
|
||||
async function purgeTtsCache(client, logger, {all, account_sid, vendor,
|
||||
language, voice, deploymentId, engine, text} = {all: true}) {
|
||||
logger = logger || noopLogger;
|
||||
|
||||
let purgedCount = 0, error;
|
||||
@@ -21,8 +22,13 @@ async function purgeTtsCache(client, logger, {all, vendor, language, voice, depl
|
||||
const keys = await client.keysAsync('tts:*');
|
||||
purgedCount = await client.delAsync(keys);
|
||||
|
||||
} else {
|
||||
} else if (account_sid && !vendor && !language && !voice && !engine && !text) {
|
||||
const keys = await client.keysAsync(`tts:${account_sid}:*`);
|
||||
purgedCount = await client.delAsync(keys);
|
||||
}
|
||||
else {
|
||||
const key = makeSynthKey({
|
||||
account_sid,
|
||||
vendor,
|
||||
language: language || '',
|
||||
voice: voice || deploymentId,
|
||||
|
||||
@@ -32,7 +32,7 @@ const {
|
||||
const {SynthesizeSpeechRequest} = require('../stubs/riva/proto/riva_tts_pb');
|
||||
const {AudioEncoding} = require('../stubs/riva/proto/riva_audio_pb');
|
||||
const debug = require('debug')('jambonz:realtimedb-helpers');
|
||||
const EXPIRES = 3600 * 24; // cache tts for 24 hours
|
||||
const EXPIRES = process.env.JAMBONES_TTS_CACHE_DURATION_MINS || 3600 * 24; // cache tts for 24 hours
|
||||
const TMP_FOLDER = '/tmp';
|
||||
|
||||
/**
|
||||
@@ -95,8 +95,9 @@ async function synthAudio(client, logger, stats, {
|
||||
} else if (vendor.startsWith('custom')) {
|
||||
assert.ok(credentials.custom_tts_url, `synthAudio requires custom_tts_url in credentials when ${vendor} is used`);
|
||||
}
|
||||
|
||||
const account_sid = credentials.account_sid;
|
||||
const key = makeSynthKey({
|
||||
account_sid,
|
||||
vendor,
|
||||
language: language || '',
|
||||
voice: voice || deploymentId,
|
||||
|
||||
@@ -16,10 +16,10 @@ const debug = require('debug')('jambonz:realtimedb-helpers');
|
||||
*/
|
||||
//const nuanceClientMap = new Map();
|
||||
|
||||
function makeSynthKey({vendor, language, voice, engine = '', text}) {
|
||||
function makeSynthKey({account_sid = '', vendor, language, voice, engine = '', text}) {
|
||||
const hash = crypto.createHash('sha1');
|
||||
hash.update(`${language}:${vendor}:${voice}:${engine}:${text}`);
|
||||
return `tts:${hash.digest('hex')}`;
|
||||
return `tts${account_sid ? (':' + account_sid) : ''}:${hash.digest('hex')}`;
|
||||
}
|
||||
|
||||
const noopLogger = {
|
||||
|
||||
@@ -427,5 +427,35 @@ test('TTS Cache tests', async(t) => {
|
||||
t.end(err);
|
||||
}
|
||||
|
||||
try {
|
||||
// clear cache
|
||||
await purgeTtsCache();
|
||||
// save some random tts keys to cache
|
||||
const minRecords = 8;
|
||||
const account_sid = "12412512_cabc_5aff"
|
||||
const account_sid2 = "22412512_cabc_5aff"
|
||||
for (const i in Array(minRecords).fill(0)) {
|
||||
await client.setAsync(makeSynthKey({account_sid, vendor: i, language: i, voice: i, engine: i, text: i}), i);
|
||||
}
|
||||
for (const i in Array(minRecords).fill(0)) {
|
||||
await client.setAsync(makeSynthKey({account_sid: account_sid2, vendor: i, language: i, voice: i, engine: i, text: i}), i);
|
||||
}
|
||||
const {purgedCount} = await purgeTtsCache({account_sid});
|
||||
t.equal(purgedCount, minRecords, `successfully purged at least ${minRecords} tts records from cache for account_sid:${account_sid}`);
|
||||
|
||||
let cached = (await client.keysAsync('tts:*')).length;
|
||||
t.equal(cached, minRecords, `successfully purged all tts records from cache for account_sid:${account_sid}`);
|
||||
|
||||
const {purgedCount: purgedCount2} = await purgeTtsCache({account_sid: account_sid2});
|
||||
t.equal(purgedCount2, minRecords, `successfully purged at least ${minRecords} tts records from cache for account_sid:${account_sid2}`);
|
||||
|
||||
cached = (await client.keysAsync('tts:*')).length;
|
||||
t.equal(cached, 0, `successfully purged all tts records from cache`);
|
||||
|
||||
} catch (err) {
|
||||
console.error(JSON.stringify(err));
|
||||
t.end(err);
|
||||
}
|
||||
|
||||
client.quit();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user