1818
1919import logging
2020from contextlib import AsyncExitStack , asynccontextmanager
21+ from functools import cache
2122from typing import TYPE_CHECKING , cast
2223from urllib .parse import urlsplit
2324
5455
5556log = logging .getLogger (__name__ )
5657
57- app : FastAPI | None = None
58- auth_manager : BaseAuthManager | None = None
58+ _auth_manager : BaseAuthManager | None = None
5959
6060
6161@asynccontextmanager
@@ -107,19 +107,17 @@ def create_app(apps: str = "all") -> FastAPI:
107107 return app
108108
109109
110+ @cache
110111def cached_app (config = None , testing = False , apps = "all" ) -> FastAPI :
111112 """Return cached instance of Airflow API app."""
112- global app
113- if not app :
114- app = create_app (apps = apps )
115- return app
113+ return create_app (apps = apps )
116114
117115
118116def purge_cached_app () -> None :
119117 """Remove the cached version of the app and auth_manager in global state."""
120- global app , auth_manager
121- app = None
122- auth_manager = None
118+ global _auth_manager
119+ cached_app . cache_clear ()
120+ _auth_manager = None
123121
124122
125123def get_auth_manager_cls () -> type [BaseAuthManager ]:
@@ -140,10 +138,10 @@ def get_auth_manager_cls() -> type[BaseAuthManager]:
140138
141139def create_auth_manager () -> BaseAuthManager :
142140 """Create the auth manager."""
143- global auth_manager
141+ global _auth_manager
144142 auth_manager_cls = get_auth_manager_cls ()
145- auth_manager = auth_manager_cls ()
146- return auth_manager
143+ _auth_manager = auth_manager_cls ()
144+ return _auth_manager
147145
148146
149147def init_auth_manager (app : FastAPI | None = None ) -> BaseAuthManager :
@@ -161,12 +159,12 @@ def init_auth_manager(app: FastAPI | None = None) -> BaseAuthManager:
161159
162160def get_auth_manager () -> BaseAuthManager :
163161 """Return the auth manager, provided it's been initialized before."""
164- if auth_manager is None :
162+ if _auth_manager is None :
165163 raise RuntimeError (
166164 "Auth Manager has not been initialized yet. "
167165 "The `init_auth_manager` method needs to be called first."
168166 )
169- return auth_manager
167+ return _auth_manager
170168
171169
172170def init_plugins (app : FastAPI ) -> None :
0 commit comments