Skip to content

Commit 731c978

Browse files
committed
fix bug when expired cache from file would be loaded into memory
1 parent 8d5a8a2 commit 731c978

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/firebolt/utils/cache.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,13 @@ def _is_expired(self, value: T) -> bool:
143143
return False
144144

145145
@noop_if_disabled
146-
def set(self, key: ReprCacheable, value: T) -> None:
146+
def set(self, key: ReprCacheable, value: T, preserve_expiry: bool = False) -> None:
147147
if not self.disabled:
148148
# Set expiry_time for ConnectionInfo objects
149149
if hasattr(value, "expiry_time"):
150-
current_time = int(time.time())
151-
value.expiry_time = current_time + CACHE_EXPIRY_SECONDS
150+
if not preserve_expiry or value.expiry_time is None:
151+
current_time = int(time.time())
152+
value.expiry_time = current_time + CACHE_EXPIRY_SECONDS
152153

153154
s_key = self.create_key(key)
154155
self._cache[s_key] = value
@@ -275,11 +276,23 @@ def get(self, key: SecureCacheKey) -> Optional[ConnectionInfo]:
275276
raw_data = self._read_data_json(file_path, encrypter)
276277
if not raw_data:
277278
return None
279+
278280
logger.debug("Cache hit on disk")
279281
data = ConnectionInfo(**raw_data)
280282

281-
# Add to memory cache and return
282-
self.memory_cache.set(key, data)
283+
# Check if the loaded data is expired
284+
if self.memory_cache._is_expired(data):
285+
# Data is expired, delete the file and return None
286+
try:
287+
if path.exists(file_path):
288+
os.remove(file_path)
289+
logger.debug("Deleted expired file %s", file_path)
290+
except OSError:
291+
logger.debug("Failed to delete expired file %s", file_path)
292+
return None
293+
294+
# Data is not expired, add to memory cache preserving original expiry time
295+
self.memory_cache.set(key, data, preserve_expiry=True)
283296
return data
284297

285298
def set(self, key: SecureCacheKey, value: ConnectionInfo) -> None:

0 commit comments

Comments
 (0)