Skip to content
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions async_lru/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from asyncio.coroutines import _is_coroutine # type: ignore[attr-defined]
from functools import _CacheInfo, _make_key, partial, partialmethod
from typing import (
Any,
Callable,
Coroutine,
Generic,
Expand Down Expand Up @@ -208,32 +209,30 @@ async def __call__(self, /, *fn_args: _P.args, **fn_kwargs: _P.kwargs) -> _R:
return cache_item.fut.result()

fut = loop.create_future()
coro = self.__wrapped__(*fn_args, **fn_kwargs)
task = loop.create_task(coro)
task = loop.create_task(self.__wrapped__(*fn_args, **fn_kwargs))
self.__tasks.add(task)
task.add_done_callback(partial(self._task_done_callback, fut, key))

self.__cache[key] = _CacheItem(fut, None)

if self.__maxsize is not None and len(self.__cache) > self.__maxsize:
dropped_key, cache_item = self.__cache.popitem(last=False)
cache_item.cancel()
self.__cache.popitem(last=False)[1].cancel()

self._cache_miss(key)
return await asyncio.shield(fut)

@overload
def __get__(self, instance: _T, owner: None) -> Self:
def __get__(self, instance: Any, owner: None) -> Self:
...

@overload
def __get__(
self, instance: _T, owner: Type[_T]
self, instance: _T, owner: Type[Any]
) -> "_LRUCacheWrapperInstanceMethod[_P, _R, _T]":
...

def __get__(
self, instance: _T, owner: Optional[Type[_T]]
self, instance: Any, owner: Optional[Type[Any]]
) -> Union[Self, "_LRUCacheWrapperInstanceMethod[_P, _R, _T]"]:
if owner is None:
return self
Expand Down Expand Up @@ -280,7 +279,7 @@ def __init__(
self.__wrapper = wrapper

def cache_invalidate(self, /, *args: _P.args, **kwargs: _P.kwargs) -> bool:
return self.__wrapper.cache_invalidate(self.__instance, *args, **kwargs)
return self.__wrapper.cache_invalidate(self.__instance, *args, **kwargs) # type: ignore[arg-type]

def cache_clear(self) -> None:
self.__wrapper.cache_clear()
Expand All @@ -296,8 +295,8 @@ def cache_info(self) -> _CacheInfo:
def cache_parameters(self) -> _CacheParameters:
return self.__wrapper.cache_parameters()

async def __call__(self, /, *fn_args: _P.args, **fn_kwargs: _P.kwargs) -> _R:
return await self.__wrapper(self.__instance, *fn_args, **fn_kwargs) # type: ignore[arg-type]
async def __call__(self, /, *args: _P.args, **kwargs: _P.kwargs) -> _R:
return self.__wrapper.cache_invalidate(self.__instance, *args, **kwargs) # type: ignore[arg-type]


def _make_wrapper(
Expand Down
Loading