Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: add simple cache func

NTFSvolume49c9554dea1c
diff --git a/cyberdrop_dl/cache.py b/cyberdrop_dl/cache.py
index 933261e4b..ba5ea72aa 100644
--- a/cyberdrop_dl/cache.py
+++ b/cyberdrop_dl/cache.py
@@ -9,7 +9,7 @@ import logging
import time
from typing import TYPE_CHECKING, Any, Protocol, Self, TypedDict, cast, overload
-from cyberdrop_dl import __version__
+from cyberdrop_dl import __version__, aio
from cyberdrop_dl.constants import MISSING
if TYPE_CHECKING:
@@ -216,6 +216,13 @@ class _CachedMethod[T, R]:
return self._cached_fn
+def async_lru_cache[T: Callable[[Any], Awaitable[Any]]](fn: T) -> T:
+ """Cache the first result.
+
+ Only works with callables that take 1 single argument"""
+ return functools.wraps(fn)(aio.async_defaultdict(fn).get) # pyright: ignore[reportReturnType]
+
+
class _InMemoryCachedMethod[R](_CachedMethod[object, R]):
def _get_cache(self, inst: object) -> TTLCacheAdapter[R]: # noqa: ARG002
return _ttl_from_callable(self.wrapped)