示例#1
0
        def replacement(*args, **kwargs):
            """ Replacement
            """
            try:
                key = get_key(fun, *args, **kwargs)
            except volatile.DontCache:
                return fun(*args, **kwargs)
            key = '%s.%s:%s' % (fun.__module__, fun.__name__, key)
            cache_store = store_in_cache(fun, *args, **kwargs)
            cached_value = cache_store.get(key, _marker)
            if cached_value is _marker:
                cached_value = fun(*args, **kwargs)

                # plone.memoize doesn't have the lifetime keyword parameter
                # like eea.cache does so we check for set method
                if getattr(cache_store, 'set', None):
                    deps = dependencies or []
                    deps = deps[:]
                    if auto_invalidate:
                        uid = uuid(*args, **kwargs)
                        if uid and uid not in deps:
                            deps.append(uid)
                    cache_store.set(key, cached_value,
                                    lifetime=lifetime, dependencies=deps)
                else:
                    cache_store[key] = cached_value

            return cached_value
示例#2
0
        def replacement(*args, **kwargs):
            """ Replacement
            """
            try:
                key = get_key(fun, *args, **kwargs)
            except volatile.DontCache:
                return fun(*args, **kwargs)
            key = '%s.%s:%s' % (fun.__module__, fun.__name__, key)
            cache_store = store_in_cache(fun, *args, **kwargs)
            cached_value = cache_store.get(key, _marker)
            if cached_value is _marker:
                cached_value = fun(*args, **kwargs)

                # #104478 avoid caching empty values by default
                # pass cache_empty=True if you want to cache empty results
                # ex: @cache(lambda *args, lifetime=3600, cache_empty=True)
                if not cached_value and not cache_empty:
                    return cached_value

                # plone.memoize doesn't have the lifetime keyword parameter
                # like eea.cache does so we check for set method
                if getattr(cache_store, 'set', None):
                    deps = dependencies or []
                    deps = deps[:]
                    if auto_invalidate:
                        uid = uuid(*args, **kwargs)
                        if uid and uid not in deps:
                            deps.append(uid)
                    cache_store.set(key, cached_value,
                                    lifetime=lifetime, dependencies=deps)
                else:
                    cache_store[key] = cached_value

            return cached_value
示例#3
0
        def replacement(*args, **kwargs):
            try:
                key = get_key(fun, *args, **kwargs)
            except volatile.DontCache:
                return fun(*args, **kwargs)
            key = str(key)

            # Do not cache when not using memcache with dependencies
            memcache_client = queryUtility(IMemcachedClient)
            if (dependencies is not None or get_dependencies is not None) and not memcache_client:
                return fun(*args, **kwargs)

            if dependencies is not None or get_dependencies is not None:
                deps = dependencies
                if get_dependencies is not None:
                    deps = get_dependencies(fun, *args, **kwargs)
                for d in deps:
                    new_deps = DEPENDENCIES.get(d, [])
                    if key not in new_deps:
                        new_deps.append(key)
                        DEPENDENCIES[d] = new_deps
            cache = store_in_cache(fun, *args, **kwargs)
            cached_value = cache.get(key, _marker)
            if cached_value is _marker:
                if lifetime is None:
                    cached_value = cache[key] = fun(*args, **kwargs)
                else:
                    cached_value = fun(*args, **kwargs)
                    cache.setWithLifetime(key, cached_value, lifetime)
            return cached_value
示例#4
0
        def replacement(*args, **kwargs):
            if not IS_MEMCACHED_ACTIVE:
                return fun(*args, **kwargs)
            try:
                key = get_key(fun, *args, **kwargs)
            except volatile.DontCache:
                return fun(*args, **kwargs)
            key = str(key)

            # Do not cache when not using memcache with dependencies
            memcache_client = queryUtility(IMemcachedClient)
            if (dependencies is not None or get_dependencies is not None) and not memcache_client:
                return fun(*args, **kwargs)

            if dependencies is not None or get_dependencies is not None:
                deps = dependencies
                if get_dependencies is not None:
                    deps = get_dependencies(fun, *args, **kwargs)
            cache = store_in_cache(fun, *args, **kwargs)
            cached_value = cache.get(key, _marker)
            if cached_value is _marker:
                if lifetime is None:
                    cached_value = fun(*args, **kwargs)
                    cache.set(key, cached_value, deps)
                else:
                    cached_value = fun(*args, **kwargs)
                    cache.setWithLifetime(key, cached_value, lifetime, deps)
            return cached_value
def cache(func):
    adapter = store_in_cache(func)

    @functools.wraps(func)
    def decorator(self, *args, **kwargs):
        counter = getPersistentTranslationCounter(self.context)
        key = "%s:%d" % (self.uuid, counter.value)
        cache = CacheProxy(adapter, key)
        return func(self, cache, *args, **kwargs)

    return decorator