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

Incapable to disable cache after modified region settings #215

Open
fmigneault opened this issue Jul 14, 2021 · 0 comments
Open

Incapable to disable cache after modified region settings #215

fmigneault opened this issue Jul 14, 2021 · 0 comments

Comments

@fmigneault
Copy link

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.

fmigneault added a commit to Ouranosinc/Magpie that referenced this issue Jul 14, 2021
… to work around bbangert/beaker#215) + test OWS parsing of request against those caches
fmigneault added a commit to Ouranosinc/Magpie that referenced this issue Jul 14, 2021
… to work around bbangert/beaker#215) + test OWS parsing of request against those caches
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant