Skip to content

Dependency Caching

Xpresso has a dependency caching system. This allows re-using of already computed dependencies within a request response cycle. This is also what enables Xpresso to persist "app" scoped dependencies across requests (see Scopes). By default, all dependencies are cached within their execution scope, but this can be disabled on a per-dependency basis with the use_cache argument to Dependant.

First we are going to declare a placeholder dependency with no sub-dependencies. We are just going to compare instances, so there's nothing else needed in this dependency.

from xpresso import App, Dependant, Path
from xpresso.typing import Annotated


class SharedDependency:
    pass


def dependency_1(shared: SharedDependency) -> SharedDependency:
    return shared


def dependency_2(shared: SharedDependency) -> SharedDependency:
    return shared


async def endpoint(
    shared1: Annotated[SharedDependency, Dependant(dependency_1)],
    shared2: Annotated[SharedDependency, Dependant(dependency_1)],
    shared3: Annotated[
        SharedDependency, Dependant(SharedDependency, use_cache=False)
    ],
) -> None:
    assert shared1 is shared2
    assert shared1 is not shared3


app = App(
    routes=[
        Path(
            "/shared",
            get=endpoint,
        )
    ]
)

Next we'll create two dependencies that depend on this dependency to test that sub-dependencies are shared:

from xpresso import App, Dependant, Path
from xpresso.typing import Annotated


class SharedDependency:
    pass


def dependency_1(shared: SharedDependency) -> SharedDependency:
    return shared


def dependency_2(shared: SharedDependency) -> SharedDependency:
    return shared


async def endpoint(
    shared1: Annotated[SharedDependency, Dependant(dependency_1)],
    shared2: Annotated[SharedDependency, Dependant(dependency_1)],
    shared3: Annotated[
        SharedDependency, Dependant(SharedDependency, use_cache=False)
    ],
) -> None:
    assert shared1 is shared2
    assert shared1 is not shared3


app = App(
    routes=[
        Path(
            "/shared",
            get=endpoint,
        )
    ]
)

Finally we create an endpoint that checks that the shared sub-dependencies are the same but the dependency declared with use_cache=False is not the same:

from xpresso import App, Dependant, Path
from xpresso.typing import Annotated


class SharedDependency:
    pass


def dependency_1(shared: SharedDependency) -> SharedDependency:
    return shared


def dependency_2(shared: SharedDependency) -> SharedDependency:
    return shared


async def endpoint(
    shared1: Annotated[SharedDependency, Dependant(dependency_1)],
    shared2: Annotated[SharedDependency, Dependant(dependency_1)],
    shared3: Annotated[
        SharedDependency, Dependant(SharedDependency, use_cache=False)
    ],
) -> None:
    assert shared1 is shared2
    assert shared1 is not shared3


app = App(
    routes=[
        Path(
            "/shared",
            get=endpoint,
        )
    ]
)

You can test this by running the app and navigating to http://127.0.0.1:800/shared. You should get a 200 OK response with no errors.