Fix: Descartar y eliminar del caché valores contaminados con mensajes de error (#12)

This commit is contained in:
2026-03-20 18:57:24 -06:00
parent e1adb5c390
commit 6ff79f5d69
2 changed files with 18 additions and 9 deletions

View File

@@ -129,10 +129,10 @@ async def translate_text(text: str, target_lang: str) -> str:
translated_segments = await asyncio.gather(*tasks)
result = "".join(translated_segments)
# Guardar en Redis por 24 horas
if result != text:
# No cachear mensajes de error
if result and result != text and "mantenimiento" not in result:
cache_set(redis_key, result, ttl=86400)
return result
return result if result else text
except Exception:
return text

View File

@@ -51,20 +51,28 @@ def translate_filter(text, lang="es"):
if not text: return ""
cache_key = f"ui:{lang}:{text}"
_BAD = "mantenimiento" # Patron de respuesta de error a ignorar
# 1. Redis (compartido y persistente entre reinicios)
redis_val = cache_get(cache_key)
if redis_val:
if redis_val and _BAD not in redis_val:
_ui_memory_cache[cache_key] = redis_val
return redis_val
elif redis_val and _BAD in redis_val:
# Valor contaminado: eliminarlo del caché
from utils.cache import cache_delete
cache_delete(cache_key)
# 2. RAM local (ultra rápida, temporal)
if cache_key in _ui_memory_cache:
return _ui_memory_cache[cache_key]
ram_val = _ui_memory_cache.get(cache_key)
if ram_val and _BAD not in ram_val:
return ram_val
elif cache_key in _ui_memory_cache:
del _ui_memory_cache[cache_key]
# 3. Base de datos MySQL
cached = get_ui_translation(text, lang)
if cached:
if cached and _BAD not in cached:
_ui_memory_cache[cache_key] = cached
cache_set(cache_key, cached, ttl=604800) # 7 días
return cached
@@ -73,12 +81,13 @@ def translate_filter(text, lang="es"):
from botdiscord.translate import translate_text_sync
translated = translate_text_sync(text, lang)
if translated and translated != text:
if translated and translated != text and _BAD not in translated:
save_ui_translation(text, lang, translated)
_ui_memory_cache[cache_key] = translated
cache_set(cache_key, translated, ttl=604800) # 7 días
return translated
return translated
return text # Fallback: devolver el texto original si todo falla
templates.env.filters["translate"] = translate_filter