From b703f21384afaafd9bf7dc0e74ca5890249d3b5c Mon Sep 17 00:00:00 2001 From: braddf Date: Fri, 28 Jul 2023 17:37:06 +0100 Subject: [PATCH] add check/wait if cached response not yet saved --- src/cache.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/cache.py b/src/cache.py index c4aecc71..08da4964 100644 --- a/src/cache.py +++ b/src/cache.py @@ -1,6 +1,7 @@ """ Caching utils for api""" import json import os +import time from datetime import datetime, timedelta, timezone from functools import wraps @@ -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 @@ -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]