fix(cache): implement query-string-based cache busting

Update the `make_cache_key` function to include the request query string
in the generated key. This allows for manual cache busting by appending
version parameters to redirects.

In the `delete_account` route, replace manual cache deletion logic with
a redirect containing a timestamped nonce. This ensures that the user's
account list and home page views are refreshed without requiring explicit
knowledge of all internal cache keys.
This commit is contained in:
Karl 2026-04-06 17:24:55 +01:00
parent 780b7f9287
commit 608c57f719

16
app.py
View File

@ -74,6 +74,9 @@ def make_cache_key(*args, **kwargs):
"""Generate a cache key based on the user's session and request path.""" """Generate a cache key based on the user's session and request path."""
username = session.get('username', 'anonymous') username = session.get('username', 'anonymous')
path = request.path path = request.path
# Include query string in cache key to bust cache with version parameter
if request.query_string:
return f"view/{username}/{path}?{request.query_string.decode()}"
return f"view/{username}/{path}" return f"view/{username}/{path}"
@app.before_request @app.before_request
@ -298,18 +301,13 @@ def delete_account() -> Response:
account_id = request.form.get("id") account_id = request.form.get("id")
base_url = app.config["BACKEND_URL"] base_url = app.config["BACKEND_URL"]
delete_user_account(base_url, session["auth_credentials"], account_id) delete_user_account(base_url, session["auth_credentials"], account_id)
# Clear cache for user accounts route
cache_key = f"view/{session['username']}/accounts"
cache.delete(cache_key)
# Also clear memoized version for good measure
cache.delete_memoized(user_accounts, key_prefix=make_cache_key)
# Clear home page cache as well since it shows account stats
cache_key_home = f"view/{session['username']}/home"
cache.delete(cache_key_home)
# Run the NPM config update in a background thread to remove the deleted account's redirect # Run the NPM config update in a background thread to remove the deleted account's redirect
thread = threading.Thread(target=_update_npm_config_in_background, args=(session["auth_credentials"],)) thread = threading.Thread(target=_update_npm_config_in_background, args=(session["auth_credentials"],))
thread.start() thread.start()
return redirect(url_for("user_accounts")) # Redirect with a version nonce to bust all caches
import time
nonce = int(time.time())
return redirect(f"{url_for('user_accounts')}?_v={nonce}")
@app.route("/validateAccount", methods=["POST"]) @app.route("/validateAccount", methods=["POST"])
def validate_account() -> Tuple[Response, int]: def validate_account() -> Tuple[Response, int]: