-
-
Notifications
You must be signed in to change notification settings - Fork 18
CACHE_STRATEGY
Your LEDMatrix system uses a sophisticated multi-tier caching strategy that balances data freshness with API efficiency.
-
Live Sports Scores: Now respects sport-specific
live_update_intervalconfiguration- Soccer live data: Uses
soccer_scoreboard.live_update_interval(default: 60 seconds) - NFL live data: Uses
nfl_scoreboard.live_update_interval(default: 60 seconds) - NHL live data: Uses
nhl_scoreboard.live_update_interval(default: 60 seconds) - NBA live data: Uses
nba_scoreboard.live_update_interval(default: 60 seconds) - MLB live data: Uses
mlb.live_update_interval(default: 60 seconds) - NCAA sports: Use respective
live_update_intervalconfigurations (default: 60 seconds)
- Soccer live data: Uses
- Current Weather: 5 minutes (300 seconds)
- Stocks: 10 minutes (600 seconds) - market hours aware
- Crypto: 5 minutes (300 seconds) - 24/7 trading
- Stock News: 1 hour (3600 seconds)
- Recent Games: 5 minutes (300 seconds)
- Upcoming Games: 1 hour (3600 seconds)
- Season Schedules: 24 hours (86400 seconds)
- Team Information: 1 week (604800 seconds)
- Team Logos: 30 days (2592000 seconds)
- Configuration Data: 1 week (604800 seconds)
Beyond time limits, the system uses content-based invalidation:
def has_data_changed(self, data_type: str, new_data: Dict[str, Any]) -> bool:
"""Check if data has changed from cached version."""- Weather: Compares temperature and conditions
- Stocks: Compares prices (only during market hours)
- Sports: Compares scores, game status, inning details
- News: Compares headlines and article IDs
For stocks, the system extends cache duration during off-hours:
def _is_market_open(self) -> bool:
"""Check if the US stock market is currently open."""
# Only invalidates cache during market hoursThe cache manager now automatically respects the live_update_interval configuration for each sport:
def get_sport_live_interval(self, sport_key: str) -> int:
"""Get the live_update_interval for a specific sport from config."""
config = self.config_manager.get_config()
sport_config = config.get(f"{sport_key}_scoreboard", {})
return sport_config.get("live_update_interval", 30)The cache manager automatically detects the sport from cache keys:
def get_sport_key_from_cache_key(self, key: str) -> Optional[str]:
"""Extract sport key from cache key to determine appropriate live_update_interval."""
# Maps cache key patterns to sport keys
sport_patterns = {
'nfl': ['nfl', 'football'],
'nba': ['nba', 'basketball'],
'mlb': ['mlb', 'baseball'],
'nhl': ['nhl', 'hockey'],
'soccer': ['soccer', 'football'],
# ... etc
}Current Configuration (config/config.json):
{
"nfl_scoreboard": {
"live_update_interval": 30,
"enabled": true
},
"soccer_scoreboard": {
"live_update_interval": 30,
"enabled": false
},
"mlb": {
"live_update_interval": 30,
"enabled": true
}
}Cache Behavior:
- NFL live data: 30-second cache (from config)
- Soccer live data: 30-second cache (from config)
- MLB live data: 30-second cache (from config)
If configuration is unavailable, the system uses sport-specific defaults:
default_intervals = {
'soccer': 60, # Soccer default
'nfl': 60, # NFL default
'nhl': 60, # NHL default
'nba': 60, # NBA default
'mlb': 60, # MLB default
'milb': 60, # Minor league default
'ncaa_fb': 60, # College football default
'ncaa_baseball': 60, # College baseball default
'ncaam_basketball': 60, # College basketball default
}# Cache manager automatically detects NFL and uses nfl_scoreboard.live_update_interval
cached_data = cache_manager.get_with_auto_strategy("nfl_live_20241201")
# Cache manager automatically detects soccer and uses soccer_scoreboard.live_update_interval
cached_data = cache_manager.get_with_auto_strategy("soccer_live_20241201")# Explicitly specify sport for custom cache keys
cached_data = cache_manager.get_cached_data_with_strategy("custom_live_key", "sports_live")- Configuration-Driven: Cache respects your sport-specific settings
- Automatic Detection: No manual cache duration management needed
- Sport-Optimized: Each sport uses its appropriate update interval
- Backward Compatible: Existing code continues to work
- Flexible: Easy to adjust intervals per sport in config
The enhanced cache manager is backward compatible. Existing code will automatically benefit from sport-specific intervals without any changes needed.
To customize intervals for specific sports, simply update the live_update_interval in your config/config.json:
{
"nfl_scoreboard": {
"live_update_interval": 15 // More aggressive for NFL
},
"mlb": {
"live_update_interval": 45 // Slower pace for MLB
}
}