Merge pull request #135 from mjpieters/refactor_prefix

Make decorator responsibe for applying the prefix
This commit is contained in:
long2ice
2023-05-11 09:25:57 +08:00
committed by GitHub
2 changed files with 6 additions and 11 deletions

View File

@@ -121,6 +121,7 @@ def cache(
) or not FastAPICache.get_enable():
return await ensure_async_func(*args, **kwargs)
prefix = FastAPICache.get_prefix()
coder = coder or FastAPICache.get_coder()
expire = expire or FastAPICache.get_expire()
key_builder = key_builder or FastAPICache.get_key_builder()
@@ -128,7 +129,7 @@ def cache(
cache_key = key_builder(
func,
namespace,
f"{prefix}:{namespace}",
request=request,
response=response,
args=args,

View File

@@ -13,13 +13,7 @@ def default_key_builder(
args: Optional[tuple[Any, ...]] = None,
kwargs: Optional[dict[str, Any]] = None,
) -> str:
from fastapi_cache import FastAPICache
prefix = f"{FastAPICache.get_prefix()}:{namespace}:"
cache_key = (
prefix
+ hashlib.md5( # nosec:B303
f"{func.__module__}:{func.__name__}:{args}:{kwargs}".encode()
).hexdigest()
)
return cache_key
cache_key = hashlib.md5( # nosec:B303
f"{func.__module__}:{func.__name__}:{args}:{kwargs}".encode()
).hexdigest()
return f"{namespace}:{cache_key}"