Fix: Descartar y eliminar del caché valores contaminados con mensajes de error (#12)
This commit is contained in:
@@ -129,10 +129,10 @@ async def translate_text(text: str, target_lang: str) -> str:
|
|||||||
translated_segments = await asyncio.gather(*tasks)
|
translated_segments = await asyncio.gather(*tasks)
|
||||||
result = "".join(translated_segments)
|
result = "".join(translated_segments)
|
||||||
|
|
||||||
# Guardar en Redis por 24 horas
|
# No cachear mensajes de error
|
||||||
if result != text:
|
if result and result != text and "mantenimiento" not in result:
|
||||||
cache_set(redis_key, result, ttl=86400)
|
cache_set(redis_key, result, ttl=86400)
|
||||||
return result
|
return result if result else text
|
||||||
except Exception:
|
except Exception:
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|||||||
@@ -51,20 +51,28 @@ def translate_filter(text, lang="es"):
|
|||||||
if not text: return ""
|
if not text: return ""
|
||||||
|
|
||||||
cache_key = f"ui:{lang}:{text}"
|
cache_key = f"ui:{lang}:{text}"
|
||||||
|
_BAD = "mantenimiento" # Patron de respuesta de error a ignorar
|
||||||
|
|
||||||
# 1. Redis (compartido y persistente entre reinicios)
|
# 1. Redis (compartido y persistente entre reinicios)
|
||||||
redis_val = cache_get(cache_key)
|
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
|
_ui_memory_cache[cache_key] = redis_val
|
||||||
return 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)
|
# 2. RAM local (ultra rápida, temporal)
|
||||||
if cache_key in _ui_memory_cache:
|
ram_val = _ui_memory_cache.get(cache_key)
|
||||||
return _ui_memory_cache[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
|
# 3. Base de datos MySQL
|
||||||
cached = get_ui_translation(text, lang)
|
cached = get_ui_translation(text, lang)
|
||||||
if cached:
|
if cached and _BAD not in cached:
|
||||||
_ui_memory_cache[cache_key] = cached
|
_ui_memory_cache[cache_key] = cached
|
||||||
cache_set(cache_key, cached, ttl=604800) # 7 días
|
cache_set(cache_key, cached, ttl=604800) # 7 días
|
||||||
return cached
|
return cached
|
||||||
@@ -73,12 +81,13 @@ def translate_filter(text, lang="es"):
|
|||||||
from botdiscord.translate import translate_text_sync
|
from botdiscord.translate import translate_text_sync
|
||||||
translated = translate_text_sync(text, lang)
|
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)
|
save_ui_translation(text, lang, translated)
|
||||||
_ui_memory_cache[cache_key] = translated
|
_ui_memory_cache[cache_key] = translated
|
||||||
cache_set(cache_key, translated, ttl=604800) # 7 días
|
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
|
templates.env.filters["translate"] = translate_filter
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user