mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 04:57:54 +00:00
Inject dependencies using a namespace
Instead of assuming that the Request and Response injected keyword arguments can be named `request` and `response`, use namespaced keyword names starting with a double underscore. By default the parameter names now start with `__fastapi_cache_` and so are a) clearly marked as internal, and b) highly unlikely to clash with existing keyword arguments. The prefix is configurable in the unlikely event that the names would clash in specific cases.
This commit is contained in:
@@ -106,6 +106,17 @@ async def uncached_put():
|
||||
return {"value": put_ret}
|
||||
|
||||
|
||||
@app.get("/namespaced_injection")
|
||||
@cache(namespace="test", expire=5, injected_dependency_namespace="monty_python")
|
||||
def namespaced_injection(
|
||||
__fastapi_cache_request: int = 42, __fastapi_cache_response: int = 17
|
||||
) -> dict[str, int]:
|
||||
return {
|
||||
"__fastapi_cache_request": __fastapi_cache_request,
|
||||
"__fastapi_cache_response": __fastapi_cache_response,
|
||||
}
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
FastAPICache.init(InMemoryBackend())
|
||||
|
||||
Reference in New Issue
Block a user