|
| 1 | +from typing import ( |
| 2 | + Callable, |
| 3 | + Concatenate, |
| 4 | + Mapping, |
| 5 | + ParamSpec, |
| 6 | + Protocol, |
| 7 | + Sequence, |
| 8 | + Type, |
| 9 | + TypeVar, |
| 10 | + overload, |
| 11 | +) |
| 12 | + |
| 13 | +from aiocache import BaseCache, Cache |
| 14 | +from aiocache.plugins import BasePlugin |
| 15 | +from aiocache.serializers import BaseSerializer |
| 16 | + |
| 17 | +Params = ParamSpec("Params") |
| 18 | +ReturnType = TypeVar("ReturnType") |
| 19 | +DecoratorKWArgs = TypeVar("DecoratorKWArgs") |
| 20 | +SerializerType = TypeVar("SerializerType", bound=BaseSerializer) |
| 21 | +CacheType = TypeVar("CacheType", bound=BaseCache) |
| 22 | +MCReturnType = TypeVar("MCReturnType", bound=Mapping) |
| 23 | +MCKey = TypeVar("MCKey") |
| 24 | +MCVal = TypeVar("MCVal") |
| 25 | + |
| 26 | +class CachedBaseProtocol(Protocol[CacheType, Params, ReturnType, SerializerType, DecoratorKWArgs]): |
| 27 | + ttl: int | None |
| 28 | + namespace: str | None |
| 29 | + serializer: SerializerType | None |
| 30 | + plugins: Sequence[BasePlugin] | None |
| 31 | + alias: str | None |
| 32 | + |
| 33 | + cache: CacheType |
| 34 | + _cache: CacheType |
| 35 | + _serializer: SerializerType |
| 36 | + _namespace: str | None |
| 37 | + _plugins: Sequence[BasePlugin] | None |
| 38 | + _kwargs: dict[str, DecoratorKWArgs] |
| 39 | + |
| 40 | +class CachedDecorated( |
| 41 | + CachedBaseProtocol[CacheType, Params, ReturnType, SerializerType, DecoratorKWArgs] |
| 42 | +): |
| 43 | + key_builder: Callable[Params, str] | None |
| 44 | + skip_cache_func: Callable[[ReturnType], bool] | None |
| 45 | + noself: bool |
| 46 | + |
| 47 | + def __call__(self, *args: Params.args, **kwargs: Params.kwargs) -> ReturnType: ... |
| 48 | + def get_cache_key(self, *args: Params.args, **kwargs: Params.kwargs) -> str: ... |
| 49 | + async def get_from_cache(self, key: str) -> ReturnType | None: ... |
| 50 | + async def set_in_cache(self, key: str, value: ReturnType) -> None: ... |
| 51 | + |
| 52 | +class CachedStampedeDecorated( |
| 53 | + CachedDecorated[CacheType, Params, ReturnType, SerializerType, DecoratorKWArgs] |
| 54 | +): |
| 55 | + lease: int |
| 56 | + |
| 57 | +class MultiCachedDecorated( |
| 58 | + CachedBaseProtocol[ |
| 59 | + CacheType, Params, MCReturnType[MCKey, MCVal], SerializerType, DecoratorKWArgs |
| 60 | + ] |
| 61 | +): |
| 62 | + keys_from_attr: str |
| 63 | + key_builder: ( |
| 64 | + Callable[ |
| 65 | + Concatenate[ |
| 66 | + MCKey, |
| 67 | + MultiCachedDecorated[ |
| 68 | + CacheType, |
| 69 | + Params, |
| 70 | + MCReturnType[MCKey, MCVal], |
| 71 | + SerializerType, |
| 72 | + DecoratorKWArgs, |
| 73 | + ], |
| 74 | + Params, |
| 75 | + ], |
| 76 | + str, |
| 77 | + ] |
| 78 | + | None |
| 79 | + ) |
| 80 | + skip_cache_func: Callable[[MCKey, MCVal], bool] | None |
| 81 | + |
| 82 | + def __call__( |
| 83 | + self, |
| 84 | + *args: Params.args, |
| 85 | + cache_read: bool = True, |
| 86 | + cache_write: bool = True, |
| 87 | + aiocache_wait_for_write: bool = True, |
| 88 | + **kwargs: Params.kwargs, |
| 89 | + ) -> MCReturnType: ... |
| 90 | + def get_cache_keys( |
| 91 | + self, |
| 92 | + f: MultiCachedDecorated[ |
| 93 | + CacheType, Params, MCReturnType[MCKey, MCVal], SerializerType, DecoratorKWArgs |
| 94 | + ], |
| 95 | + *args: Params.args, |
| 96 | + **kwargs: Params.kwargs, |
| 97 | + ) -> str: ... |
| 98 | + async def get_from_cache(self, *keys: MCKey) -> list[MCVal | None]: ... |
| 99 | + async def set_in_cache( |
| 100 | + self, |
| 101 | + result: MCReturnType[MCKey, MCVal], |
| 102 | + fn: MultiCachedDecorated[ |
| 103 | + CacheType, Params, MCReturnType[MCKey, MCVal], SerializerType, DecoratorKWArgs |
| 104 | + ], |
| 105 | + fn_args: Params.args, |
| 106 | + fn_kwargs: Params.kwargs, |
| 107 | + ) -> None: ... |
| 108 | + |
| 109 | +@overload |
| 110 | +def cached( |
| 111 | + ttl: int | None = None, |
| 112 | + *, |
| 113 | + key_builder: Callable[Params, str] | None = None, |
| 114 | + skip_cache_func: Callable[[ReturnType], bool] | None = None, |
| 115 | + cache: Type[CacheType] = Cache.MEMORY, |
| 116 | + noself: bool = False, |
| 117 | + alias: str, |
| 118 | + **kwargs: DecoratorKWArgs, |
| 119 | +) -> CachedDecorated[CacheType, Params, ReturnType, None, DecoratorKWArgs]: ... |
| 120 | +@overload |
| 121 | +def cached( |
| 122 | + ttl: int | None = None, |
| 123 | + *, |
| 124 | + key_builder: Callable[Params, str] | None = None, |
| 125 | + skip_cache_func: Callable[[ReturnType], bool] | None = None, |
| 126 | + cache: Type[CacheType] = Cache.MEMORY, |
| 127 | + noself: bool = False, |
| 128 | + namespace: str | None = None, |
| 129 | + serializer: SerializerType | None = None, |
| 130 | + plugins: Sequence[BasePlugin] | None = None, |
| 131 | + alias: None = None, |
| 132 | + **kwargs: DecoratorKWArgs, |
| 133 | +) -> CachedDecorated[CacheType, Params, ReturnType, SerializerType, DecoratorKWArgs]: ... |
| 134 | +@overload |
| 135 | +def cached_stampede( |
| 136 | + lease: int = 2, |
| 137 | + ttl: int | None = None, |
| 138 | + *, |
| 139 | + key_builder: Callable[Params, str] | None = None, |
| 140 | + skip_cache_func: Callable[[ReturnType], bool] | None = None, |
| 141 | + cache: Type[CacheType] = Cache.MEMORY, |
| 142 | + noself: bool = False, |
| 143 | + alias: str, |
| 144 | + **kwargs: DecoratorKWArgs, |
| 145 | +) -> CachedStampedeDecorated[CacheType, Params, ReturnType, None, DecoratorKWArgs]: ... |
| 146 | +@overload |
| 147 | +def cached_stampede( |
| 148 | + lease: int = 2, |
| 149 | + ttl: int | None = None, |
| 150 | + *, |
| 151 | + key_builder: Callable[Params, str] | None = None, |
| 152 | + skip_cache_func: Callable[[ReturnType], bool] | None = None, |
| 153 | + cache: Type[CacheType] = Cache.MEMORY, |
| 154 | + noself: bool = False, |
| 155 | + namespace: str | None = None, |
| 156 | + serializer: SerializerType | None = None, |
| 157 | + plugins: Sequence[BasePlugin] | None = None, |
| 158 | + alias: None = None, |
| 159 | + **kwargs: DecoratorKWArgs, |
| 160 | +) -> CachedStampedeDecorated[CacheType, Params, ReturnType, SerializerType, DecoratorKWArgs]: ... |
| 161 | +@overload |
| 162 | +def multi_cached( |
| 163 | + keys_from_attr: str, |
| 164 | + *, |
| 165 | + key_builder: ( |
| 166 | + Callable[ |
| 167 | + Concatenate[ |
| 168 | + MCKey, |
| 169 | + MultiCachedDecorated[ |
| 170 | + CacheType, |
| 171 | + Params, |
| 172 | + MCReturnType[MCKey, MCVal], |
| 173 | + SerializerType, |
| 174 | + DecoratorKWArgs, |
| 175 | + ], |
| 176 | + Params, |
| 177 | + ], |
| 178 | + str, |
| 179 | + ] |
| 180 | + | None |
| 181 | + ) = None, |
| 182 | + skip_cache_func: Callable[[MCKey, MCVal], bool] | None = None, |
| 183 | + ttl: int | None = None, |
| 184 | + cache: Type[CacheType] = Cache.MEMORY, |
| 185 | + alias: str, |
| 186 | + **kwargs: DecoratorKWArgs, |
| 187 | +) -> MultiCachedDecorated[CacheType, Params, ReturnType, SerializerType, DecoratorKWArgs]: ... |
| 188 | +@overload |
| 189 | +def multi_cached( |
| 190 | + keys_from_attr: str, |
| 191 | + *, |
| 192 | + namespace: str | None = None, |
| 193 | + key_builder: ( |
| 194 | + Callable[ |
| 195 | + Concatenate[ |
| 196 | + MCKey, |
| 197 | + MultiCachedDecorated[ |
| 198 | + CacheType, |
| 199 | + Params, |
| 200 | + MCReturnType[MCKey, MCVal], |
| 201 | + SerializerType, |
| 202 | + DecoratorKWArgs, |
| 203 | + ], |
| 204 | + Params, |
| 205 | + ], |
| 206 | + str, |
| 207 | + ] |
| 208 | + | None |
| 209 | + ) = None, |
| 210 | + skip_cache_func: Callable[[MCKey, MCVal], bool] | None = None, |
| 211 | + ttl: int | None = None, |
| 212 | + cache: Type[CacheType] = Cache.MEMORY, |
| 213 | + serializer: SerializerType | None = None, |
| 214 | + plugins: Sequence[BasePlugin] | None = None, |
| 215 | + alias: None = None, |
| 216 | + **kwargs: DecoratorKWArgs, |
| 217 | +) -> MultiCachedDecorated[CacheType, Params, ReturnType, SerializerType, DecoratorKWArgs]: ... |
0 commit comments