From 601a579a3c3e5f278af13a821798c1cdb13b4726 Mon Sep 17 00:00:00 2001 From: Sumandari Date: Thu, 19 Dec 2024 16:20:24 +0100 Subject: [PATCH 1/3] get found from request context --- src/flask_caching/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/flask_caching/__init__.py b/src/flask_caching/__init__.py index 1dd424f..c07cd88 100644 --- a/src/flask_caching/__init__.py +++ b/src/flask_caching/__init__.py @@ -26,6 +26,7 @@ from flask import current_app from flask import Flask +from flask import g from flask import request from flask import Response from flask import url_for @@ -414,10 +415,11 @@ def decorated_function(*args, **kwargs): if found and self.app.debug: logger.info(f"Cache used for key: {cache_key}") if response_hit_indication: + g.flask_caching_hit_cache = found def apply_caching(response): - if found: - response.headers["hit_cache"] = found + if g.flask_caching_hit_cache: + response.headers["hit_cache"] = g.flask_caching_hit_cache return response self.app.after_request_funcs[None].append(apply_caching) From 08f32c3c4f3949d46766994e12ff1060d1e9adbb Mon Sep 17 00:00:00 2001 From: Sumandari Date: Thu, 2 Jan 2025 12:59:49 +0100 Subject: [PATCH 2/3] add after_request with condition --- src/flask_caching/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/flask_caching/__init__.py b/src/flask_caching/__init__.py index c07cd88..7b3eb6b 100644 --- a/src/flask_caching/__init__.py +++ b/src/flask_caching/__init__.py @@ -422,7 +422,8 @@ def apply_caching(response): response.headers["hit_cache"] = g.flask_caching_hit_cache return response - self.app.after_request_funcs[None].append(apply_caching) + if "apply_caching" not in self.app.after_request_funcs[None]: + self.app.after_request_funcs[None].append(apply_caching) if not found: rv = self._call_fn(f, *args, **kwargs) From 67f5bd19f0cb3d050daa8df1678e1a15fa8aabff Mon Sep 17 00:00:00 2001 From: Sumandari Date: Thu, 2 Jan 2025 13:04:42 +0100 Subject: [PATCH 3/3] fixed "g" to use getattr instead --- src/flask_caching/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flask_caching/__init__.py b/src/flask_caching/__init__.py index 7b3eb6b..73b1620 100644 --- a/src/flask_caching/__init__.py +++ b/src/flask_caching/__init__.py @@ -418,7 +418,7 @@ def decorated_function(*args, **kwargs): g.flask_caching_hit_cache = found def apply_caching(response): - if g.flask_caching_hit_cache: + if g.get("flask_caching_hit_cache"): response.headers["hit_cache"] = g.flask_caching_hit_cache return response