perf: Implement Caching Layer for Gemini API Route Handlers (#2) - #3
Open
adimadhubani wants to merge 1 commit into
Open
perf: Implement Caching Layer for Gemini API Route Handlers (#2)#3adimadhubani wants to merge 1 commit into
adimadhubani wants to merge 1 commit into
Conversation
6 tasks
There was a problem hiding this comment.
Pull request overview
This PR introduces a shared caching layer for the Gemini API route handlers to reduce redundant Gemini calls, lower latency, and improve scalability by caching successful responses for 24 hours in Vercel KV.
Changes:
- Added
@vercel/kvdependency and KV environment variable examples. - Implemented deterministic SHA-256 cache key generation with stable JSON serialization.
- Wrapped
/api/gemini/chatand/api/gemini/insightshandlers with a KV-backed cache that setsX-Cache: HIT|MISS.
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| package.json | Adds @vercel/kv dependency for KV-backed caching. |
| lib/cache/hash.ts | Adds deterministic cache key generation via recursively sorted JSON + SHA-256. |
| lib/cache/geminiCache.ts | Adds KV read-through cache wrapper with TTL and X-Cache headers. |
| app/api/gemini/insights/route.ts | Wraps Gemini insights generation in the caching wrapper. |
| app/api/gemini/chat/route.ts | Wraps chat generation in the caching wrapper. |
| .env.example | Documents KV env vars needed for caching (with intended fail-open behavior). |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| httpOptions: { | ||
| headers: { | ||
| 'User-Agent': 'aistudio-build', | ||
| const cachePayload = { route: 'chat', message, history }; |
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.
Summary of Changes
@vercel/kvsupport with fail-open fallback mechanism.lib/cache/hash.ts) that recursively sorts JSON keys.lib/cache/geminiCache.ts) for/api/gemini/chatand/api/gemini/insights.X-Cacheresponse headers (HIT/MISS) for verification..env.examplewith KV variables.Closes #2