agentleFS
harrissohail6harrissohail6/claude-code-config/skills/caching-strategies/SKILL.md

caching-strategies

Skill1 starsChanged 11 months agoMITView on GitHub

Caching patterns and strategies. Auto-activates when implementing caching, Redis, or performance optimization.

Files
SKILL.md

Caching Strategies

Cache Patterns

Cache-Aside (Lazy Loading)

async function getUser(id: string): Promise<User> {
  // Check cache first
  const cached = await redis.get(`user:${id}`);
  if (cached) return JSON.parse(cached);

  // Cache miss - fetch from DB
  const user = await db.users.findById(id);

  // Store in cache
  await redis.setex(`user:${id}`, 3600, JSON.stringify(user));

  return user;
}

Write-Through

async function updateUser(id: string, data: Partial<User>) {
  // Update DB
  const user = await db.users.update(id, data);

  // Update cache
  await redis.setex(`user:${id}`, 3600, JSON.stringify(user));

  return user;
}

Write-Behind (Async)

async function updateUser(id: string, data: Partial<User>) {
  // Update cache immediately
  await redis.setex(`user:${id}`, 3600, JSON.stringify(data));

  // Queue DB update (async)
  await queue.add('updateUser', { id, data });

  return data;
}

Cache Invalidation

// Invalidate on update
async function deleteUser(id: string) {
  await db.users.delete(id);
  await redis.del(`user:${id}`);
}

// Pattern-based invalidation
await redis.del('users:*'); // Delete all user caches

HTTP Caching

// Cache-Control headers
res.set('Cache-Control', 'public, max-age=3600'); // Cache 1 hour
res.set('Cache-Control', 'private, no-cache');    // No caching
res.set('Cache-Control', 'public, max-age=31536000, immutable'); // Static assets

// ETag for conditional requests
const etag = generateETag(data);
res.set('ETag', etag);

if (req.headers['if-none-match'] === etag) {
  return res.status(304).end(); // Not Modified
}

Cache TTL Guidelines

Data TypeTTL
Static assets1 year
User sessions24 hours
API responses5-15 minutes
Frequently changing30-60 seconds

Redis Data Structures

// String - Simple values
await redis.setex('key', 3600, 'value');

// Hash - Objects
await redis.hset('user:1', { name: 'John', email: 'john@example.com' });

// List - Queues
await redis.lpush('queue', 'item');

// Set - Unique values
await redis.sadd('tags', 'javascript', 'typescript');

// Sorted Set - Rankings
await redis.zadd('leaderboard', 100, 'player1');

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.