Skip to content

Commit 59cf9af

Browse files
idoshridocyabranorthernSage
authored
Hit cache indication (#532)
* endpoint_params * response_hit_indication * revert endpoint_params * unrevert endpoint_params * revert * tester change log --------- Co-authored-by: Ido Shraga <[email protected]> Co-authored-by: northernSage <[email protected]>
1 parent c1b683c commit 59cf9af

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

CHANGES.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Changelog
22
=========
33

4+
5+
Version 2.3.0
6+
-------------
7+
8+
- add indication if cache is used in the response header.
9+
- Added ``response_hit_indication`` flag to ``Cache.cached`` decorator for appending 'hit_cache' headers to responses, indicating cache hits.
10+
11+
412
Version 2.2.0
513
-------------
614

@@ -18,6 +26,7 @@ Released 2024-10-08
1826
- Added docs and example for make_cache_key
1927
- support Flask 3
2028

29+
2130
Version 2.0.2
2231
-------------
2332

src/flask_caching/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ def cached(
251251
cache_none: bool = False,
252252
make_cache_key: Optional[Callable] = None,
253253
source_check: Optional[bool] = None,
254+
response_hit_indication: Optional[bool] = False,
254255
) -> Callable:
255256
"""Decorator. Use this to cache a function. By default the cache key
256257
is `view/request.path`. You are able to use this decorator with any
@@ -351,6 +352,10 @@ def get_list():
351352
formed with the function's source code hash in
352353
addition to other parameters that may be included
353354
in the formation of the key.
355+
356+
:param response_hit_indication: Default False.
357+
If True, it will add to response header field 'hit_cache'
358+
if used cache.
354359
"""
355360

356361
def decorator(f):
@@ -406,6 +411,16 @@ def decorated_function(*args, **kwargs):
406411
raise
407412
logger.exception("Exception possibly due to cache backend.")
408413
return self._call_fn(f, *args, **kwargs)
414+
if found and self.app.debug:
415+
logger.info(f"Cache used for key: {cache_key}")
416+
if response_hit_indication:
417+
418+
def apply_caching(response):
419+
if found:
420+
response.headers["hit_cache"] = found
421+
return response
422+
423+
self.app.after_request_funcs[None].append(apply_caching)
409424

410425
if not found:
411426
rv = self._call_fn(f, *args, **kwargs)

tests/test_view.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,17 @@ def view_works():
573573
# Now make sure the time for the first and third responses are the same
574574
# i.e. cached is used since cache will not check for source changes!
575575
assert third_time == first_time
576+
577+
578+
def test_hit_cache(app, cache):
579+
@app.route("/")
580+
@cache.cached(10, response_hit_indication=True)
581+
def cached_view():
582+
# This should override the timeout to be 2 seconds
583+
return {"data": "data"}
584+
585+
tc = app.test_client()
586+
587+
assert tc.get("/").headers.get("hit_cache") is None
588+
assert tc.get("/").headers.get("hit_cache") == "True"
589+
assert tc.get("/").headers.get("hit_cache") == "True"

0 commit comments

Comments
 (0)