|
7 | 7 | from pprint import pformat |
8 | 8 | from typing import Any, Final |
9 | 9 |
|
| 10 | +from aiocache import Cache, SimpleMemoryCache |
10 | 11 | from aiohttp import BasicAuth, ClientSession, client_exceptions |
11 | 12 | from aiohttp.client import ClientTimeout |
12 | 13 | from fastapi import FastAPI |
|
18 | 19 | from yarl import URL |
19 | 20 |
|
20 | 21 | from . import exceptions |
21 | | -from .cache_request_decorator import cache_requests |
22 | 22 | from .client_session import get_client_session |
23 | 23 | from .constants import ( |
24 | 24 | DIRECTOR_SIMCORE_SERVICES_PREFIX, |
@@ -202,10 +202,25 @@ async def registry_request( |
202 | 202 | logger.debug( |
203 | 203 | "Request to registry: path=%s, method=%s. no_cache=%s", path, method, no_cache |
204 | 204 | ) |
205 | | - return await cache_requests(_basic_auth_registry_request, no_cache=no_cache)( |
| 205 | + cache: SimpleMemoryCache = app.state.registry_cache_memory |
| 206 | + cache_key = f"{method}_{path}" |
| 207 | + if not no_cache and (cached_response := await cache.get(cache_key)): |
| 208 | + return cached_response |
| 209 | + |
| 210 | + app_settings = get_application_settings(app) |
| 211 | + response, response_headers = await _basic_auth_registry_request( |
206 | 212 | app, path, method, **session_kwargs |
207 | 213 | ) |
208 | 214 |
|
| 215 | + if not no_cache and app_settings.DIRECTOR_REGISTRY_CACHING and method == "GET": |
| 216 | + await cache.set( |
| 217 | + cache_key, |
| 218 | + (response, response_headers), |
| 219 | + ttl=app_settings.DIRECTOR_REGISTRY_CACHING_TTL.total_seconds(), |
| 220 | + ) |
| 221 | + |
| 222 | + return response, response_headers |
| 223 | + |
209 | 224 |
|
210 | 225 | async def _is_registry_responsive(app: FastAPI) -> bool: |
211 | 226 | path = "/v2/" |
@@ -237,6 +252,9 @@ async def wait_until_registry_responsive(app: FastAPI) -> bool: |
237 | 252 |
|
238 | 253 | def setup(app: FastAPI) -> None: |
239 | 254 | async def on_startup() -> None: |
| 255 | + cache = Cache(Cache.MEMORY) |
| 256 | + assert isinstance(cache, SimpleMemoryCache) # nosec |
| 257 | + app.state.registry_cache_memory = cache |
240 | 258 | await _setup_registry(app) |
241 | 259 |
|
242 | 260 | async def on_shutdown() -> None: |
|
0 commit comments