From 271bd56ab10db530626b0e170f898b9a64fe7bbc Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Mon, 10 Feb 2025 11:20:51 +0000 Subject: [PATCH] registy: Use the new cache instead of the in-memory cache Signed-off-by: Michael Tremer --- src/web/registry.py | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/src/web/registry.py b/src/web/registry.py index b26bd93a..4a0296af 100644 --- a/src/web/registry.py +++ b/src/web/registry.py @@ -152,12 +152,11 @@ class IndexHandler(BaseHandler): pass -# Docker is pulling the same content more than once (god knows why). After the -# ManifestLabelHandler has responded, the client will ask for the same file again -# by its digest. Therefore we simply cache them and will send them again upon request. -CACHE = {} - class ManifestLabelHandler(BaseHandler): + # Docker is pulling the same content more than once (god knows why). After the + # ManifestLabelHandler has responded, the client will ask for the same file again + # by its digest. Therefore we simply cache them and will send them again upon request. + async def head(self, *args, **kwargs): return await self.get(*args, **kwargs, send_body=False) @@ -203,7 +202,7 @@ class ManifestLabelHandler(BaseHandler): self.set_header("Content-Length", len(index)) # Hash and cache - CACHE[self.hash(index)] = index + await self.backend.cache.set(self.hash(index), index) # Send the response if send_body: @@ -216,8 +215,18 @@ class ManifestHandler(BaseHandler): async def get(self, distro_slug, digest, send_body=True): # If we have this item in the cache, we serve it straight away - if digest in CACHE: - return self._get_cached_manifest(digest, send_body=send_body) + manifest = await self.backend.cache.get(digest) + if manifest: + # Set Content-Type + self.set_header("Content-Type", "application/vnd.oci.image.index.v1+json") + self.set_header("Content-Length", len(manifest)) + + # Send the blob + if send_body: + self.finish(manifest) + + # We are done if we found a cached response + return # Fetch the distribution distro = await self.backend.distros.get_by_slug(distro_slug) @@ -236,19 +245,6 @@ class ManifestHandler(BaseHandler): if send_body: await self.stream_blob(blob) - def _get_cached_manifest(self, digest, send_body=True): - try: - manifest = CACHE[digest] - except KeyError: - raise ManifestUnknownError - - # Set Content-Type - self.set_header("Content-Type", "application/vnd.oci.image.index.v1+json") - self.set_header("Content-Length", len(index)) - - if send_body: - self.finish(manifest) - class BlobHandler(BaseHandler): async def head(self, *args, **kwargs): -- 2.47.2