From 0ac88bcd8cca063a571fc385ffbe9bcc8acaa690 Mon Sep 17 00:00:00 2001 From: Francis Charette Migneault Date: Tue, 7 Dec 2021 14:23:25 -0500 Subject: [PATCH] workaround until official implementation of ignore args available (https://github.com/bbangert/beaker/issues/201) --- beaker/cache.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/beaker/cache.py b/beaker/cache.py index 5a1ad6a..b344416 100644 --- a/beaker/cache.py +++ b/beaker/cache.py @@ -130,7 +130,7 @@ def _init(self): }) -def cache_region(region, *args): +def cache_region(region, *args, ignore_args=None): """Decorate a function such that its return result is cached, using a "region" to indicate the cache arguments. @@ -198,7 +198,7 @@ def load(cls, search_term, limit, offset): not included in the "key" used for caching. New in 1.6. """ - return _cache_decorate(args, None, None, region) + return _cache_decorate(args, None, None, region, ignore_args=ignore_args) def region_invalidate(namespace, region, *args): @@ -542,10 +542,11 @@ def load(search_term, limit, offset): _cache_decorator_invalidate(cache, key_length, args) -def _cache_decorate(deco_args, manager, options, region): +def _cache_decorate(deco_args, manager, options, region, ignore_args=None): """Return a caching function decorator.""" cache = [None] + ignore_args = [ignore_args] def decorate(func): namespace = util.func_namespace(func) @@ -579,6 +580,14 @@ def cached(*args, **kwargs): cache_key_args = args if skip_self: cache_key_args = args[1:] + if ignore_args and ignore_args[0]: + cache_key_args_include = [] + arg_names = list(signature.parameters) + arg_names = arg_names[1:] if skip_self else arg_names + for name, value in zip(arg_names, cache_key_args): + if name not in ignore_args[0]: + cache_key_args_include.append(value) + cache_key_args = cache_key_args_include cache_key = u_(" ").join(map(u_, chain(deco_args, cache_key_args, cache_key_kwargs)))