py-cachify 2.0.0__tar.gz → 2.0.1__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.
- {py_cachify-2.0.0 → py_cachify-2.0.1}/PKG-INFO +21 -3
- {py_cachify-2.0.0 → py_cachify-2.0.1}/README.md +20 -2
- py_cachify-2.0.1/py_cachify/__init__.py +25 -0
- {py_cachify-2.0.0 → py_cachify-2.0.1}/py_cachify/_backend/_cached.py +5 -4
- {py_cachify-2.0.0 → py_cachify-2.0.1}/py_cachify/_backend/_helpers.py +3 -3
- {py_cachify-2.0.0 → py_cachify-2.0.1}/py_cachify/_backend/_lock.py +14 -12
- {py_cachify-2.0.0 → py_cachify-2.0.1}/py_cachify/_backend/_types/_lock_wrap.py +2 -2
- {py_cachify-2.0.0 → py_cachify-2.0.1}/pyproject.toml +7 -26
- py_cachify-2.0.0/py_cachify/__init__.py +0 -32
- {py_cachify-2.0.0 → py_cachify-2.0.1}/LICENSE +0 -0
- {py_cachify-2.0.0 → py_cachify-2.0.1}/py_cachify/_backend/__init__.py +0 -0
- {py_cachify-2.0.0 → py_cachify-2.0.1}/py_cachify/_backend/_clients.py +0 -0
- {py_cachify-2.0.0 → py_cachify-2.0.1}/py_cachify/_backend/_exceptions.py +0 -0
- {py_cachify-2.0.0 → py_cachify-2.0.1}/py_cachify/_backend/_lib.py +0 -0
- {py_cachify-2.0.0 → py_cachify-2.0.1}/py_cachify/_backend/_types/__init__.py +0 -0
- {py_cachify-2.0.0 → py_cachify-2.0.1}/py_cachify/_backend/_types/_common.py +0 -0
- {py_cachify-2.0.0 → py_cachify-2.0.1}/py_cachify/_backend/_types/_reset_wrap.py +0 -0
- {py_cachify-2.0.0 → py_cachify-2.0.1}/py_cachify/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: py-cachify
|
|
3
|
-
Version: 2.0.
|
|
3
|
+
Version: 2.0.1
|
|
4
4
|
Summary: Distributed locks, caching, and locking decorators at hand
|
|
5
5
|
Home-page: https://github.com/EzyGang/py-cachify
|
|
6
6
|
License: MIT
|
|
@@ -143,6 +143,10 @@ async def sum_two(a: int, b: int) -> int:
|
|
|
143
143
|
# Let's put print here to see what was the function called with
|
|
144
144
|
print(f'Called with {a} {b}')
|
|
145
145
|
return a + b
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
# Reset the cache for the call with arguments a=1, b=2
|
|
149
|
+
await sub_two.reset(a=1, b=2)
|
|
146
150
|
```
|
|
147
151
|
|
|
148
152
|
Read more about `@cached` [here](https://py-cachify.readthedocs.io/latest/reference/cached/).
|
|
@@ -154,11 +158,18 @@ Locking through context manager:
|
|
|
154
158
|
```python
|
|
155
159
|
from py_cachify import lock
|
|
156
160
|
|
|
161
|
+
|
|
162
|
+
async_lock = lock('resource_key')
|
|
157
163
|
# Use it within an asynchronous context
|
|
158
|
-
async with
|
|
164
|
+
async with async_lock:
|
|
159
165
|
# Your critical section here
|
|
160
166
|
print('Critical section code')
|
|
161
167
|
|
|
168
|
+
# Check if it's locked
|
|
169
|
+
await async_lock.is_alocked()
|
|
170
|
+
|
|
171
|
+
# Forcefully release
|
|
172
|
+
await async_lock.arelease()
|
|
162
173
|
|
|
163
174
|
# Use it within a synchronous context
|
|
164
175
|
with lock('resource_key'):
|
|
@@ -173,8 +184,15 @@ Locking via decorator:
|
|
|
173
184
|
from py_cachify import lock
|
|
174
185
|
|
|
175
186
|
@lock(key='critical_function_lock-{arg}', nowait=False, timeout=10)
|
|
176
|
-
def critical_function(arg: int) -> None:
|
|
187
|
+
async def critical_function(arg: int) -> None:
|
|
177
188
|
# critical code
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
# Check if it's locked for arg=5
|
|
192
|
+
await critical_function.is_locked(arg=5)
|
|
193
|
+
|
|
194
|
+
# Forcefully release for arg=5
|
|
195
|
+
await critical_function.release(arg=5)
|
|
178
196
|
```
|
|
179
197
|
|
|
180
198
|
Read more about `lock` [here](https://py-cachify.readthedocs.io/latest/reference/lock/).
|
|
@@ -112,6 +112,10 @@ async def sum_two(a: int, b: int) -> int:
|
|
|
112
112
|
# Let's put print here to see what was the function called with
|
|
113
113
|
print(f'Called with {a} {b}')
|
|
114
114
|
return a + b
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
# Reset the cache for the call with arguments a=1, b=2
|
|
118
|
+
await sub_two.reset(a=1, b=2)
|
|
115
119
|
```
|
|
116
120
|
|
|
117
121
|
Read more about `@cached` [here](https://py-cachify.readthedocs.io/latest/reference/cached/).
|
|
@@ -123,11 +127,18 @@ Locking through context manager:
|
|
|
123
127
|
```python
|
|
124
128
|
from py_cachify import lock
|
|
125
129
|
|
|
130
|
+
|
|
131
|
+
async_lock = lock('resource_key')
|
|
126
132
|
# Use it within an asynchronous context
|
|
127
|
-
async with
|
|
133
|
+
async with async_lock:
|
|
128
134
|
# Your critical section here
|
|
129
135
|
print('Critical section code')
|
|
130
136
|
|
|
137
|
+
# Check if it's locked
|
|
138
|
+
await async_lock.is_alocked()
|
|
139
|
+
|
|
140
|
+
# Forcefully release
|
|
141
|
+
await async_lock.arelease()
|
|
131
142
|
|
|
132
143
|
# Use it within a synchronous context
|
|
133
144
|
with lock('resource_key'):
|
|
@@ -142,8 +153,15 @@ Locking via decorator:
|
|
|
142
153
|
from py_cachify import lock
|
|
143
154
|
|
|
144
155
|
@lock(key='critical_function_lock-{arg}', nowait=False, timeout=10)
|
|
145
|
-
def critical_function(arg: int) -> None:
|
|
156
|
+
async def critical_function(arg: int) -> None:
|
|
146
157
|
# critical code
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
# Check if it's locked for arg=5
|
|
161
|
+
await critical_function.is_locked(arg=5)
|
|
162
|
+
|
|
163
|
+
# Forcefully release for arg=5
|
|
164
|
+
await critical_function.release(arg=5)
|
|
147
165
|
```
|
|
148
166
|
|
|
149
167
|
Read more about `lock` [here](https://py-cachify.readthedocs.io/latest/reference/lock/).
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from ._backend._cached import async_cached as async_cached
|
|
2
|
+
from ._backend._cached import cached as cached
|
|
3
|
+
from ._backend._cached import sync_cached as sync_cached
|
|
4
|
+
from ._backend._exceptions import CachifyInitError as CachifyInitError
|
|
5
|
+
from ._backend._exceptions import CachifyLockError as CachifyLockError
|
|
6
|
+
from ._backend._lib import init_cachify as init_cachify
|
|
7
|
+
from ._backend._lock import async_once as async_once
|
|
8
|
+
from ._backend._lock import lock as lock
|
|
9
|
+
from ._backend._lock import once as once
|
|
10
|
+
from ._backend._lock import sync_once as sync_once
|
|
11
|
+
from ._backend._types._common import AsyncClient as AsyncClient
|
|
12
|
+
from ._backend._types._common import Decoder as Decoder
|
|
13
|
+
from ._backend._types._common import Encoder as Encoder
|
|
14
|
+
from ._backend._types._common import SyncClient as SyncClient
|
|
15
|
+
from ._backend._types._lock_wrap import AsyncLockWrappedF as AsyncLockWrappedF
|
|
16
|
+
from ._backend._types._lock_wrap import SyncLockWrappedF as SyncLockWrappedF
|
|
17
|
+
from ._backend._types._lock_wrap import WrappedFunctionLock as WrappedFunctionLock
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
try:
|
|
21
|
+
from importlib.metadata import version
|
|
22
|
+
|
|
23
|
+
__version__ = version('py-cachify')
|
|
24
|
+
except ModuleNotFoundError:
|
|
25
|
+
__version__ = f'No version available for {__name__}'
|
|
@@ -46,7 +46,7 @@ def cached(
|
|
|
46
46
|
) -> SyncResetWrappedF[_P, _R]: ...
|
|
47
47
|
|
|
48
48
|
def _cached_inner(
|
|
49
|
-
_func: Union[Callable[_P, _R], Callable[_P,
|
|
49
|
+
_func: Union[Callable[_P, Awaitable[_R]], Callable[_P, _R]],
|
|
50
50
|
) -> Union[AsyncResetWrappedF[_P, _R], SyncResetWrappedF[_P, _R]]:
|
|
51
51
|
signature = inspect.signature(_func)
|
|
52
52
|
|
|
@@ -72,17 +72,18 @@ def cached(
|
|
|
72
72
|
|
|
73
73
|
return cast(AsyncResetWrappedF[_P, _R], cast(object, _async_wrapper))
|
|
74
74
|
else:
|
|
75
|
+
_sync_func = cast(Callable[_P, _R], _func)
|
|
75
76
|
|
|
76
|
-
@wraps(
|
|
77
|
+
@wraps(_sync_func)
|
|
77
78
|
def _sync_wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R:
|
|
78
79
|
cachify = get_cachify()
|
|
79
80
|
_key = get_full_key_from_signature(bound_args=signature.bind(*args, **kwargs), key=key)
|
|
80
81
|
if val := cachify.get(key=_key):
|
|
81
82
|
return encode_decode_value(encoder_decoder=dec, val=val)
|
|
82
83
|
|
|
83
|
-
res =
|
|
84
|
+
res = _sync_func(*args, **kwargs)
|
|
84
85
|
cachify.set(key=_key, val=encode_decode_value(encoder_decoder=enc, val=res), ttl=ttl)
|
|
85
|
-
return
|
|
86
|
+
return res
|
|
86
87
|
|
|
87
88
|
setattr(_sync_wrapper, 'reset', partial(reset, signature=signature, key=key))
|
|
88
89
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import asyncio
|
|
2
2
|
import inspect
|
|
3
|
-
from typing import Any, Awaitable, Callable, TypeVar, Union
|
|
3
|
+
from typing import Any, Awaitable, Callable, Dict, Tuple, TypeVar, Union
|
|
4
4
|
|
|
5
5
|
from typing_extensions import ParamSpec, TypeIs
|
|
6
6
|
|
|
@@ -15,8 +15,8 @@ _S = TypeVar('_S')
|
|
|
15
15
|
|
|
16
16
|
def get_full_key_from_signature(bound_args: inspect.BoundArguments, key: str) -> str:
|
|
17
17
|
args_dict = bound_args.arguments
|
|
18
|
-
args = args_dict.pop('args', ())
|
|
19
|
-
kwargs = args_dict.pop('kwargs', {})
|
|
18
|
+
args: Tuple[str, Any] = args_dict.pop('args', ())
|
|
19
|
+
kwargs: Dict[str, Any] = args_dict.pop('kwargs', {})
|
|
20
20
|
kwargs.update(args_dict)
|
|
21
21
|
|
|
22
22
|
try:
|
|
@@ -132,10 +132,10 @@ class lock(AsyncLockMethods, SyncLockMethods):
|
|
|
132
132
|
self._exp = exp
|
|
133
133
|
|
|
134
134
|
@overload
|
|
135
|
-
def __call__(self, _func: Callable[_P, Awaitable[_R]]
|
|
135
|
+
def __call__(self, _func: Callable[_P, Awaitable[_R]]) -> AsyncLockWrappedF[_P, _R]: ... # type: ignore[overload-overlap]
|
|
136
136
|
|
|
137
137
|
@overload
|
|
138
|
-
def __call__(self, _func: Callable[_P, _R]
|
|
138
|
+
def __call__(self, _func: Callable[_P, _R]) -> SyncLockWrappedF[_P, _R]: ...
|
|
139
139
|
|
|
140
140
|
def __call__(
|
|
141
141
|
self,
|
|
@@ -153,7 +153,7 @@ class lock(AsyncLockMethods, SyncLockMethods):
|
|
|
153
153
|
_awaitable_func = _func
|
|
154
154
|
|
|
155
155
|
@wraps(_awaitable_func)
|
|
156
|
-
async def _async_wrapper(*args: _P.args, **kwargs: _P.kwargs) ->
|
|
156
|
+
async def _async_wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R:
|
|
157
157
|
bound_args = signature.bind(*args, **kwargs)
|
|
158
158
|
_key = get_full_key_from_signature(bound_args=bound_args, key=self._key)
|
|
159
159
|
|
|
@@ -170,14 +170,15 @@ class lock(AsyncLockMethods, SyncLockMethods):
|
|
|
170
170
|
|
|
171
171
|
return cast(AsyncLockWrappedF[_P, _R], cast(object, _async_wrapper))
|
|
172
172
|
else:
|
|
173
|
+
_sync_func = cast(Callable[_P, _R], _func)
|
|
173
174
|
|
|
174
|
-
@wraps(
|
|
175
|
-
def _sync_wrapper(*args: _P.args, **kwargs: _P.kwargs) ->
|
|
175
|
+
@wraps(_sync_func)
|
|
176
|
+
def _sync_wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R:
|
|
176
177
|
bound_args = signature.bind(*args, **kwargs)
|
|
177
178
|
_key = get_full_key_from_signature(bound_args=bound_args, key=self._key)
|
|
178
179
|
|
|
179
180
|
with lock(key=_key, nowait=self._nowait, timeout=self._timeout, exp=self._exp):
|
|
180
|
-
return
|
|
181
|
+
return _sync_func(*args, **kwargs)
|
|
181
182
|
|
|
182
183
|
setattr(_sync_wrapper, 'is_locked', partial(is_locked, signature=signature, key=self._key))
|
|
183
184
|
setattr(_sync_wrapper, 'release', partial(reset, signature=signature, key=self._key))
|
|
@@ -244,14 +245,14 @@ def once(key: str, raise_on_locked: bool = False, return_on_locked: Any = None)
|
|
|
244
245
|
|
|
245
246
|
def _once_inner(
|
|
246
247
|
_func: Union[Callable[_P, _R], Callable[_P, Awaitable[_R]]],
|
|
247
|
-
) -> Union[
|
|
248
|
+
) -> Union[AsyncLockWrappedF[_P, _R], SyncLockWrappedF[_P, _R]]:
|
|
248
249
|
signature = inspect.signature(_func)
|
|
249
250
|
|
|
250
251
|
if is_coroutine(_func):
|
|
251
252
|
_awaitable_func = _func
|
|
252
253
|
|
|
253
254
|
@wraps(_awaitable_func)
|
|
254
|
-
async def _async_wrapper(*args: _P.args, **kwargs: _P.kwargs) ->
|
|
255
|
+
async def _async_wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R:
|
|
255
256
|
bound_args = signature.bind(*args, **kwargs)
|
|
256
257
|
_key = get_full_key_from_signature(bound_args=bound_args, key=key)
|
|
257
258
|
|
|
@@ -262,7 +263,7 @@ def once(key: str, raise_on_locked: bool = False, return_on_locked: Any = None)
|
|
|
262
263
|
if raise_on_locked:
|
|
263
264
|
raise
|
|
264
265
|
|
|
265
|
-
return return_on_locked
|
|
266
|
+
return return_on_locked # type: ignore[no-any-return]
|
|
266
267
|
|
|
267
268
|
setattr(_async_wrapper, 'release', partial(a_reset, signature=signature, key=key))
|
|
268
269
|
setattr(_async_wrapper, 'is_locked', partial(is_alocked, signature=signature, key=key))
|
|
@@ -270,15 +271,16 @@ def once(key: str, raise_on_locked: bool = False, return_on_locked: Any = None)
|
|
|
270
271
|
return cast(AsyncLockWrappedF[_P, _R], cast(object, _async_wrapper))
|
|
271
272
|
|
|
272
273
|
else:
|
|
274
|
+
_sync_func = cast(Callable[_P, _R], _func)
|
|
273
275
|
|
|
274
|
-
@wraps(
|
|
275
|
-
def _sync_wrapper(*args: _P.args, **kwargs: _P.kwargs) ->
|
|
276
|
+
@wraps(_sync_func)
|
|
277
|
+
def _sync_wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R:
|
|
276
278
|
bound_args = signature.bind(*args, **kwargs)
|
|
277
279
|
_key = get_full_key_from_signature(bound_args=bound_args, key=key)
|
|
278
280
|
|
|
279
281
|
try:
|
|
280
282
|
with lock(key=_key):
|
|
281
|
-
return
|
|
283
|
+
return _sync_func(*args, **kwargs)
|
|
282
284
|
except CachifyLockError:
|
|
283
285
|
if raise_on_locked:
|
|
284
286
|
raise
|
|
@@ -29,10 +29,10 @@ class SyncLockWrappedF(Protocol[_P, _R]):
|
|
|
29
29
|
|
|
30
30
|
class WrappedFunctionLock(Protocol):
|
|
31
31
|
@overload
|
|
32
|
-
def __call__(self, _func: Callable[_P, Awaitable[_R]]
|
|
32
|
+
def __call__(self, _func: Callable[_P, Awaitable[_R]]) -> AsyncLockWrappedF[_P, _R]: ... # type: ignore[overload-overlap]
|
|
33
33
|
|
|
34
34
|
@overload
|
|
35
|
-
def __call__(self, _func: Callable[_P, _R]
|
|
35
|
+
def __call__(self, _func: Callable[_P, _R]) -> SyncLockWrappedF[_P, _R]: ...
|
|
36
36
|
|
|
37
37
|
def __call__( # pragma: no cover
|
|
38
38
|
self,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "py-cachify"
|
|
3
|
-
version = "2.0.
|
|
3
|
+
version = "2.0.1"
|
|
4
4
|
homepage = "https://github.com/EzyGang/py-cachify"
|
|
5
5
|
repository = "https://github.com/EzyGang/py-cachify"
|
|
6
6
|
license = "MIT"
|
|
@@ -28,7 +28,10 @@ classifiers = [
|
|
|
28
28
|
"Programming Language :: Python :: 3.12",
|
|
29
29
|
"License :: OSI Approved :: MIT License",
|
|
30
30
|
]
|
|
31
|
-
packages = [
|
|
31
|
+
packages = [
|
|
32
|
+
{ include = 'py_cachify' },
|
|
33
|
+
{ include = 'py_cachify/py.typed'},
|
|
34
|
+
]
|
|
32
35
|
|
|
33
36
|
[project]
|
|
34
37
|
requires-python = ">=3.8,<4.0"
|
|
@@ -44,7 +47,7 @@ pytest-cov = "^5.0.0"
|
|
|
44
47
|
taskipy = "^1.13.0"
|
|
45
48
|
mypy = "^1.11.1"
|
|
46
49
|
coverage = "^7.6.1"
|
|
47
|
-
pytest-asyncio = "^0.
|
|
50
|
+
pytest-asyncio = "^0.24.0"
|
|
48
51
|
pytest-socket = "^0.7.0"
|
|
49
52
|
ruff = "^0.6.2"
|
|
50
53
|
|
|
@@ -118,30 +121,8 @@ keep-runtime-typing = true
|
|
|
118
121
|
[tool.mypy]
|
|
119
122
|
# Mypy configuration:
|
|
120
123
|
# https://mypy.readthedocs.io/en/latest/config_file.html
|
|
121
|
-
|
|
122
|
-
check_untyped_defs = true
|
|
123
|
-
disallow_untyped_decorators = true
|
|
124
|
-
disallow_any_generics = false
|
|
125
|
-
disallow_untyped_calls = true
|
|
126
|
-
disallow_untyped_defs = true
|
|
127
|
-
disallow_incomplete_defs = true
|
|
128
|
-
ignore_errors = false
|
|
129
|
-
ignore_missing_imports = false
|
|
130
|
-
implicit_reexport = true
|
|
131
|
-
local_partial_types = true
|
|
132
|
-
strict_optional = true
|
|
133
|
-
strict_equality = true
|
|
134
|
-
no_implicit_optional = true
|
|
135
|
-
warn_unused_ignores = true
|
|
136
|
-
warn_redundant_casts = true
|
|
137
|
-
warn_unused_configs = true
|
|
138
|
-
warn_unreachable = true
|
|
139
|
-
warn_no_return = false
|
|
124
|
+
strict = true
|
|
140
125
|
pretty = true
|
|
141
|
-
follow_imports = "silent"
|
|
142
|
-
follow_imports_for_stubs = true
|
|
143
|
-
show_error_codes = true
|
|
144
|
-
plugins = []
|
|
145
126
|
|
|
146
127
|
exclude = ['test_']
|
|
147
128
|
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
from ._backend._cached import async_cached, cached, sync_cached
|
|
2
|
-
from ._backend._exceptions import CachifyInitError, CachifyLockError
|
|
3
|
-
from ._backend._lib import init_cachify
|
|
4
|
-
from ._backend._lock import async_once, lock, once, sync_once
|
|
5
|
-
from ._backend._types._common import AsyncClient, Decoder, Encoder, SyncClient
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
try:
|
|
9
|
-
from importlib.metadata import version
|
|
10
|
-
|
|
11
|
-
__version__ = version('py-cachify')
|
|
12
|
-
except ModuleNotFoundError:
|
|
13
|
-
__version__ = f'No version available for {__name__}'
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
__all__ = [
|
|
17
|
-
'CachifyInitError',
|
|
18
|
-
'CachifyLockError',
|
|
19
|
-
'init_cachify',
|
|
20
|
-
'lock',
|
|
21
|
-
'Encoder',
|
|
22
|
-
'Decoder',
|
|
23
|
-
'cached',
|
|
24
|
-
'once',
|
|
25
|
-
'AsyncClient',
|
|
26
|
-
'SyncClient',
|
|
27
|
-
# The following are deprecated (and are just aliases)
|
|
28
|
-
'async_once',
|
|
29
|
-
'sync_once',
|
|
30
|
-
'async_cached',
|
|
31
|
-
'sync_cached',
|
|
32
|
-
]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|