From bc065403e46e77901567e321a159db3f842c9398 Mon Sep 17 00:00:00 2001 From: Jose Rojas <39174181+jarojasm95@users.noreply.github.com> Date: Mon, 4 Nov 2024 23:43:45 -0700 Subject: [PATCH] add cache closing --- aladdin/lib/cache.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/aladdin/lib/cache.py b/aladdin/lib/cache.py index 3458b1e..33dde63 100644 --- a/aladdin/lib/cache.py +++ b/aladdin/lib/cache.py @@ -5,7 +5,7 @@ import functools import shelve import shutil -from contextlib import contextmanager +from contextlib import contextmanager, closing from collections import defaultdict from aladdin.lib.cluster_rules import ClusterRules @@ -42,26 +42,26 @@ def wrapper(certificate_scope): if not ClusterRules().certificate_lookup_cache: return func(certificate_scope) - cache = shelve.open(str(cache_path)) - data: dict = cache.get(certificate_scope) or {} + with closing(shelve.open(str(cache_path))) as cache: + data: dict = cache.get(certificate_scope) or {} - age = time.time() - data.get("time", 0) - value = data.get("value") - ttl = ttls[value] - if ( - not data - or age > ttl.total_seconds() - ): - value = func(certificate_scope) - cache[certificate_scope] = { - "value": value, - "time": time.time(), - } - cache.close() - elif value: - logging.info( - "Found CACHED certificate %s for %s", value, certificate_scope - ) - return value + age = time.time() - data.get("time", 0) + value = data.get("value") + ttl = ttls[value] + if ( + not data + or age > ttl.total_seconds() + ): + value = func(certificate_scope) + cache[certificate_scope] = { + "value": value, + "time": time.time(), + } + elif value: + logging.info( + "Found CACHED certificate %s for %s", + value, certificate_scope + ) + return value return wrapper