From 258bd0afd2fb3382c3fdc744a402dac58a9e86fe Mon Sep 17 00:00:00 2001 From: Peter Justin Date: Sun, 24 Nov 2019 17:31:29 +0100 Subject: [PATCH] Implement response_filter for memoized functions Fixes #107 --- flask_caching/__init__.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/flask_caching/__init__.py b/flask_caching/__init__.py index c0476a79..28dae0b2 100644 --- a/flask_caching/__init__.py +++ b/flask_caching/__init__.py @@ -708,6 +708,7 @@ def memoize( make_name=None, unless=None, forced_update=None, + response_filter=None, hash_method=hashlib.md5, cache_none=False, ): @@ -765,6 +766,12 @@ def big_foo(a, b): cache value will be updated regardless cache is expired or not. Useful for background renewal of cached functions. + :param response_filter: Default None. If not None, the callable is + invoked after the cached funtion evaluation, + and is given one arguement, the response + content. If the callable returns False, the + content will not be cached. Useful to prevent + caching of code 500 responses. :param hash_method: Default hashlib.md5. The hash method used to generate the keys for cached results. :param cache_none: Default False. If set to True, add a key exists @@ -825,18 +832,20 @@ def decorated_function(*args, **kwargs): if not found: rv = f(*args, **kwargs) - try: - self.cache.set( - cache_key, - rv, - timeout=decorated_function.cache_timeout, - ) - except Exception: - if self.app.debug: - raise - logger.exception( - "Exception possibly due to cache backend." - ) + + if response_filter is None or response_filter(rv): + try: + self.cache.set( + cache_key, + rv, + timeout=decorated_function.cache_timeout, + ) + except Exception: + if self.app.debug: + raise + logger.exception( + "Exception possibly due to cache backend." + ) return rv decorated_function.uncached = f