From 63ccb3a61dcbf77639ad8dc3f320b2b7b44d1443 Mon Sep 17 00:00:00 2001 From: ChuckBuilds <33324927+ChuckBuilds@users.noreply.github.com> Date: Thu, 24 Apr 2025 13:52:34 -0500 Subject: [PATCH] Add caching to MLB data fetching with dynamic expiration times --- src/mlb_manager.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/mlb_manager.py b/src/mlb_manager.py index 5e6f44a9..8c3256ee 100644 --- a/src/mlb_manager.py +++ b/src/mlb_manager.py @@ -159,6 +159,14 @@ class BaseMLBManager: # ESPN API endpoint for MLB games url = "https://site.api.espn.com/apis/site/v2/sports/baseball/mlb/scoreboard" + # Try to get cached data first + cache_key = f"mlb_games_{datetime.now().strftime('%Y%m%d')}" + cached_data = self.cache_manager.get(cache_key) + + if cached_data: + self.logger.info("Using cached MLB game data") + return cached_data + self.logger.info("Fetching MLB games from ESPN API") response = self.session.get(url, headers=self.headers, timeout=10) response.raise_for_status() @@ -215,6 +223,12 @@ class BaseMLBManager: 'start_time': event['date'] } + # Cache the results with different expiration times based on game status + cache_duration = 300 # 5 minutes for live games + if not any(game['status'] == 'in' for game in games.values()): + cache_duration = 3600 # 1 hour for non-live games + + self.cache_manager.set(cache_key, games, cache_duration) return games except Exception as e: