mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 13:07:53 +00:00
Factor out support for optional request / response
This commit is contained in:
@@ -65,6 +65,13 @@ def cache(
|
|||||||
|
|
||||||
async def ensure_async_func(*args, **kwargs):
|
async def ensure_async_func(*args, **kwargs):
|
||||||
"""Run cached sync functions in thread pool just like FastAPI."""
|
"""Run cached sync functions in thread pool just like FastAPI."""
|
||||||
|
# if the wrapped function does NOT have request or response in its function signature,
|
||||||
|
# make sure we don't pass them in as keyword arguments
|
||||||
|
if not request_param:
|
||||||
|
kwargs.pop("request")
|
||||||
|
if not response_param:
|
||||||
|
kwargs.pop("response")
|
||||||
|
|
||||||
if inspect.iscoroutinefunction(func):
|
if inspect.iscoroutinefunction(func):
|
||||||
# async, return as is.
|
# async, return as is.
|
||||||
# unintuitively, we have to await once here, so that caller
|
# unintuitively, we have to await once here, so that caller
|
||||||
@@ -114,10 +121,6 @@ def cache(
|
|||||||
return response
|
return response
|
||||||
response.headers["ETag"] = etag
|
response.headers["ETag"] = etag
|
||||||
return coder.decode(ret)
|
return coder.decode(ret)
|
||||||
if not request_param:
|
|
||||||
kwargs.pop("request")
|
|
||||||
if not response_param:
|
|
||||||
kwargs.pop("response")
|
|
||||||
|
|
||||||
ret = await ensure_async_func(*args, **kwargs)
|
ret = await ensure_async_func(*args, **kwargs)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user