Add caching to MLB data fetching with dynamic expiration times

This commit is contained in:
ChuckBuilds
2025-04-24 13:52:34 -05:00
parent 3801e46233
commit 63ccb3a61d

View File

@@ -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: