Example #1
0
    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
Example #2
0
 def decorator(func):
     cache_ns = namespace
     if cache_ns is None:
         cache_ns = util.func_namespace(func)
     def cached_method(self, *args, **kwargs):
         utility = getUtility(ICacheManager)
         if region is not None:
             cache = utility.get_cache_from_region(cache_ns, region)
         else:
             cache = utility.get_cache(cache_ns, **cache_options)
         cache_key = None
         if key is not None:
             cache_key = key(self, *args, **kwargs)
         if cache_key is None:
             cache_key = tuple()
             if IPersistent.providedBy(self):
                 cache_key += tuple([str(self._p_oid)])
             cache_key += tuple(map(str, args))
             cache_key += tuple(map(
                     lambda kwarg: "=".join(map(str, kwarg)),
                     sorted(kwargs.items(), key=operator.itemgetter(0))))
         def solves():
             return func(self, *args, **kwargs)
         return cache.get_value(_verify_key(cache_key), createfunc=solves,
             expiretime=expire)
     return cached_method
Example #3
0
    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
Example #4
0
    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
Example #5
0
            def cached(*args):
                if not cache[0]:
                    namespace = util.func_namespace(func)
                    cache[0] = self.get_cache(namespace, **kwargs)
                cache_key = key + " " + " ".join(str(x) for x in args)
                def go():
                    return func(*args)

                return cache[0].get_value(cache_key, createfunc=go)
Example #6
0
    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
Example #7
0
            def cached(*args):
                if not cache[0]:
                    namespace = util.func_namespace(func)
                    cache[0] = self.get_cache(namespace, **kwargs)
                cache_key = key + " " + " ".join(str(x) for x in args)

                def go():
                    return func(*args)

                return cache[0].get_value(cache_key, createfunc=go)
Example #8
0
 def cached(*args):
     reg = self.regions[region]
     if not reg.get('enabled', True):
         return func(*args)
     
     if not cache[0]:
         namespace = util.func_namespace(func)
         cache[0] = self.get_cache_region(namespace, region)
     
     cache_key = key + " " + " ".join(str(x) for x in args)
     def go():
         return func(*args)
     
     return cache[0].get_value(cache_key, createfunc=go)
Example #9
0
    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
Example #10
0
            def cached(*args):
                reg = self.regions[region]
                if not reg.get('enabled', True):
                    return func(*args)

                if not cache[0]:
                    namespace = util.func_namespace(func)
                    cache[0] = self.get_cache_region(namespace, region)

                cache_key = key + " " + " ".join(str(x) for x in args)

                def go():
                    return func(*args)

                return cache[0].get_value(cache_key, createfunc=go)
    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
Example #12
0
    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
Example #13
0
    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
Example #14
0
 def decorator(func):
     cache_ns = namespace
     if cache_ns is None:
         cache_ns = util.func_namespace(func)
     def cached_property(self):
         utility = getUtility(ICacheManager)
         if region is not None:
             cache = utility.get_cache_from_region(cache_ns, region)
         else:
             cache = utility.get_cache(cache_ns, **cache_options)
         cache_key = 'property'
         if IPersistent.providedBy(self):
             cache_key = str(self._p_oid)
         def solves():
             return func(self)
         return cache.get_value(_verify_key(cache_key), createfunc=solves,
             expiretime=expire)
     return property(cached_property)
Example #15
0
 def decorate(func):
     namespace = util.func_namespace(func)
     def cached(*args):
         reg = cache_regions[region]
         if not reg.get('enabled', True):
             return func(*args)
         
         if not cache[0]:
             if region not in cache_regions:
                 raise BeakerException('Cache region not configured: %s' % region)
             cache[0] = Cache._get_cache(namespace, reg)
         
         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
     cached._arg_region = region
     return cached
Example #16
0
 def decorate(func):
     namespace = util.func_namespace(func)
     def cached(*args):
         reg = cache_regions[region]
         if not reg.get('enabled', True):
             return func(*args)
         
         if not cache[0]:
             if region not in cache_regions:
                 raise BeakerException('Cache region not configured: %s' % region)
             cache[0] = cache_managers.setdefault(namespace + str(reg), Cache(namespace, **reg))
         
         cache_key = key + " " + " ".join(str(x) for x in args)
         def go():
             return func(*args)
         
         return cache[0].get_value(cache_key, createfunc=go)
     cached._arg_namespace = namespace
     cached._arg_region = region
     return cached
Example #17
0
    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
Example #18
0
from    beaker.cache          import CacheManager
from    beaker.util           import parse_cache_config_options, func_namespace
from    beakerfiddling.addone import addone
import  os

here = os.path.dirname(os.path.abspath(__file__))
c = ConfigParser.ConfigParser(defaults={'here': here})
c.read(os.path.join(here, 'development.ini'))
cache_manager = CacheManager(**parse_cache_config_options(dict(c.items('app:main'))))

print(addone(0))
print(addone(0))

print(addone(9))
print(addone(9))

cache_manager.region_invalidate(addone, 'region1', 0)
print(addone(0))
print(addone(0))

print(addone(9))
print(addone(9))

# Now interrogate the cache -- see if it has values for 0 and 9 (it
# should, of course).

c = cache_manager.get_cache(func_namespace(addone.original_function))

for n in range(10):
    print("{}: {}".format(n, "Present" if str(n) in c else "absent"))
def test_func_namespace():
    def go(x, y):
        return "hi standalone"

    assert 'test_cache_decorator' in util.func_namespace(go)
    assert util.func_namespace(go).endswith('go')