Fix duplicate caching (#62)

* Fix duplicate/redundant caching issue

- Add get_background_cached_data() and is_background_data_available() methods to CacheManager
- Update sports managers to use background service cache instead of individual caching
- Ensure consistent cache key generation between background service and managers
- Eliminate redundant API calls by making Recent/Upcoming managers cache consumers
- Fix cache miss issues where TTL < update interval

This addresses GitHub issue #57 by implementing a cleaner caching architecture
where the background service is the primary data source and managers are cache consumers.

* Update remaining sports managers to use background service cache

- Update NHL managers to use background service cache
- Update NCAA Football managers to use background service cache
- Update NCAA Hockey managers to use background service cache
- Update MLB managers to use background service cache for Recent/Upcoming

All sports managers now use the new caching architecture to eliminate
duplicate caching and redundant API calls.

* cache improvements

* updated cache manager
This commit is contained in:
Chuck
2025-09-24 16:13:41 -04:00
committed by GitHub
parent b1295047e2
commit 42e14f99b0
14 changed files with 581 additions and 83 deletions

View File

@@ -12,6 +12,7 @@ from src.cache_manager import CacheManager
from src.config_manager import ConfigManager
from src.odds_manager import OddsManager
from src.background_data_service import get_background_service
from src.background_cache_mixin import BackgroundCacheMixin
import pytz
# Import the API counter function from web interface
@@ -32,7 +33,7 @@ logging.basicConfig(
datefmt='%Y-%m-%d %H:%M:%S'
)
class BaseNBAManager:
class BaseNBAManager(BackgroundCacheMixin):
"""Base class for NBA managers with common functionality."""
# Class variables for warning tracking
_no_data_warning_logged = False
@@ -317,11 +318,16 @@ class BaseNBAManager:
return None
def _fetch_data(self, date_str: str = None) -> Optional[Dict]:
"""Fetch data using shared data mechanism."""
if isinstance(self, NBALiveManager):
return self._fetch_nba_api_data(use_cache=False)
else:
return self._fetch_nba_api_data(use_cache=True)
"""
Fetch data using background service cache first, fallback to direct API call.
This eliminates redundant caching and ensures Recent/Upcoming managers
use the same data source as the background service.
"""
return self._fetch_data_with_background_cache(
sport_key='nba',
api_fetch_method=self._fetch_nba_api_data,
live_manager_class=NBALiveManager
)
def _fetch_odds(self, game: Dict) -> None:
"""Fetch odds for a specific game if conditions are met."""