Skip to content

Implemented caching for fantasy stuff - #283

Merged
sfreeman422 merged 1 commit into
masterfrom
feature/caching-fantasy
Sep 9, 2026
Merged

Implemented caching for fantasy stuff#283
sfreeman422 merged 1 commit into
masterfrom
feature/caching-fantasy

Conversation

@sfreeman422

@sfreeman422 sfreeman422 commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

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:

  • Added a refresh parameter to the getOverview API and controller, enabling clients to bypass caches and request fresh AI recommendations when needed. [1] [2] [3]
  • Implemented in-memory caching for AI trade analysis and projections in FantasyService, with a 24-hour expiration and cache bypass logic when refresh is true. [1] [2] [3] [4] [5] [6]
  • Added comprehensive backend tests to verify correct caching and refresh behavior for AI trade analysis. [1] [2] [3]

Frontend improvements:

  • Updated the useFantasy hook and related UI to support refreshing lineup, trade, and waiver recommendations, including new button and state management for lineup refresh. [1] [2] [3] [4]
  • Enhanced frontend tests to cover the new refresh functionality for all recommendation types, ensuring correct API usage and UI updates.

Copilot AI lite review requested due to automatic review settings September 9, 2026 17:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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=true support to the getOverview API/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

  • getTradeAnalysis doesn’t coalesce concurrent requests for the same cache key, so multiple simultaneous calls can each run generate() (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`,
);
@sfreeman422
sfreeman422 merged commit 28d25ab into master Sep 9, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants