fix cache issue
This commit is contained in:
parent
b81d0ae81c
commit
e4f92a1db2
23
app.py
23
app.py
@ -30,7 +30,10 @@ if os.environ.get("FLASK_ENV") == "production":
|
||||
else:
|
||||
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:
|
||||
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
||||
@ -60,6 +63,12 @@ def inject_version() -> Dict[str, str]:
|
||||
"""Injects the version into all templates."""
|
||||
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
|
||||
def make_session_permanent() -> None:
|
||||
"""Makes the user session permanent."""
|
||||
@ -140,7 +149,7 @@ def send_test_notification_proxy():
|
||||
return jsonify({"error": str(e)}), 502
|
||||
|
||||
@app.route("/home")
|
||||
@cache.cached(timeout=60)
|
||||
@cache.cached(timeout=60, key_prefix=make_cache_key)
|
||||
def home() -> str:
|
||||
"""Renders the home page with account statistics."""
|
||||
if session.get("logged_in"):
|
||||
@ -186,7 +195,7 @@ def login() -> Union[Response, str]:
|
||||
return render_template("index.html", error=error)
|
||||
|
||||
@app.route("/urls", methods=["GET"])
|
||||
@cache.cached(timeout=300)
|
||||
@cache.cached(timeout=300, key_prefix=make_cache_key)
|
||||
def urls() -> Union[Response, str]:
|
||||
"""Renders the URLs page."""
|
||||
if not session.get("logged_in"):
|
||||
@ -197,7 +206,7 @@ def urls() -> Union[Response, str]:
|
||||
)
|
||||
|
||||
@app.route("/accounts", methods=["GET"])
|
||||
@cache.cached(timeout=60)
|
||||
@cache.cached(timeout=60, key_prefix=make_cache_key)
|
||||
def user_accounts() -> Union[Response, str]:
|
||||
"""Renders the user accounts page."""
|
||||
if not session.get("logged_in"):
|
||||
@ -234,7 +243,7 @@ def add_account() -> Union[Response, str]:
|
||||
if add_user_account(
|
||||
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 render_template(
|
||||
@ -251,7 +260,7 @@ def delete_account() -> Response:
|
||||
username = request.form.get("username")
|
||||
base_url = app.config["BASE_URL"]
|
||||
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"))
|
||||
|
||||
@app.route("/validateAccount", methods=["POST"])
|
||||
@ -271,7 +280,7 @@ def validate_account() -> Tuple[Response, int]:
|
||||
response.raise_for_status()
|
||||
response_data = response.json()
|
||||
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
|
||||
except requests.exceptions.RequestException as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
BIN
requirements.txt
BIN
requirements.txt
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user