fix cache issue

This commit is contained in:
Karl 2025-07-18 07:37:20 +01:00
parent b81d0ae81c
commit e4f92a1db2
2 changed files with 16 additions and 7 deletions

23
app.py
View File

@ -30,7 +30,10 @@ if os.environ.get("FLASK_ENV") == "production":
else: else:
app.config.from_object(DevelopmentConfig) app.config.from_object(DevelopmentConfig)
cache = Cache(app, config={"CACHE_TYPE": "SimpleCache"}) cache = Cache(app, config={
"CACHE_TYPE": "redis",
"CACHE_REDIS_URL": app.config.get("REDIS_URL", "redis://localhost:6379/0")
})
if app.config.get("OCR_ENABLED") and OCR_AVAILABLE: if app.config.get("OCR_ENABLED") and OCR_AVAILABLE:
ocr = PaddleOCR(use_angle_cls=True, lang='en') ocr = PaddleOCR(use_angle_cls=True, lang='en')
@ -60,6 +63,12 @@ def inject_version() -> Dict[str, str]:
"""Injects the version into all templates.""" """Injects the version into all templates."""
return dict(version=get_version(), config=app.config, session=session) return dict(version=get_version(), config=app.config, session=session)
def make_cache_key(*args, **kwargs):
"""Generate a cache key based on the user's session and request path."""
username = session.get('username', 'anonymous')
path = request.path
return f"view/{username}/{path}"
@app.before_request @app.before_request
def make_session_permanent() -> None: def make_session_permanent() -> None:
"""Makes the user session permanent.""" """Makes the user session permanent."""
@ -140,7 +149,7 @@ def send_test_notification_proxy():
return jsonify({"error": str(e)}), 502 return jsonify({"error": str(e)}), 502
@app.route("/home") @app.route("/home")
@cache.cached(timeout=60) @cache.cached(timeout=60, key_prefix=make_cache_key)
def home() -> str: def home() -> str:
"""Renders the home page with account statistics.""" """Renders the home page with account statistics."""
if session.get("logged_in"): if session.get("logged_in"):
@ -186,7 +195,7 @@ def login() -> Union[Response, str]:
return render_template("index.html", error=error) return render_template("index.html", error=error)
@app.route("/urls", methods=["GET"]) @app.route("/urls", methods=["GET"])
@cache.cached(timeout=300) @cache.cached(timeout=300, key_prefix=make_cache_key)
def urls() -> Union[Response, str]: def urls() -> Union[Response, str]:
"""Renders the URLs page.""" """Renders the URLs page."""
if not session.get("logged_in"): if not session.get("logged_in"):
@ -197,7 +206,7 @@ def urls() -> Union[Response, str]:
) )
@app.route("/accounts", methods=["GET"]) @app.route("/accounts", methods=["GET"])
@cache.cached(timeout=60) @cache.cached(timeout=60, key_prefix=make_cache_key)
def user_accounts() -> Union[Response, str]: def user_accounts() -> Union[Response, str]:
"""Renders the user accounts page.""" """Renders the user accounts page."""
if not session.get("logged_in"): if not session.get("logged_in"):
@ -234,7 +243,7 @@ def add_account() -> Union[Response, str]:
if add_user_account( if add_user_account(
base_url, session["auth_credentials"], username, password, stream base_url, session["auth_credentials"], username, password, stream
): ):
cache.delete_memoized(user_accounts) cache.delete_memoized(user_accounts, key_prefix=make_cache_key)
return redirect(url_for("user_accounts")) return redirect(url_for("user_accounts"))
return render_template( return render_template(
@ -251,7 +260,7 @@ def delete_account() -> Response:
username = request.form.get("username") username = request.form.get("username")
base_url = app.config["BASE_URL"] base_url = app.config["BASE_URL"]
delete_user_account(base_url, session["auth_credentials"], stream, username) delete_user_account(base_url, session["auth_credentials"], stream, username)
cache.delete_memoized(user_accounts) cache.delete_memoized(user_accounts, key_prefix=make_cache_key)
return redirect(url_for("user_accounts")) return redirect(url_for("user_accounts"))
@app.route("/validateAccount", methods=["POST"]) @app.route("/validateAccount", methods=["POST"])
@ -271,7 +280,7 @@ def validate_account() -> Tuple[Response, int]:
response.raise_for_status() response.raise_for_status()
response_data = response.json() response_data = response.json()
if response_data.get("message") == "Account is valid and updated": if response_data.get("message") == "Account is valid and updated":
cache.delete_memoized(user_accounts) cache.delete_memoized(user_accounts, key_prefix=make_cache_key)
return jsonify(response_data), response.status_code return jsonify(response_data), response.status_code
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
return jsonify({"error": str(e)}), 500 return jsonify({"error": str(e)}), 500

Binary file not shown.