Install this skill
npx skills add vercel-labs/agent-skillsWorks across Claude Code, Cursor, Codex, Copilot & Antigravity
Cross-request LRU caching provides a high-performance mechanism for retaining data in memory across distinct user interactions. Unlike request-scoped utilities such as React.cache, which clear once a single server response completes, an LRU cache maintains persistent state within the server process. By storing frequently accessed records—such as configuration settings, user profiles, or lookup tables—in the active heap, you eliminate repetitive database roundtrips during rapid sequential actions. This approach excels in environments like Vercel Fluid Compute where multiple incoming requests share a warm execution instance. Because the data resides locally, it avoids the network latency associated with external key-value stores. This technique balances memory constraints through a Least Recently Used eviction policy, ensuring the most relevant data remains accessible while preventing memory exhaustion from stale entries.
When to Use This Skill
- •Storing session-specific metadata that changes slowly
- •Caching user authorization headers across rapid sub-resource fetches
- •Reducing database load for frequent lookup tables
- •Aggregating repetitive API responses for a specific user ID
How to Invoke This Skill
Example prompts that trigger this skill in Claude Code, Cursor, or Antigravity:
- “How do I share data between different API requests?
- “Implement an in-memory cache for repeated database queries
- “Stop querying the database for the same user profile repeatedly
- “Create a cache that survives beyond a single request cycle
- “Use LRU cache to reduce latency in my serverless functions
What this skill does
- •Retains data in-memory across sequential HTTP requests
- •Configures automated memory limits to prevent heap overflow
- •Sets Time-To-Live expiration to ensure data freshness
- •Supports arbitrary key-value storage for serializable objects
- •Automatic eviction of the least accessed items when cache capacity is reached
When not to use it
- ✕When your application runs in a stateless, ephemeral serverless environment without shared compute instances
- ✕When data must be consistent across multiple distributed server nodes or regions
- ✕When the cached data is highly sensitive and exceeds memory safe limits
Example workflow
- Initialize an LRU cache instance outside the handler function to persist state
- Define a wrapper function to check the cache for a specific key
- Perform a database fetch only on a cache miss
- Write the fetched results back to the cache before returning
- Verify the subsequent request retrieves the object directly from the cache
Prerequisites
- –node-lru-cache package installed
- –Persistent server execution environment
Pitfalls & limitations
- !Cache poisoning if keys are not scoped correctly per user
- !Memory bloat if the cache size limits are set too high
- !Stale data if the TTL is significantly longer than the expected data change interval
- !Data loss if the server instance restarts or scales down
FAQ
How it compares
Generic caching solutions usually rely on external network calls to databases, whereas this pattern optimizes performance by utilizing the local server heap directly.
📄 Full skill instructions — original source: vercel-labs/agent-skills
React.cache() only works within one request. For data shared across sequential requests (user clicks button A then button B), use an LRU cache.**Implementation:**
import { LRUCache } from 'lru-cache'
const cache = new LRUCache<string, any>({
max: 1000,
ttl: 5 * 60 * 1000 // 5 minutes
})
export async function getUser(id: string) {
const cached = cache.get(id)
if (cached) return cached
const user = await db.user.findUnique({ where: { id } })
cache.set(id, user)
return user
}
// Request 1: DB query, result cached
// Request 2: cache hit, no DB queryUse when sequential user actions hit multiple endpoints needing the same data within seconds.
**With Vercel's [Fluid Compute](https://vercel.com/docs/fluid-compute):** LRU caching is especially effective because multiple concurrent requests can share the same function instance and cache. This means the cache persists across requests without needing external storage like Redis.
**In traditional serverless:** Each invocation runs in isolation, so consider Redis for cross-process caching.
Reference: [https://github.com/isaacs/node-lru-cache](https://github.com/isaacs/node-lru-cache)
How to Use This Skill Unit
Option A: Project-Specific (Recommended)
- Click "Download" above
- In your project, create the directory:
.agent/skills/server-cache-lru/ - Save the file as
SKILL.md - The agent will automatically discover the skill based on its description.
Option B: Global Installation (All Agents)
Save the file to these locations to make it available across all projects:
- Claude Code:
~/.claude/skills/vercel-labs/agent-skills/server-cache-lru/SKILL.md - Cursor:
~/.cursor/skills/vercel-labs/agent-skills/server-cache-lru/SKILL.md - Antigravity:
~/.gemini/antigravity/skills/vercel-labs/agent-skills/server-cache-lru/SKILL.md
🚀 Install with CLI:npx skills add vercel-labs/agent-skills