Skip to main content
rule CX Codex

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

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/server-cache-lru.md
Open on GitHub

What 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-cache layer to the user lookup using id as 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.md

The paths frontmatter field limits the files

Step 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

Similar IDE rules

All IDE rules for Backend and APIs →

From the same set130

The whole set →
rendering-animate-svg-wrapper rendering-animate-svg-wrapper enforces wrapping svg elements in an outer div container for CSS animations. Enables GPU hardware acceleration across browsers. CX Codex +1 ★ 7,191repo stars rerender-derived-state-no-effect The rerender-derived-state-no-effect rule keeps derived values out of React state and avoids effects that mirror props. Use it when reviewing components for extra renders and state drift. CX Codex +1 ★ 7,191repo stars rerender-move-effect-to-event rerender-move-effect-to-event tells the agent to fire a side effect from the handler that caused it, not from useEffect watching a state flag. CX Codex +1 ★ 7,191repo stars async-dependencies async-dependencies instructs Claude Code to execute partially dependent async tasks concurrently using better-all or native promise chains rather than blocking Promise.all calls. CX Codex +1 ★ 7,191repo stars advanced-event-handler-refs advanced-event-handler-refs guides Claude Code to keep event callbacks in refs or useEffectEvent, stopping React effects from re-subscribing on every render. CX Codex +1 ★ 7,191repo stars server-parallel-fetching The server-parallel-fetching rule shows how to compose React Server Components so independent data requests can run in parallel. Use it when one component waits on another component’s fetch. CX Codex +1 ★ 7,191repo stars _sections Defines eight performance optimization sections, ranks them by impact, and guides the agent in prioritizing code improvements. Install once, reference by ID when analyzing bottlenecks. CX Codex +1 ★ 7,191repo stars client-passive-event-listeners client-passive-event-listeners instructs the agent to add { passive: true } to touch and wheel listeners, preventing scroll stutter in browser viewports. CX Codex +1 ★ 7,191repo stars

Project sponsors

Companies that keep this analytics open