diff --git a/src/yapcache/__init__.py b/src/yapcache/__init__.py index be6e128..27e73ef 100644 --- a/src/yapcache/__init__.py +++ b/src/yapcache/__init__.py @@ -1,8 +1,10 @@ import asyncio from functools import wraps from typing import ( + Any, Awaitable, Callable, + Coroutine, Optional, ParamSpec, TypeVar, @@ -22,7 +24,9 @@ def memoize( ttl: float, best_before: Callable[[R], Optional[float]] = lambda *a, **kw: None, lock: Callable[[str], DistLock] = lambda *a, **kw: NullLock(), -) -> Callable[[Callable[P, Awaitable[R]]], Callable[P, Awaitable[R]]]: +) -> Callable[ + [Callable[P, Coroutine[Any, Any, R]]], Callable[P, Coroutine[Any, Any, R]] +]: update_tasks: dict[str, asyncio.Task] = {} def decorator(fn: Callable[P, Awaitable[R]]): diff --git a/src/yapcache/caches/__init__.py b/src/yapcache/caches/__init__.py index 0305252..1cf284f 100644 --- a/src/yapcache/caches/__init__.py +++ b/src/yapcache/caches/__init__.py @@ -1,8 +1,12 @@ import asyncio -from typing import Any, override +from typing import Any, Callable, Coroutine, ParamSpec, TypeVar, override +from yapcache import memoize from yapcache.cache_item import NOT_FOUND, CacheItem, NotFound +P = ParamSpec("P") +R = TypeVar("R") + class Cache: def __init__(self, key_prefix: str = ""): @@ -25,6 +29,9 @@ async def set( async def close(self): ... + def memoize[R](self, fn: Callable[..., Coroutine[Any, Any, R]], *args, **kwargs): + return memoize(self, *args, **kwargs)(fn) + class MultiLayerCache(Cache): def __init__(self, caches: list[Cache] = [], **kwargs):