diff --git a/src/flask_caching/__init__.py b/src/flask_caching/__init__.py index 1dd424f..73b1620 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,13 +415,15 @@ 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.get("flask_caching_hit_cache"): + 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)