From 9f7e8977f5685fc0b7e1dc68f05059d81a372415 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Wed, 8 Jul 2015 00:45:36 +0200 Subject: [PATCH 1/3] decorators: allow cache decorators to vary on a subset of args or kwargs --- src/authentic2/decorators.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/authentic2/decorators.py b/src/authentic2/decorators.py index 20a52e8..3006520 100644 --- a/src/authentic2/decorators.py +++ b/src/authentic2/decorators.py @@ -119,9 +119,12 @@ class CacheDecoratorBase(object): return cls(**kwargs)(args[0]) return super(CacheDecoratorBase, cls).__new__(cls, *args, **kwargs) - def __init__(self, timeout=None, hostname_vary=True): + def __init__(self, timeout=None, hostname_vary=True, args=None, + kwargs=None): self.timeout = timeout self.hostname_vary = hostname_vary + self.args = args + self.kwargs = kwargs def set(self, key, value): raise NotImplementedError @@ -160,9 +163,14 @@ class CacheDecoratorBase(object): # if we cannot determine the hostname it's better to ignore the # cache raise CacheUnusable - for arg in args: + for i, arg in enumerate(args): + if self.args and i not in self.args: + continue parts.append(unicode(arg)) + for kw, arg in sorted(kwargs.iteritems(), key=lambda x: x[0]): + if self.kwargs in kw not in self.kwargs: + continue parts.append(u'%s-%s' % (unicode(kw), unicode(arg))) return u'|'.join(parts) -- 2.1.4