@@ -84,25 +84,47 @@ async def inner_wrapper(*args, **kwargs):
84
84
85
85
def standard_wrapper (func ):
86
86
@wraps (func )
87
- async def inner_wrapper (* args , ** kwargs ):
87
+ async def inner_wrapper_async (* args , ** kwargs ):
88
88
"""Return cached value if one exists, otherwise evaluate the wrapped function and cache the result."""
89
89
90
90
func_kwargs = kwargs .copy ()
91
91
redis_cache = FastApiRedisCache ()
92
92
if redis_cache .not_connected :
93
93
# if the redis client is not connected or request is not cacheable, no caching behavior is performed.
94
- return await get_api_response_async ( func , * args , ** kwargs )
94
+ return await func ( * args , ** kwargs )
95
95
key = redis_cache .get_cache_key (func , * args , ** kwargs )
96
96
ttl , in_cache = redis_cache .check_cache (key )
97
97
if in_cache :
98
98
return deserialize_json (in_cache )
99
99
100
- response_data = await get_api_response_async ( func , * args , ** kwargs )
100
+ response_data = await func ( * args , ** kwargs )
101
101
ttl = calculate_ttl (expire )
102
102
redis_cache .add_to_cache (key , response_data , ttl )
103
103
return response_data
104
104
105
- return inner_wrapper
105
+ @wraps (func )
106
+ def inner_wrapper_sync (* args , ** kwargs ):
107
+ """Return cached value if one exists, otherwise evaluate the wrapped function and cache the result."""
108
+
109
+ func_kwargs = kwargs .copy ()
110
+ redis_cache = FastApiRedisCache ()
111
+ if redis_cache .not_connected :
112
+ # if the redis client is not connected or request is not cacheable, no caching behavior is performed.
113
+ return func (* args , ** kwargs )
114
+ key = redis_cache .get_cache_key (func , * args , ** kwargs )
115
+ ttl , in_cache = redis_cache .check_cache (key )
116
+ if in_cache :
117
+ return deserialize_json (in_cache )
118
+
119
+ response_data = func (* args , ** kwargs )
120
+ ttl = calculate_ttl (expire )
121
+ redis_cache .add_to_cache (key , response_data , ttl )
122
+ return response_data
123
+
124
+ if asyncio .iscoroutinefunction (func ):
125
+ return inner_wrapper_async
126
+ else :
127
+ return inner_wrapper_sync
106
128
107
129
if fastapi_route :
108
130
return fastapi_route_wrapper
0 commit comments