Skip to content

Commit

Permalink
fix @memoize typing annotations and add cache.memoize method
Browse files Browse the repository at this point in the history
  • Loading branch information
ecarrara committed Oct 2, 2024
1 parent 03f521f commit 08d9e71
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/yapcache/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import asyncio
from functools import wraps
from typing import (
Any,
Awaitable,
Callable,
Coroutine,
Optional,
ParamSpec,
TypeVar,
Expand All @@ -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]]):
Expand Down
9 changes: 8 additions & 1 deletion src/yapcache/caches/__init__.py
Original file line number Diff line number Diff line change
@@ -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 = ""):
Expand All @@ -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):
Expand Down

0 comments on commit 08d9e71

Please sign in to comment.