From 633e76ee8948da659c17f89730654db664037a92 Mon Sep 17 00:00:00 2001 From: Peter Dudfield <34686298+peterdudfield@users.noreply.github.com> Date: Tue, 23 Jan 2024 14:52:56 +0000 Subject: [PATCH] Rm old cache (#320) * remove old cache * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fastapi==0.108.0 * pathy=0.10.3 * pathy==0.10.3 * update requirements * remove background tasks in cache * role back requirements * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * more robust cache tidy up * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- src/cache.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/cache.py b/src/cache.py index 8f2b9da..c0b90ee 100644 --- a/src/cache.py +++ b/src/cache.py @@ -30,14 +30,21 @@ def remove_old_cache( now = datetime.now(tz=timezone.utc) logger.info("Removing old cache entries") keys_to_remove = [] - for key, value in last_updated.items(): + last_updated_copy = last_updated.copy() + for key, value in last_updated_copy.items(): if now - timedelta(seconds=remove_cache_time_seconds) > value: logger.debug(f"Removing {key} from cache, ({value})") keys_to_remove.append(key) for key in keys_to_remove: - last_updated.pop(key) - response.pop(key) + try: + last_updated.pop(key) + response.pop(key) + except KeyError: + logger.warning( + f"Could not remove {key} from cache. " + f"This could be because it has already been removed" + ) return last_updated, response