acachetools 0.1.0__tar.gz → 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: acachetools
3
- Version: 0.1.0
3
+ Version: 0.1.2
4
4
  Summary: Async cachetools decorators with stampede protection.
5
5
  Author: RektPunk
6
6
  Author-email: RektPunk <rektpunk@gmail.com>
@@ -14,7 +14,7 @@ Description-Content-Type: text/markdown
14
14
  <img src="https://capsule-render.vercel.app/api?type=transparent&fontColor=0047AB&text=acachetools&height=120&fontSize=90">
15
15
  </div>
16
16
 
17
- **acachetools** provides asyncio-compatible versions of `cachetools.cached()` and `cachetools.cachedmethod()`. Concurrent calls for the same cache key share a single in-flight computation, preventing cache stampedes.
17
+ **acachetools** provides asyncio-compatible versions of `cachetools.cached()` and `cachetools.cachedmethod()`, with support for [`cachetools`](https://github.com/tkem/cachetools) cache implementations such as `TTLCache` and `LRUCache`. Concurrent calls for the same cache key share a single in-flight computation, preventing cache stampedes.
18
18
 
19
19
  ## Installation
20
20
  ```bash
@@ -22,12 +22,30 @@ pip install acachetools
22
22
  ```
23
23
 
24
24
  ## Usage
25
+
26
+ ### `cached`
27
+
25
28
  ```python
26
29
  from cachetools import TTLCache
27
30
  from acachetools import cached
28
31
 
29
- # Compatible with TTLCache, LRUCache, LFUCache, RRCache, etc.
30
32
  @cached(cache=TTLCache(maxsize=1024, ttl=600))
31
- async def foo(bar: int):
33
+ async def some_func(value: int):
32
34
  ...
33
35
  ```
36
+
37
+ ### `cachedmethod`
38
+
39
+ ```python
40
+ from cachetools import LRUCache
41
+ from acachetools import cachedmethod
42
+
43
+
44
+ class SomeClass:
45
+ def __init__(self):
46
+ self.cache = LRUCache(maxsize=128)
47
+
48
+ @cachedmethod(lambda self: self.cache)
49
+ async def some_func(self, value: int):
50
+ ...
51
+ ```
@@ -0,0 +1,39 @@
1
+ <div style="text-align: center;">
2
+ <img src="https://capsule-render.vercel.app/api?type=transparent&fontColor=0047AB&text=acachetools&height=120&fontSize=90">
3
+ </div>
4
+
5
+ **acachetools** provides asyncio-compatible versions of `cachetools.cached()` and `cachetools.cachedmethod()`, with support for [`cachetools`](https://github.com/tkem/cachetools) cache implementations such as `TTLCache` and `LRUCache`. Concurrent calls for the same cache key share a single in-flight computation, preventing cache stampedes.
6
+
7
+ ## Installation
8
+ ```bash
9
+ pip install acachetools
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ### `cached`
15
+
16
+ ```python
17
+ from cachetools import TTLCache
18
+ from acachetools import cached
19
+
20
+ @cached(cache=TTLCache(maxsize=1024, ttl=600))
21
+ async def some_func(value: int):
22
+ ...
23
+ ```
24
+
25
+ ### `cachedmethod`
26
+
27
+ ```python
28
+ from cachetools import LRUCache
29
+ from acachetools import cachedmethod
30
+
31
+
32
+ class SomeClass:
33
+ def __init__(self):
34
+ self.cache = LRUCache(maxsize=128)
35
+
36
+ @cachedmethod(lambda self: self.cache)
37
+ async def some_func(self, value: int):
38
+ ...
39
+ ```
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "acachetools"
3
- version = "0.1.0"
3
+ version = "0.1.2"
4
4
  description = "Async cachetools decorators with stampede protection."
5
5
  readme = "README.md"
6
6
  authors = [
@@ -1,8 +1,8 @@
1
1
  import asyncio
2
- from collections.abc import Awaitable, Callable, MutableMapping
2
+ from collections.abc import Callable, Coroutine, MutableMapping
3
3
  from functools import update_wrapper
4
4
  from inspect import iscoroutinefunction
5
- from typing import Any, ParamSpec, Protocol, TypeVar, cast, runtime_checkable
5
+ from typing import Any, Concatenate, ParamSpec, Protocol, TypeVar, cast
6
6
 
7
7
  from cachetools.keys import hashkey, methodkey
8
8
 
@@ -10,17 +10,15 @@ P = ParamSpec("P")
10
10
  R = TypeVar("R", covariant=True)
11
11
 
12
12
 
13
- @runtime_checkable
14
13
  class CachedAsyncFunction(Protocol[P, R]):
15
- def __call__(self, *args: P.args, **kwargs: P.kwargs) -> Awaitable[R]: ...
14
+ def __call__(self, *args: P.args, **kwargs: P.kwargs) -> Coroutine[Any, Any, R]: ...
16
15
 
17
16
  cache: MutableMapping[Any, Any]
18
17
  cache_clear: Callable[[], None]
19
18
 
20
19
 
21
- @runtime_checkable
22
20
  class CachedAsyncMethod(Protocol[P, R]):
23
- def __call__(self, *args: P.args, **kwargs: P.kwargs) -> Awaitable[R]: ...
21
+ def __call__(self, *args: P.args, **kwargs: P.kwargs) -> Coroutine[Any, Any, R]: ...
24
22
 
25
23
  cache: Callable[[Any], MutableMapping[Any, Any]]
26
24
  cache_clear: Callable[[Any], None]
@@ -29,7 +27,7 @@ class CachedAsyncMethod(Protocol[P, R]):
29
27
  async def _run_cached(
30
28
  cache_store: MutableMapping[Any, asyncio.Future[R]],
31
29
  cache_key: Any,
32
- coro_factory: Callable[[], Awaitable[R]],
30
+ coro_factory: Callable[[], Coroutine[Any, Any, R]],
33
31
  ) -> R:
34
32
  while True:
35
33
  future = cache_store.get(cache_key)
@@ -49,7 +47,7 @@ async def _run_cached(
49
47
  return await asyncio.shield(future)
50
48
  except asyncio.CancelledError:
51
49
  # The caller was cancelled while waiting
52
- continue
50
+ raise
53
51
 
54
52
  # Cached computation completed
55
53
  try:
@@ -118,8 +116,8 @@ def cached(
118
116
  *,
119
117
  key: Callable[..., Any] = hashkey,
120
118
  info: bool = False,
121
- lock: Any | None = None,
122
- ) -> Callable[[Callable[P, Awaitable[R]]], CachedAsyncFunction[P, R]]:
119
+ lock: object | None = None,
120
+ ) -> Callable[[Callable[P, Coroutine[Any, Any, R]]], CachedAsyncFunction[P, R]]:
123
121
  if info:
124
122
  raise NotImplementedError("acachetools does not support `info`.")
125
123
  if lock is not None:
@@ -131,7 +129,7 @@ def cached(
131
129
  )
132
130
 
133
131
  def decorator(
134
- fn: Callable[P, Awaitable[R]],
132
+ fn: Callable[P, Coroutine[Any, Any, R]],
135
133
  ) -> CachedAsyncFunction[P, R]:
136
134
  if not iscoroutinefunction(fn):
137
135
  raise TypeError(f"Expected Coroutine function, got {fn}")
@@ -149,10 +147,11 @@ def cached(
149
147
  def cache_clear() -> None:
150
148
  _clear_cache(cache_store)
151
149
 
152
- wrapped = update_wrapper(wrapper, fn)
153
- wrapped.cache = cache_store # type: ignore[attr-defined]
154
- wrapped.cache_clear = cache_clear # type: ignore[attr-defined]
155
- return wrapped # type: ignore[return-value]
150
+ wrapped = cast(CachedAsyncFunction[P, R], update_wrapper(wrapper, fn))
151
+ wrapped.cache = cache_store
152
+ wrapped.cache_clear = cache_clear
153
+
154
+ return wrapped
156
155
 
157
156
  return decorator
158
157
 
@@ -162,11 +161,15 @@ def cachedmethod(
162
161
  *,
163
162
  key: Callable[..., Any] = methodkey,
164
163
  lock: Callable[[Any], Any] | None = None,
165
- ) -> Callable[[Callable[..., Awaitable[R]]], CachedAsyncMethod[P, R]]:
164
+ ) -> Callable[
165
+ [Callable[Concatenate[Any, P], Coroutine[Any, Any, R]]], CachedAsyncMethod[P, R]
166
+ ]:
166
167
  if lock is not None:
167
168
  raise NotImplementedError("acachetools does not support `lock`.")
168
169
 
169
- def decorator(method: Callable[..., Awaitable[R]]) -> CachedAsyncMethod[P, R]:
170
+ def decorator(
171
+ method: Callable[Concatenate[Any, P], Coroutine[Any, Any, R]],
172
+ ) -> CachedAsyncMethod[P, R]:
170
173
  if not iscoroutinefunction(method):
171
174
  raise TypeError(f"Expected coroutine function, got {method!r}")
172
175
 
@@ -196,10 +199,10 @@ def cachedmethod(
196
199
  )
197
200
  _clear_cache(cache_store)
198
201
 
199
- wrapped = update_wrapper(wrapper, method)
200
- wrapped.cache = cache # type: ignore[attr-defined]
201
- wrapped.cache_clear = cache_clear # type: ignore[attr-defined]
202
+ wrapped = cast(CachedAsyncMethod[P, R], update_wrapper(wrapper, method))
203
+ wrapped.cache = cache
204
+ wrapped.cache_clear = cache_clear
202
205
 
203
- return wrapped # type: ignore[return-value]
206
+ return wrapped
204
207
 
205
208
  return decorator
@@ -1,21 +0,0 @@
1
- <div style="text-align: center;">
2
- <img src="https://capsule-render.vercel.app/api?type=transparent&fontColor=0047AB&text=acachetools&height=120&fontSize=90">
3
- </div>
4
-
5
- **acachetools** provides asyncio-compatible versions of `cachetools.cached()` and `cachetools.cachedmethod()`. Concurrent calls for the same cache key share a single in-flight computation, preventing cache stampedes.
6
-
7
- ## Installation
8
- ```bash
9
- pip install acachetools
10
- ```
11
-
12
- ## Usage
13
- ```python
14
- from cachetools import TTLCache
15
- from acachetools import cached
16
-
17
- # Compatible with TTLCache, LRUCache, LFUCache, RRCache, etc.
18
- @cached(cache=TTLCache(maxsize=1024, ttl=600))
19
- async def foo(bar: int):
20
- ...
21
- ```