Exemplo n.º 1
0
    def decorate(func):
        namespace = func_namespace(func, deco_args)
        skip_self = has_self_arg(func)
        regenerate = kwargs.get('regenerate', True)

        @wraps(func)
        def cached(*args):
            if region not in CacheRegion.regions:
                raise CacheConfigurationError(
                    'Cache region not configured: %s' % region)
            if not CacheRegion.enabled:
                return func(*args)

            if skip_self:
                try:
                    cache_key = " ".join(map(str, args[1:]))
                except UnicodeEncodeError:
                    cache_key = " ".join(map(unicode, args[1:]))
            else:
                try:
                    cache_key = " ".join(map(str, args))
                except UnicodeEncodeError:
                    cache_key = " ".join(map(unicode, args))

            def go():
                return func(*args)
            return CacheRegion.load(region, namespace, cache_key,
                                    regenerate=regenerate, callable=go)
        cached._region = region
        cached._namespace = namespace
        return cached