mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-12 21:43:00 +00:00
cache football dates when checking for next or last
This commit is contained in:
@@ -179,29 +179,43 @@ class BaseNCAAFBManager: # Renamed class
|
|||||||
BaseNCAAFBManager._last_shared_update = current_time
|
BaseNCAAFBManager._last_shared_update = current_time
|
||||||
return cached_data
|
return cached_data
|
||||||
|
|
||||||
# Smart game-based fetching: fetch incrementally until we have enough games
|
# Smart game-based fetching with range caching
|
||||||
today = datetime.now(self._get_timezone()).date()
|
today = datetime.now(self._get_timezone()).date()
|
||||||
all_events = []
|
all_events = []
|
||||||
past_events = []
|
past_events = []
|
||||||
future_events = []
|
future_events = []
|
||||||
|
|
||||||
# Start with today and expand outward until we have enough games
|
# Check for cached search ranges
|
||||||
days_to_check = 0
|
range_cache_key = f"search_ranges_ncaafb_{fetch_past_games}_{fetch_future_games}"
|
||||||
|
cached_ranges = BaseNCAAFBManager.cache_manager.get(range_cache_key, max_age=86400) # Cache for 24 hours
|
||||||
|
|
||||||
|
if cached_ranges:
|
||||||
|
past_days_needed = cached_ranges.get('past_days', 0)
|
||||||
|
future_days_needed = cached_ranges.get('future_days', 0)
|
||||||
|
BaseNCAAFBManager.logger.info(f"[NCAAFB] Using cached search ranges: {past_days_needed} days past, {future_days_needed} days future")
|
||||||
|
else:
|
||||||
|
past_days_needed = 0
|
||||||
|
future_days_needed = 0
|
||||||
|
|
||||||
|
# Start with cached ranges or expand incrementally
|
||||||
|
days_to_check = max(past_days_needed, future_days_needed)
|
||||||
max_days_to_check = 365 # Limit to 1 year to prevent infinite loops
|
max_days_to_check = 365 # Limit to 1 year to prevent infinite loops
|
||||||
|
|
||||||
while (len(past_events) < fetch_past_games or len(future_events) < fetch_future_games) and days_to_check <= max_days_to_check:
|
while (len(past_events) < fetch_past_games or len(future_events) < fetch_future_games) and days_to_check <= max_days_to_check:
|
||||||
# Check dates in both directions
|
# Check dates in both directions
|
||||||
dates_to_check = []
|
dates_to_check = []
|
||||||
|
|
||||||
# Check past dates
|
# Check past dates (start from cached range if available)
|
||||||
if len(past_events) < fetch_past_games:
|
if len(past_events) < fetch_past_games:
|
||||||
for i in range(1, days_to_check + 1):
|
start_day = past_days_needed if cached_ranges else 1
|
||||||
|
for i in range(start_day, days_to_check + 1):
|
||||||
past_date = today - timedelta(days=i)
|
past_date = today - timedelta(days=i)
|
||||||
dates_to_check.append(past_date.strftime('%Y%m%d'))
|
dates_to_check.append(past_date.strftime('%Y%m%d'))
|
||||||
|
|
||||||
# Check future dates
|
# Check future dates (start from cached range if available)
|
||||||
if len(future_events) < fetch_future_games:
|
if len(future_events) < fetch_future_games:
|
||||||
for i in range(1, days_to_check + 1):
|
start_day = future_days_needed if cached_ranges else 1
|
||||||
|
for i in range(start_day, days_to_check + 1):
|
||||||
future_date = today + timedelta(days=i)
|
future_date = today + timedelta(days=i)
|
||||||
dates_to_check.append(future_date.strftime('%Y%m%d'))
|
dates_to_check.append(future_date.strftime('%Y%m%d'))
|
||||||
|
|
||||||
@@ -209,6 +223,7 @@ class BaseNCAAFBManager: # Renamed class
|
|||||||
if days_to_check == 0:
|
if days_to_check == 0:
|
||||||
dates_to_check.append(today.strftime('%Y%m%d'))
|
dates_to_check.append(today.strftime('%Y%m%d'))
|
||||||
|
|
||||||
|
if dates_to_check:
|
||||||
BaseNCAAFBManager.logger.info(f"[NCAAFB] Checking {len(dates_to_check)} dates (day {days_to_check}) to find {fetch_past_games} past and {fetch_future_games} future games")
|
BaseNCAAFBManager.logger.info(f"[NCAAFB] Checking {len(dates_to_check)} dates (day {days_to_check}) to find {fetch_past_games} past and {fetch_future_games} future games")
|
||||||
|
|
||||||
# Fetch data for each date
|
# Fetch data for each date
|
||||||
@@ -261,6 +276,21 @@ class BaseNCAAFBManager: # Renamed class
|
|||||||
|
|
||||||
days_to_check += 1
|
days_to_check += 1
|
||||||
|
|
||||||
|
# Cache the search ranges for next time
|
||||||
|
if len(past_events) >= fetch_past_games and len(future_events) >= fetch_future_games:
|
||||||
|
# Calculate how many days we actually needed
|
||||||
|
actual_past_days = max(1, days_to_check - 1) if past_events else 0
|
||||||
|
actual_future_days = max(1, days_to_check - 1) if future_events else 0
|
||||||
|
|
||||||
|
# Cache the ranges for next time
|
||||||
|
range_data = {
|
||||||
|
'past_days': actual_past_days,
|
||||||
|
'future_days': actual_future_days,
|
||||||
|
'last_updated': current_time
|
||||||
|
}
|
||||||
|
BaseNCAAFBManager.cache_manager.set(range_cache_key, range_data)
|
||||||
|
BaseNCAAFBManager.logger.info(f"[NCAAFB] Cached search ranges: {actual_past_days} days past, {actual_future_days} days future")
|
||||||
|
|
||||||
# Take the specified number of games
|
# Take the specified number of games
|
||||||
selected_past_events = past_events[-fetch_past_games:] if past_events else []
|
selected_past_events = past_events[-fetch_past_games:] if past_events else []
|
||||||
selected_future_events = future_events[:fetch_future_games] if future_events else []
|
selected_future_events = future_events[:fetch_future_games] if future_events else []
|
||||||
|
|||||||
@@ -179,29 +179,43 @@ class BaseNFLManager: # Renamed class
|
|||||||
BaseNFLManager._last_shared_update = current_time
|
BaseNFLManager._last_shared_update = current_time
|
||||||
return cached_data
|
return cached_data
|
||||||
|
|
||||||
# Smart game-based fetching: fetch incrementally until we have enough games
|
# Smart game-based fetching with range caching
|
||||||
today = datetime.now(self._get_timezone()).date()
|
today = datetime.now(self._get_timezone()).date()
|
||||||
all_events = []
|
all_events = []
|
||||||
past_events = []
|
past_events = []
|
||||||
future_events = []
|
future_events = []
|
||||||
|
|
||||||
# Start with today and expand outward until we have enough games
|
# Check for cached search ranges
|
||||||
days_to_check = 0
|
range_cache_key = f"search_ranges_nfl_{fetch_past_games}_{fetch_future_games}"
|
||||||
|
cached_ranges = BaseNFLManager.cache_manager.get(range_cache_key, max_age=86400) # Cache for 24 hours
|
||||||
|
|
||||||
|
if cached_ranges:
|
||||||
|
past_days_needed = cached_ranges.get('past_days', 0)
|
||||||
|
future_days_needed = cached_ranges.get('future_days', 0)
|
||||||
|
BaseNFLManager.logger.info(f"[NFL] Using cached search ranges: {past_days_needed} days past, {future_days_needed} days future")
|
||||||
|
else:
|
||||||
|
past_days_needed = 0
|
||||||
|
future_days_needed = 0
|
||||||
|
|
||||||
|
# Start with cached ranges or expand incrementally
|
||||||
|
days_to_check = max(past_days_needed, future_days_needed)
|
||||||
max_days_to_check = 365 # Limit to 1 year to prevent infinite loops
|
max_days_to_check = 365 # Limit to 1 year to prevent infinite loops
|
||||||
|
|
||||||
while (len(past_events) < fetch_past_games or len(future_events) < fetch_future_games) and days_to_check <= max_days_to_check:
|
while (len(past_events) < fetch_past_games or len(future_events) < fetch_future_games) and days_to_check <= max_days_to_check:
|
||||||
# Check dates in both directions
|
# Check dates in both directions
|
||||||
dates_to_check = []
|
dates_to_check = []
|
||||||
|
|
||||||
# Check past dates
|
# Check past dates (start from cached range if available)
|
||||||
if len(past_events) < fetch_past_games:
|
if len(past_events) < fetch_past_games:
|
||||||
for i in range(1, days_to_check + 1):
|
start_day = past_days_needed if cached_ranges else 1
|
||||||
|
for i in range(start_day, days_to_check + 1):
|
||||||
past_date = today - timedelta(days=i)
|
past_date = today - timedelta(days=i)
|
||||||
dates_to_check.append(past_date.strftime('%Y%m%d'))
|
dates_to_check.append(past_date.strftime('%Y%m%d'))
|
||||||
|
|
||||||
# Check future dates
|
# Check future dates (start from cached range if available)
|
||||||
if len(future_events) < fetch_future_games:
|
if len(future_events) < fetch_future_games:
|
||||||
for i in range(1, days_to_check + 1):
|
start_day = future_days_needed if cached_ranges else 1
|
||||||
|
for i in range(start_day, days_to_check + 1):
|
||||||
future_date = today + timedelta(days=i)
|
future_date = today + timedelta(days=i)
|
||||||
dates_to_check.append(future_date.strftime('%Y%m%d'))
|
dates_to_check.append(future_date.strftime('%Y%m%d'))
|
||||||
|
|
||||||
@@ -209,6 +223,7 @@ class BaseNFLManager: # Renamed class
|
|||||||
if days_to_check == 0:
|
if days_to_check == 0:
|
||||||
dates_to_check.append(today.strftime('%Y%m%d'))
|
dates_to_check.append(today.strftime('%Y%m%d'))
|
||||||
|
|
||||||
|
if dates_to_check:
|
||||||
BaseNFLManager.logger.info(f"[NFL] Checking {len(dates_to_check)} dates (day {days_to_check}) to find {fetch_past_games} past and {fetch_future_games} future games")
|
BaseNFLManager.logger.info(f"[NFL] Checking {len(dates_to_check)} dates (day {days_to_check}) to find {fetch_past_games} past and {fetch_future_games} future games")
|
||||||
|
|
||||||
# Fetch data for each date
|
# Fetch data for each date
|
||||||
@@ -261,6 +276,21 @@ class BaseNFLManager: # Renamed class
|
|||||||
|
|
||||||
days_to_check += 1
|
days_to_check += 1
|
||||||
|
|
||||||
|
# Cache the search ranges for next time
|
||||||
|
if len(past_events) >= fetch_past_games and len(future_events) >= fetch_future_games:
|
||||||
|
# Calculate how many days we actually needed
|
||||||
|
actual_past_days = max(1, days_to_check - 1) if past_events else 0
|
||||||
|
actual_future_days = max(1, days_to_check - 1) if future_events else 0
|
||||||
|
|
||||||
|
# Cache the ranges for next time
|
||||||
|
range_data = {
|
||||||
|
'past_days': actual_past_days,
|
||||||
|
'future_days': actual_future_days,
|
||||||
|
'last_updated': current_time
|
||||||
|
}
|
||||||
|
BaseNFLManager.cache_manager.set(range_cache_key, range_data)
|
||||||
|
BaseNFLManager.logger.info(f"[NFL] Cached search ranges: {actual_past_days} days past, {actual_future_days} days future")
|
||||||
|
|
||||||
# Take the specified number of games
|
# Take the specified number of games
|
||||||
selected_past_events = past_events[-fetch_past_games:] if past_events else []
|
selected_past_events = past_events[-fetch_past_games:] if past_events else []
|
||||||
selected_future_events = future_events[:fetch_future_games] if future_events else []
|
selected_future_events = future_events[:fetch_future_games] if future_events else []
|
||||||
|
|||||||
Reference in New Issue
Block a user