Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow ignoring some arguments for caching key #221

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions beaker/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)))

Expand Down