Files
fastapi-cache/fastapi_cache/key_builder.py
Martijn Pieters d9965a45e5 Make decorator responsibe for applying the prefix
The key builder should not have to fetch the prefix separately, as this
makes creating custom key builders that much harder.
2023-05-10 18:59:59 +01:00

20 lines
569 B
Python

import hashlib
from typing import Any, Callable, Optional
from starlette.requests import Request
from starlette.responses import Response
def default_key_builder(
func: Callable[..., Any],
namespace: str = "",
request: Optional[Request] = None,
response: Optional[Response] = None,
args: Optional[tuple[Any, ...]] = None,
kwargs: Optional[dict[str, Any]] = None,
) -> str:
cache_key = hashlib.md5( # nosec:B303
f"{func.__module__}:{func.__name__}:{args}:{kwargs}".encode()
).hexdigest()
return f"{namespace}:{cache_key}"