-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
118 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,80 @@ | ||
from __future__ import annotations | ||
|
||
import inspect | ||
from functools import partial, wraps | ||
from typing import Awaitable, Callable, Optional, Tuple, TypeVar, Union, cast | ||
from functools import wraps | ||
from typing import Awaitable, Callable, Tuple, TypeVar, Union, cast, overload | ||
|
||
from typing_extensions import ParamSpec | ||
from typing_extensions import ParamSpec, deprecated | ||
|
||
from py_cachify.backend.lib import get_cachify | ||
|
||
from .helpers import Decoder, Encoder, encode_decode_value, get_full_key_from_signature, is_coroutine | ||
from .helpers import Decoder, Encoder, SyncOrAsync, encode_decode_value, get_full_key_from_signature, is_coroutine | ||
|
||
|
||
R = TypeVar('R') | ||
P = ParamSpec('P') | ||
|
||
|
||
def _decorator( | ||
_func: Union[Callable[P, R], Callable[P, Awaitable[R]]], | ||
key: str, | ||
ttl: Union[int, None] = None, | ||
enc_dec: Optional[Tuple[Encoder, Decoder]] = None, | ||
) -> Union[Callable[P, R], Callable[P, Awaitable[R]]]: | ||
signature = inspect.signature(_func) | ||
def cached(key: str, ttl: Union[int, None] = None, enc_dec: Union[Tuple[Encoder, Decoder], None] = None) -> SyncOrAsync: | ||
@overload | ||
def _cached_inner( | ||
_func: Callable[P, Awaitable[R]], | ||
) -> Callable[P, Awaitable[R]]: ... | ||
|
||
enc, dec = None, None | ||
if enc_dec is not None: | ||
enc, dec = enc_dec | ||
@overload | ||
def _cached_inner( | ||
_func: Callable[P, R], | ||
) -> Callable[P, R]: ... | ||
|
||
if is_coroutine(_func): | ||
_awaitable_func = _func | ||
def _cached_inner( # type: ignore[misc] | ||
_func: Union[Callable[P, R], Callable[P, Awaitable[R]]], | ||
) -> Union[Callable[P, R], Callable[P, Awaitable[R]]]: | ||
signature = inspect.signature(_func) | ||
|
||
@wraps(_awaitable_func) | ||
async def _async_wrapper(*args: P.args, **kwargs: P.kwargs) -> R: | ||
cachify = get_cachify() | ||
_key = get_full_key_from_signature(bound_args=signature.bind(*args, **kwargs), key=key) | ||
if val := await cachify.a_get(key=_key): | ||
return encode_decode_value(encoder_decoder=dec, val=val) | ||
enc, dec = None, None | ||
if enc_dec is not None: | ||
enc, dec = enc_dec | ||
|
||
res = await _awaitable_func(*args, **kwargs) | ||
await cachify.a_set(key=_key, val=encode_decode_value(encoder_decoder=enc, val=res), ttl=ttl) | ||
return res | ||
if is_coroutine(_func): | ||
_awaitable_func = _func | ||
|
||
return cast(Callable[P, Awaitable[R]], _async_wrapper) | ||
else: | ||
@wraps(_awaitable_func) | ||
async def _async_wrapper(*args: P.args, **kwargs: P.kwargs) -> R: | ||
cachify = get_cachify() | ||
_key = get_full_key_from_signature(bound_args=signature.bind(*args, **kwargs), key=key) | ||
if val := await cachify.a_get(key=_key): | ||
return encode_decode_value(encoder_decoder=dec, val=val) | ||
|
||
@wraps(_func) | ||
def _sync_wrapper(*args: P.args, **kwargs: P.kwargs) -> R: | ||
cachify = get_cachify() | ||
_key = get_full_key_from_signature(bound_args=signature.bind(*args, **kwargs), key=key) | ||
if val := cachify.get(key=_key): | ||
return encode_decode_value(encoder_decoder=dec, val=val) | ||
res = await _awaitable_func(*args, **kwargs) | ||
await cachify.a_set(key=_key, val=encode_decode_value(encoder_decoder=enc, val=res), ttl=ttl) | ||
return res | ||
|
||
res = _func(*args, **kwargs) | ||
cachify.set(key=_key, val=encode_decode_value(encoder_decoder=enc, val=res), ttl=ttl) | ||
return cast(R, res) | ||
return _async_wrapper | ||
else: | ||
|
||
return cast(Callable[P, R], _sync_wrapper) | ||
@wraps(_func) # type: ignore[unreachable] | ||
def _sync_wrapper(*args: P.args, **kwargs: P.kwargs) -> R: | ||
cachify = get_cachify() | ||
_key = get_full_key_from_signature(bound_args=signature.bind(*args, **kwargs), key=key) | ||
if val := cachify.get(key=_key): | ||
return encode_decode_value(encoder_decoder=dec, val=val) | ||
|
||
res = _func(*args, **kwargs) | ||
cachify.set(key=_key, val=encode_decode_value(encoder_decoder=enc, val=res), ttl=ttl) | ||
return cast(R, res) | ||
|
||
def cached( | ||
key: str, ttl: Union[int, None] = None, enc_dec: Union[Tuple[Encoder, Decoder], None] = None | ||
) -> Callable[[Union[Callable[P, Awaitable[R]], Callable[P, R]]], Union[Callable[P, Awaitable[R]], Callable[P, R]]]: | ||
return cast( | ||
Callable[[Union[Callable[P, Awaitable[R]], Callable[P, R]]], Union[Callable[P, Awaitable[R]], Callable[P, R]]], | ||
partial(_decorator, key=key, ttl=ttl, enc_dec=enc_dec), | ||
) | ||
return _sync_wrapper | ||
|
||
return _cached_inner | ||
|
||
|
||
@deprecated('sync_cached is deprecated, use cached instead. Scheduled for removal in 1.3.0') | ||
def sync_cached( | ||
key: str, ttl: Union[int, None] = None, enc_dec: Union[Tuple[Encoder, Decoder], None] = None | ||
) -> Callable[[Callable[P, R]], Callable[P, R]]: | ||
return cast(Callable[[Callable[P, R]], Callable[P, R]], partial(_decorator, key=key, ttl=ttl, enc_dec=enc_dec)) | ||
return cached(key=key, ttl=ttl, enc_dec=enc_dec) | ||
|
||
|
||
@deprecated('async_cached is deprecated, use cached instead. Scheduled for removal in 1.3.0') | ||
def async_cached( | ||
key: str, ttl: Union[int, None] = None, enc_dec: Union[Tuple[Encoder, Decoder], None] = None | ||
) -> Callable[[Callable[P, Awaitable[R]]], Callable[P, Awaitable[R]]]: | ||
return cast( | ||
Callable[[Callable[P, Awaitable[R]]], Callable[P, Awaitable[R]]], | ||
partial(_decorator, key=key, ttl=ttl, enc_dec=enc_dec), | ||
) | ||
return cached(key=key, ttl=ttl, enc_dec=enc_dec) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters