Exemplo n.º 1
0
        def wrapper(wrappingobj: CacheableObject, *args: Any,
                    **kwargs: Any) -> Any:
            function_sig = f.__name__
            for arg in args:
                function_sig += str(arg)
            for _, value in kwargs.items():
                function_sig += str(value)
            # TODO: Do I really need hash() here?
            cache_key = hash(function_sig)

            with wrappingobj.lock:
                now = ts_now()
                if cache_key in wrappingobj.results_cache:
                    cache_life_secs = now - wrappingobj.results_cache[
                        cache_key].timestamp

                cache_miss = (cache_key not in wrappingobj.results_cache
                              or cache_life_secs >= wrappingobj.cache_ttl_secs)

            if cache_miss:
                result = f(wrappingobj, *args, **kwargs)
                with wrappingobj.lock:
                    wrappingobj.results_cache[cache_key] = ResultCache(
                        result, now)
                return result

            # else hit the cache
            with wrappingobj.lock:
                return wrappingobj.results_cache[cache_key].result
Exemplo n.º 2
0
        def wrapper(wrappingobj, *args):
            with wrappingobj.lock:
                now = ts_now()
                cache_miss = (
                    f.__name__ not in wrappingobj.results_cache or
                    now - wrappingobj.results_cache[f.__name__].timestamp > seconds
                )
            if cache_miss:
                result = f(wrappingobj, *args)
                with wrappingobj.lock:
                    wrappingobj.results_cache[f.__name__] = ResultCache(result, now)
                return result

            # else hit the cache
            with wrappingobj.lock:
                return wrappingobj.results_cache[f.__name__].result
Exemplo n.º 3
0
        def wrapper(wrappingobj, *args):
            with wrappingobj.lock:
                now = ts_now()
                if f.__name__ in wrappingobj.results_cache:
                    cache_life_secs = now - wrappingobj.results_cache[f.__name__].timestamp
                cache_miss = (
                    f.__name__ not in wrappingobj.results_cache or
                    cache_life_secs > wrappingobj.cache_ttl_secs
                )
            if cache_miss:
                result = f(wrappingobj, *args)
                with wrappingobj.lock:
                    wrappingobj.results_cache[f.__name__] = ResultCache(result, now)
                return result

            # else hit the cache
            with wrappingobj.lock:
                return wrappingobj.results_cache[f.__name__].result
Exemplo n.º 4
0
        def wrapper(wrappingobj: CacheableObject, *args: Any,
                    **kwargs: Any) -> Any:
            ignore_cache = kwargs.pop('ignore_cache', False)
            cache_key = _function_sig_key(f.__name__, False, *args, **kwargs)
            now = ts_now()
            if ignore_cache is False:
                # Check the cache
                if cache_key in wrappingobj.results_cache:
                    cache_life_secs = now - wrappingobj.results_cache[
                        cache_key].timestamp

            cache_miss = (ignore_cache is True
                          or cache_key not in wrappingobj.results_cache
                          or cache_life_secs >= wrappingobj.cache_ttl_secs)

            if cache_miss:
                # Call the function, write the result in cache and return it
                result = f(wrappingobj, *args, **kwargs)
                wrappingobj.results_cache[cache_key] = ResultCache(result, now)
                return result

            # else hit the cache and return it
            return wrappingobj.results_cache[cache_key].result