Skip to content

Commit

Permalink
Remove cached responses older than 4 mins (#318)
Browse files Browse the repository at this point in the history
* 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

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
peterdudfield and pre-commit-ci[bot] authored Jan 22, 2024
1 parent 4aaf520 commit 3aa4f6a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ httpx
structlog
sentry-sdk
slowapi
pathy==0.10.3
29 changes: 29 additions & 0 deletions src/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@

CACHE_TIME_SECONDS = 120
cache_time_seconds = int(os.getenv("CACHE_TIME_SECONDS", CACHE_TIME_SECONDS))
DELETE_CACHE_TIME_SECONDS = 240
delete_cache_time_seconds = int(os.getenv("DELETE_CACHE_TIME_SECONDS", DELETE_CACHE_TIME_SECONDS))


def remove_old_cache(
last_updated: dict, response: dict, remove_cache_time_seconds: float = delete_cache_time_seconds
):
"""
Remove old cache entries from the cache
:param last_updated: dict of last updated times
:param response: dict of responses, same keys as last_updated
:param remove_cache_time_seconds: the amount of time, after which the cache should be removed
"""
now = datetime.now(tz=timezone.utc)
logger.info("Removing old cache entries")
keys_to_remove = []
for key, value in last_updated.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)

return last_updated, response


def cache_response(func):
Expand Down Expand Up @@ -52,6 +79,8 @@ def wrapper(*args, **kwargs): # noqa
if var in route_variables:
route_variables.pop(var)

last_updated, response = remove_old_cache(last_updated, response)

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

Expand Down

0 comments on commit 3aa4f6a

Please sign in to comment.