Decorators¶
Synchronous and asynchronous caching decorators, plus cache invalidation and cached property.
缓存装饰器(同步函数)
自动缓存函数返回值,提升性能。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manager
|
CacheManager
|
缓存管理器实例 |
required |
ttl
|
int | None
|
缓存过期时间(秒),None 表示永不过期 |
None
|
key_builder
|
Callable[[Callable[..., Any], tuple[Any, ...], dict[str, Any]], str] | None
|
自定义键生成函数 |
None
|
key_prefix
|
str
|
键前缀(用于命名空间隔离) |
''
|
Returns:
| Type | Description |
|---|---|
Callable[[F], F]
|
装饰后的函数 |
示例: >>> @cache(manager, ttl=3600, key_prefix="user:") >>> def get_user(user_id: int): ... return db.query(User).get(user_id) >>> >>> user = get_user(123) # 缓存 1 小时
Source code in src/symphra_cache/decorators.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
缓存装饰器(异步函数)
异步版本的缓存装饰器,支持 async/await 函数。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manager
|
CacheManager
|
缓存管理器实例 |
required |
ttl
|
int | None
|
缓存过期时间(秒) |
None
|
key_builder
|
Callable[[Callable[..., Any], tuple[Any, ...], dict[str, Any]], str] | None
|
自定义键生成函数 |
None
|
key_prefix
|
str
|
键前缀 |
''
|
Returns:
| Type | Description |
|---|---|
Callable[[AsyncF], AsyncF]
|
装饰后的异步函数 |
示例: >>> @acache(manager, ttl=600) >>> async def fetch_data(api_url: str): ... async with httpx.AsyncClient() as client: ... response = await client.get(api_url) ... return response.json() >>> >>> data = await fetch_data("https://api.example.com/users")
Source code in src/symphra_cache/decorators.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
缓存失效装饰器
在函数执行后,删除对应的缓存。 常用于更新操作(如 update_user 后清除 get_user 缓存)。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manager
|
CacheManager
|
缓存管理器 |
required |
key_builder
|
Callable[[Callable[..., Any], tuple[Any, ...], dict[str, Any]], str] | None
|
键生成函数(需与 @cache 一致) |
None
|
key_prefix
|
str
|
键前缀 |
''
|
Returns:
| Type | Description |
|---|---|
Callable[[F], F]
|
装饰后的函数 |
示例: >>> @cache(manager, key_prefix="user:") >>> def get_user(user_id: int): ... return db.query(User).get(user_id) >>> >>> @cache_invalidate(manager, key_prefix="user:") >>> def update_user(user_id: int, **updates): ... db.query(User).filter_by(id=user_id).update(updates) ... db.commit() >>> >>> get_user(123) # 缓存结果 >>> update_user(123, name="Bob") # 清除缓存 >>> get_user(123) # 重新查询数据库
Source code in src/symphra_cache/decorators.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
缓存属性装饰器
类似于 functools.cached_property,但使用外部缓存后端。 适用于需要在多个实例间共享缓存的场景。
示例: >>> class User: ... def init(self, user_id): ... self.user_id = user_id ... ... @CachedProperty(manager, ttl=600) ... def profile(self): ... # 数据库查询 ... return db.query(Profile).get(self.user_id) >>> >>> user = User(123) >>> profile = user.profile # 第一次:查询数据库 >>> profile = user.profile # 第二次:从缓存获取
Source code in src/symphra_cache/decorators.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
__call__(func)
¶
设置被装饰的方法
Source code in src/symphra_cache/decorators.py
316 317 318 319 | |
__get__(instance, owner=None)
¶
描述器协议:获取属性值
Source code in src/symphra_cache/decorators.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
__init__(manager, ttl=None, key_prefix='')
¶
初始化缓存属性
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manager
|
CacheManager
|
缓存管理器 |
required |
ttl
|
int | None
|
缓存过期时间 |
None
|
key_prefix
|
str
|
键前缀 |
''
|
Source code in src/symphra_cache/decorators.py
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |