From 099f6e6fe2ab3fe5e777e69a8f15c0f0226a00a9 Mon Sep 17 00:00:00 2001 From: Michael Manganiello Date: Fri, 13 Dec 2024 14:33:56 -0300 Subject: [PATCH] misc: Remove unneeded Redis key for Twitch token expiration The key `romm:twitch_token_expires_at` is not needed, as we already set an expiration time for the token key itself. If the token is still present in Redis, it is valid. If not, it means we either never fetched it, or it was too close to expiration. With this change, fetching the Twitch token makes a single Redis call instead of two. --- backend/handler/metadata/igdb_handler.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/backend/handler/metadata/igdb_handler.py b/backend/handler/metadata/igdb_handler.py index 75ff5fa23..d387540d9 100644 --- a/backend/handler/metadata/igdb_handler.py +++ b/backend/handler/metadata/igdb_handler.py @@ -1,6 +1,5 @@ import functools import re -import time from typing import Final, NotRequired, TypedDict import httpx @@ -664,12 +663,12 @@ class TwitchAuth(MetadataHandler): self.timeout = 10 async def _update_twitch_token(self) -> str: - token = None - expires_in = 0 - if not IGDB_API_ENABLED: return "" + token = None + expires_in = 0 + httpx_client = ctx_httpx_client.get() try: log.debug( @@ -698,11 +697,8 @@ class TwitchAuth(MetadataHandler): if not token or expires_in == 0: return "" - # Set token in redis to expire in seconds + # Set token in Redis to expire some seconds before it actually expires. await async_cache.set("romm:twitch_token", token, ex=expires_in - 10) - await async_cache.set( - "romm:twitch_token_expires_at", time.time() + expires_in - 10 - ) log.info("Twitch token fetched!") @@ -718,9 +714,7 @@ class TwitchAuth(MetadataHandler): # Fetch the token cache token = await async_cache.get("romm:twitch_token") - token_expires_at = await async_cache.get("romm:twitch_token_expires_at") - - if not token or time.time() > float(token_expires_at or 0): + if not token: log.warning("Twitch token invalid: fetching a new one...") return await self._update_twitch_token()