mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 04:57:54 +00:00
Merge pull request #91 from cpbotha/fix-sync-for-cache-disabled-path
Fix sync for cache disabled path
This commit is contained in:
@@ -41,11 +41,13 @@ async def get_data(request: Request, response: Response):
|
|||||||
return pendulum.today()
|
return pendulum.today()
|
||||||
|
|
||||||
|
|
||||||
|
# Note: This function MUST be sync to demonstrate fastapi-cache's correct handling,
|
||||||
|
# i.e. running cached sync functions in threadpool just like FastAPI itself!
|
||||||
@app.get("/blocking")
|
@app.get("/blocking")
|
||||||
@cache(namespace="test", expire=10)
|
@cache(namespace="test", expire=10)
|
||||||
async def blocking():
|
def blocking():
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
return dict(ret=await get_ret())
|
return dict(ret=get_ret())
|
||||||
|
|
||||||
|
|
||||||
@app.get("/datetime")
|
@app.get("/datetime")
|
||||||
|
|||||||
@@ -62,13 +62,24 @@ def cache(
|
|||||||
nonlocal coder
|
nonlocal coder
|
||||||
nonlocal expire
|
nonlocal expire
|
||||||
nonlocal key_builder
|
nonlocal key_builder
|
||||||
|
|
||||||
|
async def ensure_async_func(*args, **kwargs):
|
||||||
|
"""Run cached sync functions in thread pool just like FastAPI."""
|
||||||
|
if inspect.iscoroutinefunction(func):
|
||||||
|
# async, return as is
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
else:
|
||||||
|
# sync, wrap in thread and return async
|
||||||
|
return run_in_threadpool(func, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
copy_kwargs = kwargs.copy()
|
copy_kwargs = kwargs.copy()
|
||||||
request = copy_kwargs.pop("request", None)
|
request = copy_kwargs.pop("request", None)
|
||||||
response = copy_kwargs.pop("response", None)
|
response = copy_kwargs.pop("response", None)
|
||||||
if (
|
if (
|
||||||
request and request.headers.get("Cache-Control") == "no-store"
|
request and request.headers.get("Cache-Control") == "no-store"
|
||||||
) or not FastAPICache.get_enable():
|
) or not FastAPICache.get_enable():
|
||||||
return await func(*args, **kwargs)
|
return await ensure_async_func(*args, **kwargs)
|
||||||
|
|
||||||
coder = coder or FastAPICache.get_coder()
|
coder = coder or FastAPICache.get_coder()
|
||||||
expire = expire or FastAPICache.get_expire()
|
expire = expire or FastAPICache.get_expire()
|
||||||
@@ -82,12 +93,13 @@ def cache(
|
|||||||
if not request:
|
if not request:
|
||||||
if ret is not None:
|
if ret is not None:
|
||||||
return coder.decode(ret)
|
return coder.decode(ret)
|
||||||
ret = await func(*args, **kwargs)
|
ret = await ensure_async_func(*args, **kwargs)
|
||||||
await backend.set(cache_key, coder.encode(ret), expire or FastAPICache.get_expire())
|
await backend.set(cache_key, coder.encode(ret), expire or FastAPICache.get_expire())
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
if request.method != "GET":
|
if request.method != "GET":
|
||||||
return await func(request, *args, **kwargs)
|
return await ensure_async_func(request, *args, **kwargs)
|
||||||
|
|
||||||
if_none_match = request.headers.get("if-none-match")
|
if_none_match = request.headers.get("if-none-match")
|
||||||
if ret is not None:
|
if ret is not None:
|
||||||
if response:
|
if response:
|
||||||
@@ -102,10 +114,8 @@ def cache(
|
|||||||
kwargs.pop("request")
|
kwargs.pop("request")
|
||||||
if not response_param:
|
if not response_param:
|
||||||
kwargs.pop("response")
|
kwargs.pop("response")
|
||||||
if inspect.iscoroutinefunction(func):
|
|
||||||
ret = await func(*args, **kwargs)
|
ret = await ensure_async_func(*args, **kwargs)
|
||||||
else:
|
|
||||||
ret = await run_in_threadpool(func, *args, **kwargs)
|
|
||||||
|
|
||||||
await backend.set(cache_key, coder.encode(ret), expire or FastAPICache.get_expire())
|
await backend.set(cache_key, coder.encode(ret), expire or FastAPICache.get_expire())
|
||||||
return ret
|
return ret
|
||||||
|
|||||||
Reference in New Issue
Block a user