Implemented caching for fantasy stuff - #283
Merged
Merged
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The new refresh + caching behavior has concurrency/cache-bypass gaps that can lead to duplicate in-flight AI calls and potentially cached “refresh” responses.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR adds an explicit “refresh” mechanism for fantasy recommendations and introduces backend in-memory caching to reduce repeated AI/projection work while still allowing clients to bypass caches when needed.
Changes:
- Adds
refresh=truesupport to thegetOverviewAPI/controller path and wires it through to the backend service. - Implements in-memory caching for AI trade analysis (and current projections) with a 24-hour TTL and refresh bypass.
- Updates the frontend (hook + page) and tests to trigger refreshes for lineup/trade/waiver recommendation sections.
File summaries
| File | Description |
|---|---|
| packages/frontend/src/pages/FantasyPage.tsx | Adds refresh UI controls for lineup/trade/waiver sections and loading/spinner states. |
| packages/frontend/src/pages/FantasyPage.spec.tsx | Updates UI tests to validate refresh behavior and the refresh=true query param. |
| packages/frontend/src/hooks/useFantasy.ts | Extends refresh handling to include lineup and switches refresh param to refresh=true. |
| packages/backend/src/fantasy/fantasy.service.ts | Adds caches for AI analysis and projections; plumbs refresh through overview generation. |
| packages/backend/src/fantasy/fantasy.service.spec.ts | Adds tests validating analysis caching TTL and refresh bypass behavior. |
| packages/backend/src/fantasy/fantasy.controller.ts | Plumbs refresh=true query param into getOverview(...). |
| packages/backend/src/fantasy/fantasy.controller.spec.ts | Updates controller tests to assert refresh behavior is forwarded correctly. |
Review details
Suppressed comments (1)
packages/backend/src/fantasy/fantasy.service.ts:389
getTradeAnalysisdoesn’t coalesce concurrent requests for the same cache key, so multiple simultaneous calls can each rungenerate()(potentially issuing multiple OpenAI calls). It also leaves expired entries in the Map unless overwritten. Use the in-flight promise cache and delete expired entries on access.
const cached = this.analysisCache.get(key);
if (!refresh && cached && cached.expiresAt > Date.now()) {
return cached.analysis;
}
- Files reviewed: 7/7 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
118
to
120
| private projectionCache = new Map<string, { expiresAt: number; projections: SleeperProjection[] }>(); | ||
| private analysisCache = new Map<string, { expiresAt: number; analysis: AITradeAnalysis }>(); | ||
| private readonly openAi: OpenAIClientLike; |
Comment on lines
97
to
99
| const data = await request<FantasyOverview>( | ||
| `/fantasy/leagues/${encodeURIComponent(selectedLeagueId)}?refresh=${Date.now()}`, | ||
| `/fantasy/leagues/${encodeURIComponent(selectedLeagueId)}?refresh=true`, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces a refresh mechanism for AI-powered fantasy football recommendations, allowing users to explicitly request updated lineup, trade, and waiver suggestions. It also implements backend caching for AI analysis and projections, ensuring efficient use of resources and up-to-date recommendations when needed. Additionally, the UI and tests are updated to reflect these enhancements.
Backend enhancements:
refreshparameter to thegetOverviewAPI and controller, enabling clients to bypass caches and request fresh AI recommendations when needed. [1] [2] [3]FantasyService, with a 24-hour expiration and cache bypass logic whenrefreshis true. [1] [2] [3] [4] [5] [6]Frontend improvements:
useFantasyhook and related UI to support refreshing lineup, trade, and waiver recommendations, including new button and state management for lineup refresh. [1] [2] [3] [4]