Esempio n. 1
0
 def _format_key(self, key):
     if not isinstance(key, str):
         key = key.decode("ascii")
     formated_key = (self.namespace + "_" + key).replace(" ", "\302\267")
     if len(formated_key) > MAX_KEY_LENGTH:
         formated_key = sha1(formated_key).hexdigest()
     return formated_key
Esempio n. 2
0
        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):
                cache_key = sha1(cache_key).hexdigest()

            def go():
                return func(*args)

            return cache[0].get_value(cache_key, createfunc=go)
Esempio n. 3
0
def _cache_decorator_invalidate(cache, key_length, args):
    """Invalidate a cache key based on function arguments."""

    try:
        cache_key = " ".join(map(str, args))
    except UnicodeEncodeError:
        cache_key = " ".join(map(unicode, args))
    if len(cache_key) + len(cache.namespace_name) > key_length:
        cache_key = sha1(cache_key).hexdigest()
    cache.remove_value(cache_key)