acachetools 0.1.2__tar.gz → 0.1.3__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.2
3
+ Version: 0.1.3
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.3"
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.2"
3
+ version = "0.1.3"
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"]
@@ -43,11 +43,7 @@ async def _run_cached(
43
43
  if not future.done():
44
44
  # Another task is still computing this key
45
45
  # Wait for the shared result instead of recomputing
46
- try:
47
- return await asyncio.shield(future)
48
- except asyncio.CancelledError:
49
- # The caller was cancelled while waiting
50
- raise
46
+ return await asyncio.shield(future)
51
47
 
52
48
  # Cached computation completed
53
49
  try:
@@ -76,8 +72,6 @@ async def _run_cached(
76
72
  if not shared_future.done():
77
73
  shared_future.set_result(result)
78
74
 
79
- return result
80
-
81
75
  except asyncio.CancelledError:
82
76
  # The owner task was cancelled
83
77
  if cache_store.get(cache_key) is shared_future:
@@ -100,6 +94,8 @@ async def _run_cached(
100
94
 
101
95
  raise
102
96
 
97
+ return result
98
+
103
99
 
104
100
  def _clear_cache(
105
101
  cache_store: MutableMapping[Any, asyncio.Future[Any]],
@@ -123,10 +119,7 @@ def cached(
123
119
  if lock is not None:
124
120
  raise NotImplementedError("acachetools does not support `lock`.")
125
121
 
126
- cache_store = cast(
127
- MutableMapping[Any, asyncio.Future[R]],
128
- {} if cache is None else cache,
129
- )
122
+ _cachec_dict = {} if cache is None else cache
130
123
 
131
124
  def decorator(
132
125
  fn: Callable[P, Coroutine[Any, Any, R]],
@@ -134,6 +127,8 @@ def cached(
134
127
  if not iscoroutinefunction(fn):
135
128
  raise TypeError(f"Expected Coroutine function, got {fn}")
136
129
 
130
+ cache_store = cast("MutableMapping[Any, asyncio.Future[R]]", _cachec_dict)
131
+
137
132
  async def wrapper(
138
133
  *args: P.args,
139
134
  **kwargs: P.kwargs,
@@ -147,7 +142,7 @@ def cached(
147
142
  def cache_clear() -> None:
148
143
  _clear_cache(cache_store)
149
144
 
150
- wrapped = cast(CachedAsyncFunction[P, R], update_wrapper(wrapper, fn))
145
+ wrapped = cast("CachedAsyncFunction[P, R]", update_wrapper(wrapper, fn))
151
146
  wrapped.cache = cache_store
152
147
  wrapped.cache_clear = cache_clear
153
148
 
@@ -162,7 +157,8 @@ def cachedmethod(
162
157
  key: Callable[..., Any] = methodkey,
163
158
  lock: Callable[[Any], Any] | None = None,
164
159
  ) -> Callable[
165
- [Callable[Concatenate[Any, P], Coroutine[Any, Any, R]]], CachedAsyncMethod[P, R]
160
+ [Callable[Concatenate[Any, P], Coroutine[Any, Any, R]]],
161
+ CachedAsyncMethod[P, R],
166
162
  ]:
167
163
  if lock is not None:
168
164
  raise NotImplementedError("acachetools does not support `lock`.")
@@ -179,7 +175,7 @@ def cachedmethod(
179
175
  **kwargs: P.kwargs,
180
176
  ) -> R:
181
177
  cache_store = cast(
182
- MutableMapping[Any, asyncio.Future[R]],
178
+ "MutableMapping[Any, asyncio.Future[R]]",
183
179
  cache(self),
184
180
  )
185
181
  return await _run_cached(
@@ -194,12 +190,12 @@ def cachedmethod(
194
190
 
195
191
  def cache_clear(self: Any) -> None:
196
192
  cache_store = cast(
197
- MutableMapping[Any, asyncio.Future[Any]],
193
+ "MutableMapping[Any, asyncio.Future[Any]]",
198
194
  cache(self),
199
195
  )
200
196
  _clear_cache(cache_store)
201
197
 
202
- wrapped = cast(CachedAsyncMethod[P, R], update_wrapper(wrapper, method))
198
+ wrapped = cast("CachedAsyncMethod[P, R]", update_wrapper(wrapper, method))
203
199
  wrapped.cache = cache
204
200
  wrapped.cache_clear = cache_clear
205
201