Skip to content

(HKA-66) Cache song search results to reduce repeated fetch#2

Open
hkalbertkim wants to merge 2 commits intomainfrom
lin-HKA-66-cache-search-results
Open

(HKA-66) Cache song search results to reduce repeated fetch#2
hkalbertkim wants to merge 2 commits intomainfrom
lin-HKA-66-cache-search-results

Conversation

@hkalbertkim
Copy link
Copy Markdown
Owner

Summary

Adds an in-memory TTL cache to the searchYouTube function in server/src/services/youtube.ts. Repeated searches for the same query now return cached results instead of hitting the YouTube Data API again.

Why

Every search triggers 2 YouTube API calls (search + video details). The YouTube API has a daily quota (10,000 units); each search+details pair costs ~101 units. Users often re-search the same terms. Caching eliminates redundant API calls, reduces latency, and preserves quota.

What changed

  • server/src/services/youtube.ts — Added:
    • searchCache: a Map<string, CacheEntry> keyed by lowercased query
    • getCachedSearch(query): returns cached results if within TTL (5 min)
    • setCachedSearch(query, results): stores results with timestamp; evicts oldest entry when cache exceeds 100 entries
    • Cache check runs before any API call in searchYouTube()
    • Structured log lines: [search-cache] HIT / [search-cache] MISS

How to test

  1. Start the server: cd server && npm run dev
  2. Search for a song: curl 'http://localhost:4000/api/search?q=bohemian+rhapsody'
  3. Check server logs — should see [search-cache] MISS
  4. Repeat the same search within 5 minutes
  5. Check server logs — should see [search-cache] HIT and response is instant
  6. Wait >5 minutes and search again — should see MISS (TTL expired)

Risks / rollback

  • Low risk: Cache is in-memory only, no persistence. Server restart clears it.
  • Rollback: Revert this single commit; no schema/data changes.
  • Edge case: Stale results for up to 5 minutes after a video is published/removed — acceptable for song search.

@vercel
Copy link
Copy Markdown

vercel Bot commented Feb 11, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
sing-sync Ready Ready Preview, Comment Feb 11, 2026 4:57am

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.

1 participant