From 410a81ca6b0841f3e2ad3708fa04c4af928301b0 Mon Sep 17 00:00:00 2001 From: Ido Shraga Date: Sun, 14 Jul 2024 15:53:17 +0300 Subject: [PATCH 1/3] tester fix --- src/flask_caching/__init__.py | 16 ++++++++++------ tests/test_view.py | 18 +++++++++++++++++- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/flask_caching/__init__.py b/src/flask_caching/__init__.py index 1dd424f..4bcd1f5 100644 --- a/src/flask_caching/__init__.py +++ b/src/flask_caching/__init__.py @@ -25,6 +25,7 @@ from typing import Union from flask import current_app +from flask import g from flask import Flask from flask import request from flask import Response @@ -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,8 @@ 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 +423,8 @@ 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_cashing_found = found - def apply_caching(response): - if found: - response.headers["hit_cache"] = found - return response - - self.app.after_request_funcs[None].append(apply_caching) if not found: rv = self._call_fn(f, *args, **kwargs) diff --git a/tests/test_view.py b/tests/test_view.py index b876403..3836aa7 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 \ No newline at end of file From 3181510a1621c112e88e999329731143abc275c3 Mon Sep 17 00:00:00 2001 From: Ido Shraga Date: Sun, 14 Jul 2024 15:56:04 +0300 Subject: [PATCH 2/3] doc --- CHANGES.rst | 2 ++ 1 file changed, 2 insertions(+) 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 ------------- From 158fba56b1917a053858f79a869a0493fec63932 Mon Sep 17 00:00:00 2001 From: Ido Shraga Date: Sun, 14 Jul 2024 15:57:47 +0300 Subject: [PATCH 3/3] pre commit --- src/flask_caching/__init__.py | 12 +++++++----- tests/test_memoize.py | 1 + tests/test_view.py | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/flask_caching/__init__.py b/src/flask_caching/__init__.py index 4bcd1f5..2c63c26 100644 --- a/src/flask_caching/__init__.py +++ b/src/flask_caching/__init__.py @@ -25,8 +25,8 @@ from typing import Union from flask import current_app -from flask import g from flask import Flask +from flask import g from flask import request from flask import Response from flask import url_for @@ -69,8 +69,8 @@ def __init__(self, response, timeout): def apply_caching(response): - if g.get('flask_cashing_found'): - response.headers["hit_cache"] = g.get('flask_cashing_found') + if g.get("flask_cashing_found"): + response.headers["hit_cache"] = g.get("flask_cashing_found") return response @@ -364,7 +364,10 @@ 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]: + 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): @@ -425,7 +428,6 @@ def decorated_function(*args, **kwargs): if response_hit_indication: g.flask_cashing_found = found - if not found: rv = self._call_fn(f, *args, **kwargs) if inspect.isgenerator(rv): 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 3836aa7..afd5441 100644 --- a/tests/test_view.py +++ b/tests/test_view.py @@ -602,4 +602,4 @@ def indication_false_cached_view(): 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 \ No newline at end of file + assert tc.get("/indication-false").headers.get("hit_cache") is None