Skip to content

Commit 9369cb5

Browse files
committed
replaced home made caching by aiocache
1 parent 17bbe29 commit 9369cb5

File tree

4 files changed

+20
-130
lines changed

4 files changed

+20
-130
lines changed

services/director/src/simcore_service_director/cache_request_decorator.py

Lines changed: 0 additions & 42 deletions
This file was deleted.

services/director/src/simcore_service_director/core/application.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from fastapi import FastAPI
55
from servicelib.fastapi.tracing import setup_tracing
66

7-
from .. import registry_cache_task
87
from .._meta import (
98
API_VERSION,
109
API_VTAG,
@@ -55,7 +54,6 @@ def create_app(settings: ApplicationSettings) -> FastAPI:
5554

5655
# replace by httpx client
5756
setup_client_session(app)
58-
registry_cache_task.setup(app)
5957
setup_registry(app)
6058

6159
setup_instrumentation(app)

services/director/src/simcore_service_director/registry_cache_task.py

Lines changed: 0 additions & 84 deletions
This file was deleted.

services/director/src/simcore_service_director/registry_proxy.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pprint import pformat
88
from typing import Any, Final
99

10+
from aiocache import Cache, SimpleMemoryCache
1011
from aiohttp import BasicAuth, ClientSession, client_exceptions
1112
from aiohttp.client import ClientTimeout
1213
from fastapi import FastAPI
@@ -18,7 +19,6 @@
1819
from yarl import URL
1920

2021
from . import exceptions
21-
from .cache_request_decorator import cache_requests
2222
from .client_session import get_client_session
2323
from .constants import (
2424
DIRECTOR_SIMCORE_SERVICES_PREFIX,
@@ -202,10 +202,25 @@ async def registry_request(
202202
logger.debug(
203203
"Request to registry: path=%s, method=%s. no_cache=%s", path, method, no_cache
204204
)
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(
206212
app, path, method, **session_kwargs
207213
)
208214

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+
209224

210225
async def _is_registry_responsive(app: FastAPI) -> bool:
211226
path = "/v2/"
@@ -237,6 +252,9 @@ async def wait_until_registry_responsive(app: FastAPI) -> bool:
237252

238253
def setup(app: FastAPI) -> None:
239254
async def on_startup() -> None:
255+
cache = Cache(Cache.MEMORY)
256+
assert isinstance(cache, SimpleMemoryCache) # nosec
257+
app.state.registry_cache_memory = cache
240258
await _setup_registry(app)
241259

242260
async def on_shutdown() -> None:

0 commit comments

Comments
 (0)