def decorate(func): skip_self = util.has_self_arg(func) func_name = util.func_namespace(func) @wraps(func) def cached(*args): region_name = region[0] if type(region) is list else region # If router exist, use it to determine the cache region if router is not None: region_name = router(*args) if region_name is None: return func(*args) if cache.get(region_name) is None: reg = cache_regions.get(region_name) cache[region_name] = Cache._get_cache(namespace, reg) # If key generator exists, use it to generate a key if key_generator is not None: combined_args = decor_args + key_generator(*args) else: combined_args = decor_args + args[1:] if skip_self else args cache_key = get_cache_key(combined_args, func_name) def go(): value = func(*args) # jsonify object to avoid pickle to get better performance return json.dumps(value) value = cache[region_name].get_value(cache_key, createfunc=go) return json.loads(value) cached._func_name = func_name return cached
def decorate(func): namespace = util.func_namespace(func) skip_self = util.has_self_arg(func) def cached(*args): 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) cache[0] = Cache._get_cache(namespace, reg) elif manager: cache[0] = manager.get_cache(namespace, **kwargs) else: raise Exception("'manager + kwargs' or 'region' " "argument is required") if skip_self: cache_key = " ".join(map(str, deco_args + args[1:])) else: cache_key = " ".join(map(str, deco_args + args)) def go(): return func(*args) return cache[0].get_value(cache_key, createfunc=go) cached._arg_namespace = namespace if region is not None: cached._arg_region = region return cached
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") cache_key_kwargs = [] if kwargs: # kwargs provided, merge them in positional args # to avoid having different cache keys. args, kwargs = bindfuncargs(signature, args, kwargs) cache_key_kwargs = [u_(':').join((u_(key), u_(value))) for key, value in kwargs.items()] cache_key_args = args if skip_self: cache_key_args = args[1:] cache_key = u_(" ").join(map(u_, chain(deco_args, cache_key_args, cache_key_kwargs))) if region: cachereg = cache_regions[region] key_length = cachereg.get('key_length', util.DEFAULT_CACHE_KEY_LENGTH) else: key_length = options.pop('key_length', util.DEFAULT_CACHE_KEY_LENGTH) # TODO: This is probably a bug as length is checked before converting to UTF8 # which will cause cache_key to grow in size. if len(cache_key) + len(namespace) > int(key_length): cache_key = sha1(cache_key.encode('utf-8')).hexdigest() def go(): return func(*args, **kwargs) # save org function name go.__name__ = '_cached_%s' % (func.__name__,) return cache[0].get_value(cache_key, createfunc=go) cached._arg_namespace = namespace if region is not None: cached._arg_region = region return cached
def decorate(func): namespace = util.func_namespace(func) skip_self = util.has_self_arg(func) @wraps(func) def cached(*args): 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) cache[0] = Cache._get_cache(namespace, reg) elif manager: cache[0] = manager.get_cache(namespace, **kwargs) else: raise Exception("'manager + kwargs' or 'region' " "argument is required") if skip_self: try: cache_key = " ".join(map(str, deco_args + args[1:])) except UnicodeEncodeError: cache_key = " ".join(map(unicode, deco_args + args[1:])) else: try: cache_key = " ".join(map(str, deco_args + args)) except UnicodeEncodeError: cache_key = " ".join(map(unicode, deco_args + args)) if region: cachereg = cache_regions[region] key_length = cachereg.get('key_length', util.DEFAULT_CACHE_KEY_LENGTH) else: key_length = kwargs.pop('key_length', util.DEFAULT_CACHE_KEY_LENGTH) if len(cache_key) + len(namespace) > int(key_length): if util.py3k: cache_key = cache_key.encode('utf-8') cache_key = sha1(cache_key).hexdigest() def go(): return func(*args) return cache[0].get_value(cache_key, createfunc=go) cached._arg_namespace = namespace if region is not None: cached._arg_region = region return cached
def decorate(func): namespace = util.func_namespace(func) skip_self = util.has_self_arg(func) @wraps(func) def cached(*args): 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) cache[0] = Cache._get_cache(namespace, reg) elif manager: cache[0] = manager.get_cache(namespace, **kwargs) else: raise Exception("'manager + kwargs' or 'region' " "argument is required") cache_key_args = args if skip_self: cache_key_args = args[1:] cache_key = u_(" ").join(map(u_, deco_args + cache_key_args)) if region: cachereg = cache_regions[region] key_length = cachereg.get('key_length', util.DEFAULT_CACHE_KEY_LENGTH) else: key_length = kwargs.pop('key_length', util.DEFAULT_CACHE_KEY_LENGTH) # TODO: This is probably a bug as length is checked before converting to UTF8 # which will cause cache_key to grow in size. if len(cache_key) + len(namespace) > int(key_length): cache_key = sha1(cache_key.encode('utf-8')).hexdigest() def go(): return func(*args) return cache[0].get_value(cache_key, createfunc=go) cached._arg_namespace = namespace if region is not None: cached._arg_region = region return cached
def decorate(func): namespace = util.func_namespace(func) skip_self = util.has_self_arg(func) def cached(*args): 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) cache[0] = Cache._get_cache(namespace, reg) elif manager: cache[0] = manager.get_cache(namespace, **kwargs) else: raise Exception("'manager + kwargs' or 'region' " "argument is required") if skip_self: try: cache_key = " ".join(map(str, deco_args + args[1:])) except UnicodeEncodeError: cache_key = " ".join(map(unicode, deco_args + args[1:])) else: try: cache_key = " ".join(map(str, deco_args + args)) except UnicodeEncodeError: cache_key = " ".join(map(unicode, deco_args + args)) if region: key_length = cache_regions[region]['key_length'] else: key_length = kwargs.pop('key_length', 250) if len(cache_key) + len(namespace) > int(key_length): if util.py3k: cache_key = cache_key.encode('utf-8') cache_key = sha1(cache_key).hexdigest() def go(): return func(*args) return cache[0].get_value(cache_key, createfunc=go) cached._arg_namespace = namespace if region is not None: cached._arg_region = region return cached
def decorate(func): namespace = util.func_namespace(func) skip_self = util.has_self_arg(func) def cached(*args): 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) cache[0] = Cache._get_cache(namespace, reg) elif manager: cache[0] = manager.get_cache(namespace, **kwargs) else: raise Exception("'manager + kwargs' or 'region' " "argument is required") if skip_self: allargs = deco_args + args[1:] else: allargs = deco_args + args cache_key = " ".join(map(util.text_type, allargs)) cache_key = cache_key.encode("utf8") if region: key_length = cache_regions[region]['key_length'] else: key_length = kwargs.pop('key_length', 250) if len(cache_key) + len(namespace) > int(key_length): cache_key = sha1(cache_key).hexdigest() def go(): return func(*args) return cache[0].get_value(cache_key, createfunc=go) cached._arg_namespace = namespace if region is not None: cached._arg_region = region return cached