From e78794ed54e25d60cc93a0fcb2d0f7f225a18cb7 Mon Sep 17 00:00:00 2001 From: Casey Ransom Date: Fri, 5 Apr 2024 10:53:39 -0400 Subject: [PATCH] Add manifest caching shortcut --- src/zinc/storages/aws.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/zinc/storages/aws.py b/src/zinc/storages/aws.py index 08287a7..9af9bf2 100644 --- a/src/zinc/storages/aws.py +++ b/src/zinc/storages/aws.py @@ -4,6 +4,8 @@ from urllib.parse import urlparse import logging +from os.path import exists + import boto3 from . import StorageBackend @@ -63,10 +65,16 @@ def _get_keyname(self, subpath): def get(self, subpath): keyname = self._get_keyname(subpath) t = TemporaryFile(mode='w+b') - self._bucket.download_fileobj(keyname, t) - if t.tell() == 0: - return None - t.seek(0) + cache_location = os.environ.get('ZINC_CACHE') + if cache_location and exists(os.path.join(cache_location, keyname)): + log.info(f"GET Cache hit: {subpath} ({keyname})") + t = open(os.path.join(cache_location,keyname), 'rb') + else: + log.info(f"GET Cache miss: {subpath} ({keyname})") + self._bucket.download_fileobj(keyname, t) + if t.tell() == 0: + return None + t.seek(0) return t def get_meta(self, subpath):