mirror of https://github.com/tiangolo/fastapi.git
perf(dependencies): skip doing any work when sub dependant is already cached
This commit is contained in:
parent
7e05bff3d4
commit
0264bcbe80
|
|
@ -590,6 +590,17 @@ async def solve_dependencies(
|
|||
dependency_cache = {}
|
||||
for sub_dependant in dependant.dependencies:
|
||||
sub_dependant.call = cast(Callable[..., Any], sub_dependant.call)
|
||||
|
||||
if sub_dependant.use_cache:
|
||||
# Use a unique object to compare against in case the cached value is None
|
||||
cache_miss = object()
|
||||
cached_value = dependency_cache.get(sub_dependant.cache_key, cache_miss)
|
||||
# If the sub dependant is already cached, skip doing any more work
|
||||
if cached_value is not cache_miss:
|
||||
if sub_dependant.name is not None:
|
||||
values[sub_dependant.name] = cached_value
|
||||
continue
|
||||
|
||||
call = sub_dependant.call
|
||||
use_sub_dependant = sub_dependant
|
||||
if (
|
||||
|
|
@ -624,11 +635,7 @@ async def solve_dependencies(
|
|||
if solved_result.errors:
|
||||
errors.extend(solved_result.errors)
|
||||
continue
|
||||
if sub_dependant.use_cache and sub_dependant.cache_key in dependency_cache:
|
||||
solved = dependency_cache[sub_dependant.cache_key]
|
||||
elif (
|
||||
use_sub_dependant.is_gen_callable or use_sub_dependant.is_async_gen_callable
|
||||
):
|
||||
if use_sub_dependant.is_gen_callable or use_sub_dependant.is_async_gen_callable:
|
||||
use_astack = request_astack
|
||||
if sub_dependant.scope == "function":
|
||||
use_astack = function_astack
|
||||
|
|
|
|||
Loading…
Reference in New Issue