From 2bb19d8dfd9511022e8e377239c03dcd7c074d7a Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Mon, 2 Mar 2026 15:04:16 -0600 Subject: [PATCH] refactor: invalidate roster cache after submission instead of force-refreshing Simplify review found that force_refresh=True on every validate_transaction() call caused redundant API fetches on every embed render and button press. Instead, invalidate the roster cache after successful submit_transaction() so the next operation fetches fresh data. This preserves the cache for normal interaction flows while still preventing stale data after submissions. Also adds type annotation for roster_svc DI parameter. Co-Authored-By: Claude Opus 4.6 --- services/transaction_builder.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/services/transaction_builder.py b/services/transaction_builder.py index c38145a..2aa879c 100644 --- a/services/transaction_builder.py +++ b/services/transaction_builder.py @@ -14,7 +14,7 @@ from models.transaction import Transaction from models.team import Team from models.player import Player from models.roster import TeamRoster -from services.roster_service import roster_service +from services.roster_service import RosterService, roster_service from services.transaction_service import transaction_service from services.league_service import league_service from models.team import RosterType @@ -179,7 +179,7 @@ class TransactionBuilder: team: Team, user_id: int, season: int = get_config().sba_season, - roster_svc=None, + roster_svc: Optional[RosterService] = None, ): """ Initialize transaction builder. @@ -367,9 +367,7 @@ class TransactionBuilder: Returns: RosterValidationResult with validation details """ - # Always refresh roster data before validation to prevent stale cache - # from causing incorrect roster counts (e.g. after a previous /ilmove submission) - await self.load_roster_data(force_refresh=True) + await self.load_roster_data() # Load existing transactions if next_week is provided if next_week is not None: @@ -694,8 +692,17 @@ class TransactionBuilder: logger.info( f"Created {len(transactions)} transactions for submission with move_id {move_id}" ) + + # Invalidate roster cache so subsequent operations fetch fresh data + self.invalidate_roster_cache() + return transactions + def invalidate_roster_cache(self) -> None: + """Invalidate cached roster data so next load fetches fresh data.""" + self._roster_loaded = False + self._current_roster = None + def clear_moves(self) -> None: """Clear all moves from the transaction builder.""" self.moves.clear()