2021-03-20 14:42:29 +08:00
|
|
|
import hashlib
|
2020-10-16 16:55:33 +08:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
|
|
from starlette.requests import Request
|
|
|
|
|
from starlette.responses import Response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def default_key_builder(
|
|
|
|
|
func,
|
|
|
|
|
namespace: Optional[str] = "",
|
2021-01-06 10:34:30 +08:00
|
|
|
request: Optional[Request] = None,
|
|
|
|
|
response: Optional[Response] = None,
|
|
|
|
|
args: Optional[tuple] = None,
|
|
|
|
|
kwargs: Optional[dict] = None,
|
2020-10-16 16:55:33 +08:00
|
|
|
):
|
|
|
|
|
from fastapi_cache import FastAPICache
|
|
|
|
|
|
2021-03-20 14:42:29 +08:00
|
|
|
prefix = f"{FastAPICache.get_prefix()}:{namespace}:"
|
|
|
|
|
cache_key = (
|
2021-03-20 14:47:46 +08:00
|
|
|
prefix
|
|
|
|
|
+ hashlib.md5( # nosec:B303
|
|
|
|
|
f"{func.__module__}:{func.__name__}:{args}:{kwargs}"
|
|
|
|
|
).hexdigest()
|
2021-03-20 14:42:29 +08:00
|
|
|
)
|
2020-10-16 16:55:33 +08:00
|
|
|
return cache_key
|