redis fix

This commit is contained in:
Karl 2025-07-18 08:49:26 +01:00
parent 9e63cbc2f0
commit 8c982666d6

23
app.py
View File

@ -6,6 +6,8 @@ import requests.auth
import os
import base64
from typing import Dict, Any, Tuple, Union
import sys
import redis
from lib.datetime import filter_accounts_next_30_days, filter_accounts_expired
from lib.reqs import (get_urls, get_user_accounts, add_user_account,
@ -30,10 +32,22 @@ if os.environ.get("FLASK_ENV") == "production":
else:
app.config.from_object(DevelopmentConfig)
cache = Cache(app, config={
"CACHE_TYPE": "redis",
"CACHE_REDIS_URL": app.config.get("REDIS_URL", "redis://localhost:6379/0")
})
# Check for Redis availability and configure cache
redis_url = app.config.get("REDIS_URL", "redis://localhost:6379/0")
cache_config = {"CACHE_TYPE": "redis", "CACHE_REDIS_URL": redis_url}
try:
# Use a short timeout to prevent hanging
r = redis.from_url(redis_url, socket_connect_timeout=1)
r.ping()
except redis.exceptions.ConnectionError as e:
print(
f"WARNING: Redis connection failed: {e}. Falling back to SimpleCache. "
"This is not recommended for production with multiple workers.",
file=sys.stderr,
)
cache_config = {"CACHE_TYPE": "SimpleCache"}
cache = Cache(app, config=cache_config)
if app.config.get("OCR_ENABLED") and OCR_AVAILABLE:
ocr = PaddleOCR(use_angle_cls=True, lang='en')
@ -44,7 +58,6 @@ app.config["SESSION_COOKIE_SECURE"] = not app.config["DEBUG"]
app.config['SESSION_COOKIE_HTTPONLY'] = True
app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'
app.config['PERMANENT_SESSION_LIFETIME'] = 60 * 60 * 24 * 365 # 1 year
cache.clear()
def get_version() -> str:
"""Retrieves the application version from the VERSION file.