Skip to content

Incapable to disable cache after modified region settings #215

@fmigneault

Description

@fmigneault

When using cache_region decorator, beaker will use the following snippet to determine if it should instantiate a new cache for each function that was decorated with it.

beaker/beaker/cache.py

Lines 545 to 570 in 889d305

def _cache_decorate(deco_args, manager, options, region):
"""Return a caching function decorator."""
cache = [None]
def decorate(func):
namespace = util.func_namespace(func)
skip_self = util.has_self_arg(func)
signature = func_signature(func)
@wraps(func)
def cached(*args, **kwargs):
if not cache[0]:
if region is not None:
if region not in cache_regions:
raise BeakerException(
'Cache region not configured: %s' % region)
reg = cache_regions[region]
if not reg.get('enabled', True):
return func(*args, **kwargs)
cache[0] = Cache._get_cache(namespace, reg)
elif manager:
cache[0] = manager.get_cache(namespace, **options)
else:
raise Exception("'manager + kwargs' or 'region' "
"argument is required")

When cache[0] is None, the decorator generates the missing cache if the corresponding region was defined with enabled = true and stores it in that local list.

I have a situation where I'm running unit tests on an application where I want to evaluate different combinations of cache regions with enabled/disable states. The problem with that local list storing the enabled cache is that if one test case later that must have it disabled, the if not cache[0]: check completely bypasses whichever new settings I set via cache_regions, since the cache is already created.

Also, since the Cache reference is stored in that local list (rather than global/package one), I cannot even wipe it.

Calling cache_managers.clear(), which has the same reference to that local cache added from the following lines will NOT clear cache[0].

beaker/beaker/cache.py

Lines 308 to 314 in 889d305

def _get_cache(cls, namespace, kw):
key = namespace + str(kw)
try:
return cache_managers[key]
except KeyError:
cache_managers[key] = cache = cls(namespace, **kw)
return cache

Is there a way to avoid this?
I struggle trying to monkey-patch the cache object in cache[0] for my test cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions