1+ import logging
12from fastapi_cache import FastAPICache
23from fastapi_cache .decorator import cache as original_cache
34from functools import partial , wraps
45from typing import Optional , Set , Any , Dict , Tuple , Callable
56from inspect import Parameter , signature
6- import logging
77from contextlib import asynccontextmanager
88import asyncio
99
10- logger = logging .getLogger (__name__ )
1110
1211# 锁管理
1312_cache_locks = {}
@@ -66,7 +65,7 @@ def custom_key_builder(
6665 base_key += f"{ value } :"
6766
6867 except (IndexError , KeyError , AttributeError ) as e :
69- logger .warning (f"Failed to evaluate keyExpression '{ keyExpression } ': { str (e )} " )
68+ logging .warning (f"Failed to evaluate keyExpression '{ keyExpression } ': { str (e )} " )
7069
7170 return base_key
7271
@@ -128,13 +127,19 @@ async def wrapper(*args, **kwargs):
128127 )
129128
130129 async with _get_cache_lock (cache_key ):
130+ logging .info (f"Using cache key: { cache_key } " )
131+ print (f"Using cache key: { cache_key } " )
131132 backend = FastAPICache .get_backend ()
132133 cached_value = await backend .get (cache_key )
133134 if cached_value is not None :
135+ logging .info (f"Cache hit for key: { cache_key } , the value is: { cached_value } " )
136+ print (f"Cache hit for key: { cache_key } , the value is: { cached_value } " )
134137 return cached_value
135138
136139 result = await func (* args , ** kwargs )
137140 await backend .set (cache_key , result , expire )
141+ logging .info (f"Cache miss for key: { cache_key } , result cached." )
142+ print (f"Cache miss for key: { cache_key } , result cached." )
138143 return result
139144
140145 return wrapper
@@ -161,6 +166,8 @@ async def wrapper(*args, **kwargs):
161166 async with _get_cache_lock (cache_key ):
162167 await FastAPICache .clear (key = cache_key )
163168 result = await func (* args , ** kwargs )
169+ logging .info (f"Clearing cache for key: { cache_key } " )
170+ print (f"Clearing cache for key: { cache_key } " )
164171 return result
165172
166173 return wrapper
0 commit comments