V2 Pro: Logging rotativo, Redis cache, Health Check de LibreTranslate y Rate Limiting en botones (#7)

This commit is contained in:
2026-03-20 18:03:56 -06:00
parent 57d570ad31
commit 06da793709
13 changed files with 264 additions and 17 deletions

View File

@@ -35,10 +35,13 @@ SCRIPT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
templates = Jinja2Templates(directory=os.path.join(SCRIPT_DIR, "panel", "templates"))
# Caché de memoria para traducciones UI (evita 50 queries SQL por página)
from utils.logger import panel_logger as log
from utils.cache import cache_get, cache_set, cache_increment
# Caché de memoria RAM como fallback si Redis no está disponible
_ui_memory_cache = {}
# Filtro de traducción para Jinja2
# Filtro de traducción para Jinja2 (usa Redis → RAM → DB → LibreTranslate en cascada)
def translate_filter(text, lang="es"):
if lang == "es" or not text:
return text
@@ -47,24 +50,33 @@ def translate_filter(text, lang="es"):
text = _normalize_text(text)
if not text: return ""
# 1. Buscamos en caché de MEMORIA (RAM) - Ultra rápido
cache_key = f"{lang}:{text}"
cache_key = f"ui:{lang}:{text}"
# 1. Redis (compartido y persistente entre reinicios)
redis_val = cache_get(cache_key)
if redis_val:
_ui_memory_cache[cache_key] = redis_val
return redis_val
# 2. RAM local (ultra rápida, temporal)
if cache_key in _ui_memory_cache:
return _ui_memory_cache[cache_key]
# 2. Buscamos en caché de la base de datos (acceso rápido)
# 3. Base de datos MySQL
cached = get_ui_translation(text, lang)
if cached:
_ui_memory_cache[cache_key] = cached
cache_set(cache_key, cached, ttl=604800) # 7 días
return cached
# 3. Si no está en ningún caché, traducimos de forma síncrona
# 4. LibreTranslate (sincrónico, solo si no está en ningún caché)
from botdiscord.translate import translate_text_sync
translated = translate_text_sync(text, lang)
if translated and translated != text:
save_ui_translation(text, lang, translated)
_ui_memory_cache[cache_key] = translated
cache_set(cache_key, translated, ttl=604800) # 7 días
return translated