Files
fastapi-cache/fastapi_cache/key_builder.py
Martijn Pieters 32acafa5e0 Correct type hint: namespace is not optional
The namespace argument is positional and will never be `None` so should
not be marked as Optional. It is always a string, and the default is
to pass in an empty string.
2023-04-27 16:11:59 +01:00

26 lines
659 B
Python

import hashlib
from typing import Callable, Optional
from starlette.requests import Request
from starlette.responses import Response
def default_key_builder(
func: Callable,
namespace: str = "",
request: Optional[Request] = None,
response: Optional[Response] = None,
args: Optional[tuple] = None,
kwargs: Optional[dict] = 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