server-cache-lru — Claude Code rule: what it does, install
The server-cache-lru rule explains when React.cache() is too narrow and an LRU cache should share data between requests. Use it for sequential actions that reuse the same records.
MIT ★ 7,191 updated 2026-10-24
.claude/rules/server-cache-lru.mdWhat server-cache-lru does
server-cache-lru covers caching across separate requests rather than within one request. When two endpoints need the same user data in sequence, the first database lookup can populate an LRU cache and the next request can read that value.
Its example uses lru-cache with the user id as the key, a max value of 1000, and ttl: 5 * 60 * 1000. The lookup checks the cache first, then calls db.user.findUnique and stores the returned record when there is no hit.
With Vercel Fluid Compute, concurrent requests may share one function instance, so the cache can persist without Redis. Traditional serverless isolates each invocation, so cross-process caching requires Redis.
What it helps with
- share user data across sequential requests
- avoid repeating the same database lookup
- set an LRU cache entry limit
- expire cached records after a defined TTL
Example prompts for the agent
- Check this code and replace
React.cache()where data must survive across requests. - Add an
lru-cachelayer to the user lookup usingidas the key. - Explain whether this cache works for my serverless deployment or whether I need Redis.
How to install server-cache-lru
Manually: put the files in place
.claude/rules/server-cache-lru.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/server-cache-lru.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/server-cache-lru.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/server-cache-lru.mdManually: put the files in place
.clinerules/server-cache-lru.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about server-cache-lru
How do I install server-cache-lru in Claude Code?
Place the rule file at .claude/rules/server-cache-lru.md.
What does server-cache-lru do?
It guides Claude Code to use an LRU cache for data reused by separate requests. The pattern checks the cache, queries through db.user.findUnique on a miss, and stores the result.
Does this LRU cache work in traditional serverless?
Each invocation is isolated there, so processes cannot share the in-memory cache. The rule points to Redis for cross-process caching.
Source text
server-cache-lru.md · MIT
---
title: Cross-Request LRU Caching
impact: HIGH
impactDescription: caches across requests
tags: server, cache, lru, cross-request
---
## Cross-Request LRU Caching
`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:**
```typescript
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 query
```
Use 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)
© tech-leads-club · MIT · tech-leads-club/agent-skills