Skip to content

Commit

Permalink
Merge pull request #257 from openclimatefix/fix/cache-async-bug
Browse files Browse the repository at this point in the history
add check/wait if cached response not yet saved
  • Loading branch information
braddf authored Aug 2, 2023
2 parents 3cc5122 + b703f21 commit f22cd23
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Caching utils for api"""
import json
import os
import time
from datetime import datetime, timedelta, timezone
from functools import wraps

Expand Down Expand Up @@ -51,7 +52,7 @@ def wrapper(*args, **kwargs): # noqa
if var in route_variables:
route_variables.pop(var)

# make into string
# make route_variables into a string
route_variables = json.dumps(route_variables)

# check if its been called before
Expand All @@ -69,6 +70,27 @@ def wrapper(*args, **kwargs): # noqa
last_updated[route_variables] = now
return response[route_variables]

# re-run if response is not cached for some reason or is empty
if route_variables not in response or response[route_variables] is None:
logger.debug("not using cache as response is empty")
attempt = 0
# wait until response has been cached
while attempt < 10:
logger.debug(f"waiting for response to be cached, {attempt} seconds elapsed")
time.sleep(1)
attempt += 1
if route_variables in response and response[route_variables] is not None:
logger.debug(
f"response cached after {attempt} seconds, returning cached response"
)
break
if attempt >= 10:
# if response is not in cache after 10 seconds, re-run
logger.debug("response not cached after 10 seconds, re-running")
response[route_variables] = func(*args, **kwargs)
last_updated[route_variables] = now
return response[route_variables]

# use cache
logger.debug("Using cache route")
return response[route_variables]
Expand Down

0 comments on commit f22cd23

Please sign in to comment.