diff --git a/CHANGES.rst b/CHANGES.rst index dfe52a5..4b509d0 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,8 @@ Changelog ========= +- fix ``response_hit_indication`` return True always #570. + Version 2.3.0 ------------- diff --git a/src/flask_caching/__init__.py b/src/flask_caching/__init__.py index 1dd424f..2c63c26 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 @@ -67,6 +68,12 @@ def __init__(self, response, timeout): self.timeout = timeout +def apply_caching(response): + if g.get("flask_cashing_found"): + response.headers["hit_cache"] = g.get("flask_cashing_found") + return response + + class Cache: """This class is used to control the cache objects.""" @@ -357,6 +364,11 @@ def get_list(): If True, it will add to response header field 'hit_cache' if used cache. """ + if ( + response_hit_indication + and apply_caching not in self.app.after_request_funcs[None] + ): + self.app.after_request_funcs[None].append(apply_caching) def decorator(f): @functools.wraps(f) @@ -414,13 +426,7 @@ def decorated_function(*args, **kwargs): if found and self.app.debug: logger.info(f"Cache used for key: {cache_key}") if response_hit_indication: - - def apply_caching(response): - if found: - response.headers["hit_cache"] = found - return response - - self.app.after_request_funcs[None].append(apply_caching) + g.flask_cashing_found = found if not found: rv = self._call_fn(f, *args, **kwargs) diff --git a/tests/test_memoize.py b/tests/test_memoize.py index 9e2552f..a4ab74b 100644 --- a/tests/test_memoize.py +++ b/tests/test_memoize.py @@ -838,6 +838,7 @@ def big_foo(self, a, b): def test_memoize_function_ignore_kwarg(app, cache): with app.test_request_context(): + @cache.memoize(50, args_to_ignore=["b"]) def big_foo(a, b): return a + b + random.randrange(0, 100000) diff --git a/tests/test_view.py b/tests/test_view.py index b876403..afd5441 100644 --- a/tests/test_view.py +++ b/tests/test_view.py @@ -577,13 +577,29 @@ def view_works(): def test_hit_cache(app, cache): @app.route("/") - @cache.cached(10, response_hit_indication=True) + @cache.cached(2, response_hit_indication=True) def cached_view(): # This should override the timeout to be 2 seconds return {"data": "data"} + @app.route("/indication-false") + @cache.cached(2, response_hit_indication=False) + def indication_false_cached_view(): + # This should override the timeout to be 2 seconds + return {"data": "data"} + tc = app.test_client() assert tc.get("/").headers.get("hit_cache") is None assert tc.get("/").headers.get("hit_cache") == "True" assert tc.get("/").headers.get("hit_cache") == "True" + time.sleep(2) + assert tc.get("/").headers.get("hit_cache") is None + + # indication-false + + assert tc.get("/indication-false").headers.get("hit_cache") is None + assert tc.get("/indication-false").headers.get("hit_cache") is None + assert tc.get("/indication-false").headers.get("hit_cache") is None + time.sleep(2) + assert tc.get("/indication-false").headers.get("hit_cache") is None