Skip to content

Commit

Permalink
add cache closing
Browse files Browse the repository at this point in the history
  • Loading branch information
jarojasm95 committed Nov 5, 2024
1 parent 749f1ce commit bc06540
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions aladdin/lib/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit bc06540

Please sign in to comment.