Skip to content

perf: Implement Caching Layer for Gemini API Route Handlers (#2) - #3

Open
adimadhubani wants to merge 1 commit into
Lets-code-with-us:mainfrom
adimadhubani:feature/gemini-api-caching
Open

perf: Implement Caching Layer for Gemini API Route Handlers (#2)#3
adimadhubani wants to merge 1 commit into
Lets-code-with-us:mainfrom
adimadhubani:feature/gemini-api-caching

Conversation

@adimadhubani

Copy link
Copy Markdown

Summary of Changes

  • Added @vercel/kv support with fail-open fallback mechanism.
  • Created deterministic SHA-256 cache key utility (lib/cache/hash.ts) that recursively sorts JSON keys.
  • Implemented 24-hour TTL caching wrapper (lib/cache/geminiCache.ts) for /api/gemini/chat and /api/gemini/insights.
  • Added X-Cache response headers (HIT / MISS) for verification.
  • Updated .env.example with KV variables.

Closes #2

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.

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/kv dependency and KV environment variable examples.
  • Implemented deterministic SHA-256 cache key generation with stable JSON serialization.
  • Wrapped /api/gemini/chat and /api/gemini/insights handlers with a KV-backed cache that sets X-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 };
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.

perf: Implement Caching for Gemini API Route Handlers

2 participants