Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix - #570 - hit indication always true #579

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Changelog
=========

- fix ``response_hit_indication`` return True always #570.


Version 2.3.0
-------------
Expand Down
20 changes: 13 additions & 7 deletions src/flask_caching/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tests/test_memoize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 17 additions & 1 deletion tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading