Add version-based cache invalidation for voice data

Users with old cached voice data need to see the new models (tts-1-silero,
tts-1-kokoro). Added a cache version system that automatically invalidates
old cache when we update the voice models.

Changes:
- Added cacheVersionKey to track cache format version
- Set currentVersion to '2' (increment when voice models change)
- Cache is invalidated if version doesn't match
- Old cache entries are cleaned up automatically
- Users will automatically fetch fresh voice data on next modal open
This commit is contained in:
Russell Ballestrini 2025-11-10 04:07:53 -05:00
parent 0db28f7984
commit f3d825278a

View file

@ -9,20 +9,30 @@ async function populateVoiceDropdown(voiceSelection) {
// Check cache first (1 minute expiration)
const cacheKey = 'uncloseai_voices_cache';
const cacheTimeKey = 'uncloseai_voices_cache_time';
const cacheVersionKey = 'uncloseai_voices_cache_version';
const cacheExpiration = 60000; // 1 minute in milliseconds
const currentVersion = '2'; // Increment this when voice models change
try {
const cachedTime = localStorage.getItem(cacheTimeKey);
const cachedVoices = localStorage.getItem(cacheKey);
const cachedVersion = localStorage.getItem(cacheVersionKey);
if (cachedTime && cachedVoices) {
// Check if cache is valid (not expired AND correct version)
if (cachedTime && cachedVoices && cachedVersion === currentVersion) {
const age = Date.now() - parseInt(cachedTime, 10);
if (age < cacheExpiration) {
console.log('Using cached voices data');
console.log('Using cached voices data (v' + currentVersion + ')');
const voices = JSON.parse(cachedVoices);
renderVoiceOptions(voiceSelection, voices);
return voices;
}
} else if (cachedVersion !== currentVersion) {
console.log('Cache version mismatch - invalidating old cache');
// Clear old cache
localStorage.removeItem(cacheKey);
localStorage.removeItem(cacheTimeKey);
localStorage.removeItem(cacheVersionKey);
}
} catch (error) {
console.warn('Cache read failed:', error);
@ -39,10 +49,11 @@ async function populateVoiceDropdown(voiceSelection) {
const data = await response.json();
// Cache the response
// Cache the response with version
try {
localStorage.setItem(cacheKey, JSON.stringify(data.voices));
localStorage.setItem(cacheTimeKey, Date.now().toString());
localStorage.setItem(cacheVersionKey, currentVersion);
} catch (error) {
console.warn('Cache write failed:', error);
}