mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 04:57:54 +00:00
Simplify key_builder calling
The keybuilder is either returning a string, or a coroutine or other awaitable. If the latter, await on the return value to get the string.
This commit is contained in:
@@ -116,16 +116,6 @@ def cache(
|
|||||||
key_builder = key_builder or FastAPICache.get_key_builder()
|
key_builder = key_builder or FastAPICache.get_key_builder()
|
||||||
backend = FastAPICache.get_backend()
|
backend = FastAPICache.get_backend()
|
||||||
|
|
||||||
if inspect.iscoroutinefunction(key_builder):
|
|
||||||
cache_key = await key_builder(
|
|
||||||
func,
|
|
||||||
namespace,
|
|
||||||
request=request,
|
|
||||||
response=response,
|
|
||||||
args=args,
|
|
||||||
kwargs=copy_kwargs,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
cache_key = key_builder(
|
cache_key = key_builder(
|
||||||
func,
|
func,
|
||||||
namespace,
|
namespace,
|
||||||
@@ -134,6 +124,9 @@ def cache(
|
|||||||
args=args,
|
args=args,
|
||||||
kwargs=copy_kwargs,
|
kwargs=copy_kwargs,
|
||||||
)
|
)
|
||||||
|
if inspect.isawaitable(cache_key):
|
||||||
|
cache_key = await cache_key
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ttl, ret = await backend.get_with_ttl(cache_key)
|
ttl, ret = await backend.get_with_ttl(cache_key)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
Reference in New Issue
Block a user