Initial commit

This commit is contained in:
HalbeBruno
2026-02-18 10:18:46 -03:00
commit b264b583b8
24 changed files with 2338 additions and 0 deletions

27
utils/cache.py Normal file
View File

@@ -0,0 +1,27 @@
from cachetools import TTLCache, cached
from config import Config
from flask import request
# Criação do cache com TTL (Time To Live)
# O tamanho máximo e o tempo de vida são configurados via config.py
ttl_cache = TTLCache(maxsize=Config.CACHE_MAX_SIZE, ttl=Config.CACHE_TTL)
def get_cache_key(*args, **kwargs):
"""
Gera uma chave única para o cache baseada na URL completa da requisição.
Isso garante que query parameters diferentes (host, driver) gerem entradas diferentes.
"""
if request:
return request.url
return str(args) + str(kwargs)
def cache_response(func):
"""
Decorador wrapper para aplicar cache nas chamadas de função.
A chave do cache será baseada na URL da request.
"""
# A função wrapper precisa aceitar args/kwargs, mas a chave é gerada por get_cache_key
@cached(cache=ttl_cache, key=get_cache_key)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper