From 613bb108a79538a8b38c5e34b2fae981b987bce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 24 Nov 2023 13:14:01 +0100 Subject: [PATCH] Proper caching of player history --- src/constants.py | 5 +++-- src/fetch_games.py | 8 ++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/constants.py b/src/constants.py index 544f400..2a55294 100644 --- a/src/constants.py +++ b/src/constants.py @@ -41,5 +41,6 @@ FORBIDDEN_GAME_OPTIONS = [ , "detrimentalCharacters" ] -# Cache time in secodn -USER_HISTORY_CACHE_TIME = 5 +# Cache time (in seconds) for history requests of players +# In case of frequent reruns (especially during development), we do not want to stress the server too much. +USER_HISTORY_CACHE_TIME = 5 * 60 diff --git a/src/fetch_games.py b/src/fetch_games.py index c46b53a..5323ef0 100644 --- a/src/fetch_games.py +++ b/src/fetch_games.py @@ -19,13 +19,17 @@ from config import config_manager # This ensures that we do not pose too many requests to the website, especially while in development where we # might frequently re-initialize the database. session = requests_cache.CachedSession( - platformdirs.user_cache_dir(constants.APP_NAME) + 'hanab.live.requests-cache', + platformdirs.user_cache_dir(constants.APP_NAME) + '/hanab.live.requests-cache', urls_expire_after={ # Game exports will never change, so cache them forever - 'hanab.live/export/*': requests_cache.NEVER_EXPIRE + 'hanab.live/export/*': requests_cache.NEVER_EXPIRE, + # Cash history requests for 5 minutes, this seems like a reasonable time + 'hanab.live/api/v1/history-full/*': constants.USER_HISTORY_CACHE_TIME } ) +print(platformdirs.user_cache_dir(constants.APP_NAME) + 'hanab.live.requests-cache') + class GameInfo: def __init__(self, game_id: int, num_players: int, variant_id: int, seed: str, score: int, num_turns: int,