Exemplo n.º 1
0
    def decorator(fn):
        cache = LRUCache(maxsize)
        argspec = inspect2.getfullargspec(get_original_fn(fn))
        arg_names = argspec.args[1:] + argspec.kwonlyargs  # remove self
        async_fun = fn.asynq
        kwargs_defaults = get_kwargs_defaults(argspec)

        cache_key = key_fn
        if cache_key is None:

            def cache_key(args, kwargs):
                return get_args_tuple(args, kwargs, arg_names, kwargs_defaults)

        @asynq()
        @functools.wraps(fn)
        def wrapper(*args, **kwargs):
            key = cache_key(args, kwargs)
            try:
                result(cache[key])
                return
            except KeyError:
                value = yield async_fun(*args, **kwargs)
                cache[key] = value
                result(value)
                return

        return wrapper
Exemplo n.º 2
0
    def decorator(fun):
        _keygetter = keygetter
        if _keygetter is None:
            original_fn = get_original_fn(fun)
            argspec = inspect2.getfullargspec(original_fn)
            arg_names = argspec.args + argspec.kwonlyargs
            kwargs_defaults = get_kwargs_defaults(argspec)
            _keygetter = lambda args, kwargs: get_args_tuple(
                args, kwargs, arg_names, kwargs_defaults)

        return decorate(DeduplicateDecorator, fun.task_cls, _keygetter)(fun)
Exemplo n.º 3
0
    def cache_fun(fun):
        argspec = inspect2.getfullargspec(get_original_fn(fun))
        arg_names = argspec.args[1:] + argspec.kwonlyargs  # remove self
        async_fun = fun.asynq
        kwargs_defaults = get_kwargs_defaults(argspec)
        cache = {}

        def cache_key(args, kwargs):
            return get_args_tuple(args, kwargs, arg_names, kwargs_defaults)

        def clear_cache(instance_key, ref):
            del cache[instance_key]

        @async_proxy()
        @functools.wraps(fun)
        def new_fun(self, *args, **kwargs):
            instance_key = id(self)
            if instance_key not in cache:
                ref = weakref.ref(self,
                                  functools.partial(clear_cache, instance_key))
                cache[instance_key] = (ref, {})
            instance_cache = cache[instance_key][1]

            k = cache_key(args, kwargs)
            try:
                return ConstFuture(instance_cache[k])
            except KeyError:

                def callback(task):
                    instance_cache[k] = task.value()

                task = async_fun(self, *args, **kwargs)
                task.on_computed.subscribe(callback)
                return task

        # just so unit tests can check that this is cleaned up correctly
        new_fun.__acached_per_instance_cache__ = cache
        return new_fun
Exemplo n.º 4
0
    def cache_fun(fun):
        argspec = inspect2.getfullargspec(get_original_fn(fun))
        arg_names = argspec.args[1:] + argspec.kwonlyargs  # remove self
        async_fun = fun.asynq
        kwargs_defaults = get_kwargs_defaults(argspec)
        cache = {}

        def cache_key(args, kwargs):
            return get_args_tuple(args, kwargs, arg_names, kwargs_defaults)

        def clear_cache(instance_key, ref):
            del cache[instance_key]

        @asynq()
        @functools.wraps(fun)
        def new_fun(self, *args, **kwargs):
            instance_key = id(self)
            if instance_key not in cache:
                ref = weakref.ref(self,
                                  functools.partial(clear_cache, instance_key))
                cache[instance_key] = (ref, {})
            instance_cache = cache[instance_key][1]

            k = cache_key(args, kwargs)
            try:
                result(instance_cache[k])
                return
            except KeyError:
                value = yield async_fun(self, *args, **kwargs)
                instance_cache[k] = value
                result(value)
                return

        # just so unit tests can check that this is cleaned up correctly
        new_fun.__acached_per_instance_cache__ = cache
        return new_fun