acachetools 0.1.2__tar.gz → 0.1.4__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.
- {acachetools-0.1.2 → acachetools-0.1.4}/PKG-INFO +5 -5
- {acachetools-0.1.2 → acachetools-0.1.4}/README.md +4 -4
- acachetools-0.1.4/pyproject.toml +40 -0
- acachetools-0.1.2/pyproject.toml → acachetools-0.1.4/pyproject.toml.orig +6 -1
- {acachetools-0.1.2 → acachetools-0.1.4}/src/acachetools/__init__.py +30 -37
- {acachetools-0.1.2 → acachetools-0.1.4}/src/acachetools/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: acachetools
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.4
|
|
4
4
|
Summary: Async cachetools decorators with stampede protection.
|
|
5
5
|
Author: RektPunk
|
|
6
6
|
Author-email: RektPunk <rektpunk@gmail.com>
|
|
@@ -17,6 +17,7 @@ Description-Content-Type: text/markdown
|
|
|
17
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
21
|
```bash
|
|
21
22
|
pip install acachetools
|
|
22
23
|
```
|
|
@@ -29,9 +30,9 @@ pip install acachetools
|
|
|
29
30
|
from cachetools import TTLCache
|
|
30
31
|
from acachetools import cached
|
|
31
32
|
|
|
33
|
+
|
|
32
34
|
@cached(cache=TTLCache(maxsize=1024, ttl=600))
|
|
33
|
-
async def some_func(value: int):
|
|
34
|
-
...
|
|
35
|
+
async def some_func(value: int): ...
|
|
35
36
|
```
|
|
36
37
|
|
|
37
38
|
### `cachedmethod`
|
|
@@ -46,6 +47,5 @@ class SomeClass:
|
|
|
46
47
|
self.cache = LRUCache(maxsize=128)
|
|
47
48
|
|
|
48
49
|
@cachedmethod(lambda self: self.cache)
|
|
49
|
-
async def some_func(self, value: int):
|
|
50
|
-
...
|
|
50
|
+
async def some_func(self, value: int): ...
|
|
51
51
|
```
|
|
@@ -5,6 +5,7 @@
|
|
|
5
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
6
|
|
|
7
7
|
## Installation
|
|
8
|
+
|
|
8
9
|
```bash
|
|
9
10
|
pip install acachetools
|
|
10
11
|
```
|
|
@@ -17,9 +18,9 @@ pip install acachetools
|
|
|
17
18
|
from cachetools import TTLCache
|
|
18
19
|
from acachetools import cached
|
|
19
20
|
|
|
21
|
+
|
|
20
22
|
@cached(cache=TTLCache(maxsize=1024, ttl=600))
|
|
21
|
-
async def some_func(value: int):
|
|
22
|
-
...
|
|
23
|
+
async def some_func(value: int): ...
|
|
23
24
|
```
|
|
24
25
|
|
|
25
26
|
### `cachedmethod`
|
|
@@ -34,6 +35,5 @@ class SomeClass:
|
|
|
34
35
|
self.cache = LRUCache(maxsize=128)
|
|
35
36
|
|
|
36
37
|
@cachedmethod(lambda self: self.cache)
|
|
37
|
-
async def some_func(self, value: int):
|
|
38
|
-
...
|
|
38
|
+
async def some_func(self, value: int): ...
|
|
39
39
|
```
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "acachetools"
|
|
3
|
+
version = "0.1.4"
|
|
4
|
+
description = "Async cachetools decorators with stampede protection."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.10"
|
|
7
|
+
classifiers = ["Topic :: Software Development :: Libraries :: Python Modules"]
|
|
8
|
+
dependencies = ["cachetools>=7.1.4"]
|
|
9
|
+
|
|
10
|
+
[[project.authors]]
|
|
11
|
+
name = "RektPunk"
|
|
12
|
+
email = "rektpunk@gmail.com"
|
|
13
|
+
|
|
14
|
+
[project.urls]
|
|
15
|
+
repository = "https://github.com/RektPunk/acachetools"
|
|
16
|
+
|
|
17
|
+
[build-system]
|
|
18
|
+
requires = ["uv_build>=0.11.23,<0.12.0"]
|
|
19
|
+
build-backend = "uv_build"
|
|
20
|
+
|
|
21
|
+
[dependency-groups]
|
|
22
|
+
dev = [
|
|
23
|
+
"pytest>=9.1.1",
|
|
24
|
+
"pytest-asyncio>=1.4.0",
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
[tool.pytest.ini_options]
|
|
28
|
+
asyncio_mode = "auto"
|
|
29
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
30
|
+
|
|
31
|
+
[tool.ruff.lint]
|
|
32
|
+
select = [
|
|
33
|
+
"B",
|
|
34
|
+
"E",
|
|
35
|
+
"F",
|
|
36
|
+
"I",
|
|
37
|
+
"UP",
|
|
38
|
+
"SIM",
|
|
39
|
+
]
|
|
40
|
+
ignore = ["E501"]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "acachetools"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.4"
|
|
4
4
|
description = "Async cachetools decorators with stampede protection."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
authors = [
|
|
@@ -30,3 +30,8 @@ dev = [
|
|
|
30
30
|
[tool.pytest.ini_options]
|
|
31
31
|
asyncio_mode = "auto"
|
|
32
32
|
asyncio_default_fixture_loop_scope = "function"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
[tool.ruff.lint]
|
|
36
|
+
select = ["B","E","F","I","UP","SIM"]
|
|
37
|
+
ignore = ["E501"]
|
|
@@ -24,6 +24,15 @@ class CachedAsyncMethod(Protocol[P, R]):
|
|
|
24
24
|
cache_clear: Callable[[Any], None]
|
|
25
25
|
|
|
26
26
|
|
|
27
|
+
def _remove_if_current(
|
|
28
|
+
cache_store: MutableMapping[Any, asyncio.Future[Any]],
|
|
29
|
+
cache_key: Any,
|
|
30
|
+
future: asyncio.Future[Any],
|
|
31
|
+
) -> None:
|
|
32
|
+
if cache_store.get(cache_key) is future:
|
|
33
|
+
cache_store.pop(cache_key, None)
|
|
34
|
+
|
|
35
|
+
|
|
27
36
|
async def _run_cached(
|
|
28
37
|
cache_store: MutableMapping[Any, asyncio.Future[R]],
|
|
29
38
|
cache_key: Any,
|
|
@@ -36,26 +45,20 @@ async def _run_cached(
|
|
|
36
45
|
if future is not None:
|
|
37
46
|
if future.cancelled():
|
|
38
47
|
# Cached Future was cancelled
|
|
39
|
-
|
|
40
|
-
cache_store.pop(cache_key, None)
|
|
48
|
+
_remove_if_current(cache_store, cache_key, future)
|
|
41
49
|
continue
|
|
42
50
|
|
|
43
51
|
if not future.done():
|
|
44
52
|
# Another task is still computing this key
|
|
45
53
|
# Wait for the shared result instead of recomputing
|
|
46
|
-
|
|
47
|
-
return await asyncio.shield(future)
|
|
48
|
-
except asyncio.CancelledError:
|
|
49
|
-
# The caller was cancelled while waiting
|
|
50
|
-
raise
|
|
54
|
+
return await asyncio.shield(future)
|
|
51
55
|
|
|
52
56
|
# Cached computation completed
|
|
53
57
|
try:
|
|
54
58
|
return future.result()
|
|
55
59
|
except Exception:
|
|
56
60
|
# Failed results are not cached
|
|
57
|
-
|
|
58
|
-
cache_store.pop(cache_key, None)
|
|
61
|
+
_remove_if_current(cache_store, cache_key, future)
|
|
59
62
|
continue
|
|
60
63
|
|
|
61
64
|
# Cache miss
|
|
@@ -76,12 +79,9 @@ async def _run_cached(
|
|
|
76
79
|
if not shared_future.done():
|
|
77
80
|
shared_future.set_result(result)
|
|
78
81
|
|
|
79
|
-
return result
|
|
80
|
-
|
|
81
82
|
except asyncio.CancelledError:
|
|
82
83
|
# The owner task was cancelled
|
|
83
|
-
|
|
84
|
-
cache_store.pop(cache_key, None)
|
|
84
|
+
_remove_if_current(cache_store, cache_key, shared_future)
|
|
85
85
|
|
|
86
86
|
if not shared_future.done():
|
|
87
87
|
shared_future.cancel()
|
|
@@ -90,8 +90,7 @@ async def _run_cached(
|
|
|
90
90
|
|
|
91
91
|
except Exception as exc:
|
|
92
92
|
# Exceptions are not cached
|
|
93
|
-
|
|
94
|
-
cache_store.pop(cache_key, None)
|
|
93
|
+
_remove_if_current(cache_store, cache_key, shared_future)
|
|
95
94
|
|
|
96
95
|
if not shared_future.done():
|
|
97
96
|
shared_future.set_exception(exc)
|
|
@@ -99,6 +98,13 @@ async def _run_cached(
|
|
|
99
98
|
shared_future.exception()
|
|
100
99
|
|
|
101
100
|
raise
|
|
101
|
+
finally:
|
|
102
|
+
# Ensure the shared Future is never left pending
|
|
103
|
+
if not shared_future.done():
|
|
104
|
+
_remove_if_current(cache_store, cache_key, shared_future)
|
|
105
|
+
shared_future.cancel()
|
|
106
|
+
|
|
107
|
+
return result
|
|
102
108
|
|
|
103
109
|
|
|
104
110
|
def _clear_cache(
|
|
@@ -112,28 +118,18 @@ def _clear_cache(
|
|
|
112
118
|
|
|
113
119
|
|
|
114
120
|
def cached(
|
|
115
|
-
cache: MutableMapping[Any, Any]
|
|
121
|
+
cache: MutableMapping[Any, Any],
|
|
116
122
|
*,
|
|
117
123
|
key: Callable[..., Any] = hashkey,
|
|
118
|
-
info: bool = False,
|
|
119
|
-
lock: object | None = None,
|
|
120
124
|
) -> Callable[[Callable[P, Coroutine[Any, Any, R]]], CachedAsyncFunction[P, R]]:
|
|
121
|
-
if info:
|
|
122
|
-
raise NotImplementedError("acachetools does not support `info`.")
|
|
123
|
-
if lock is not None:
|
|
124
|
-
raise NotImplementedError("acachetools does not support `lock`.")
|
|
125
|
-
|
|
126
|
-
cache_store = cast(
|
|
127
|
-
MutableMapping[Any, asyncio.Future[R]],
|
|
128
|
-
{} if cache is None else cache,
|
|
129
|
-
)
|
|
130
|
-
|
|
131
125
|
def decorator(
|
|
132
126
|
fn: Callable[P, Coroutine[Any, Any, R]],
|
|
133
127
|
) -> CachedAsyncFunction[P, R]:
|
|
134
128
|
if not iscoroutinefunction(fn):
|
|
135
129
|
raise TypeError(f"Expected Coroutine function, got {fn}")
|
|
136
130
|
|
|
131
|
+
cache_store = cast("MutableMapping[Any, asyncio.Future[R]]", cache)
|
|
132
|
+
|
|
137
133
|
async def wrapper(
|
|
138
134
|
*args: P.args,
|
|
139
135
|
**kwargs: P.kwargs,
|
|
@@ -147,7 +143,7 @@ def cached(
|
|
|
147
143
|
def cache_clear() -> None:
|
|
148
144
|
_clear_cache(cache_store)
|
|
149
145
|
|
|
150
|
-
wrapped = cast(CachedAsyncFunction[P, R], update_wrapper(wrapper, fn))
|
|
146
|
+
wrapped = cast("CachedAsyncFunction[P, R]", update_wrapper(wrapper, fn))
|
|
151
147
|
wrapped.cache = cache_store
|
|
152
148
|
wrapped.cache_clear = cache_clear
|
|
153
149
|
|
|
@@ -160,13 +156,10 @@ def cachedmethod(
|
|
|
160
156
|
cache: Callable[[Any], MutableMapping[Any, Any]],
|
|
161
157
|
*,
|
|
162
158
|
key: Callable[..., Any] = methodkey,
|
|
163
|
-
lock: Callable[[Any], Any] | None = None,
|
|
164
159
|
) -> Callable[
|
|
165
|
-
[Callable[Concatenate[Any, P], Coroutine[Any, Any, R]]],
|
|
160
|
+
[Callable[Concatenate[Any, P], Coroutine[Any, Any, R]]],
|
|
161
|
+
CachedAsyncMethod[P, R],
|
|
166
162
|
]:
|
|
167
|
-
if lock is not None:
|
|
168
|
-
raise NotImplementedError("acachetools does not support `lock`.")
|
|
169
|
-
|
|
170
163
|
def decorator(
|
|
171
164
|
method: Callable[Concatenate[Any, P], Coroutine[Any, Any, R]],
|
|
172
165
|
) -> CachedAsyncMethod[P, R]:
|
|
@@ -179,7 +172,7 @@ def cachedmethod(
|
|
|
179
172
|
**kwargs: P.kwargs,
|
|
180
173
|
) -> R:
|
|
181
174
|
cache_store = cast(
|
|
182
|
-
MutableMapping[Any, asyncio.Future[R]],
|
|
175
|
+
"MutableMapping[Any, asyncio.Future[R]]",
|
|
183
176
|
cache(self),
|
|
184
177
|
)
|
|
185
178
|
return await _run_cached(
|
|
@@ -194,12 +187,12 @@ def cachedmethod(
|
|
|
194
187
|
|
|
195
188
|
def cache_clear(self: Any) -> None:
|
|
196
189
|
cache_store = cast(
|
|
197
|
-
MutableMapping[Any, asyncio.Future[Any]],
|
|
190
|
+
"MutableMapping[Any, asyncio.Future[Any]]",
|
|
198
191
|
cache(self),
|
|
199
192
|
)
|
|
200
193
|
_clear_cache(cache_store)
|
|
201
194
|
|
|
202
|
-
wrapped = cast(CachedAsyncMethod[P, R], update_wrapper(wrapper, method))
|
|
195
|
+
wrapped = cast("CachedAsyncMethod[P, R]", update_wrapper(wrapper, method))
|
|
203
196
|
wrapped.cache = cache
|
|
204
197
|
wrapped.cache_clear = cache_clear
|
|
205
198
|
|
|
File without changes
|