@@ -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 )
0 commit comments