agentleFS
RidhuanDEVRidhuanDEV/claude-skills/shadow-cache/SKILL.md

shadow-cache

Skill1 starsChanged yesterdayMITView on GitHub

Client & Server Caching Architecture. Use when implementing or debugging TanStack Query/SWR invalidation, Redis caching, cache stampede protection, optimistic UI updates, or Idempotency-Key patterns.

The file

We split this skill into the rules it gives an agent

7 rules. About 0 are common, 1 is distinctive, and 6 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 6

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use this skill when designing or debugging data fetching, caching layers (TanStack Query, SWR, Redis, HTTP Cache-Control), cache …

    line 8
  2. Distinctive:

    Never scatter ad-hoc string arrays across components.

    line 15
  3. Very specific:

    Every useMutation (POST, PUT, PATCH, DELETE) MUST invalidate affected list and detail keys in onSuccess (or roll back cleanly in onError …

    line 26
  4. Very specific:

    Keep Zustand/Context strictly for true client UI state (sidebar open, theme, multi-step wizard draft).

    line 28
  5. Very specific:

    Every cached key (except explicit persistent queues/locks) MUST have a TTL (EX).

    line 38
  6. Very specific:

    When an ID lookup misses in the DB, cache a short-lived sentinel ("__NULL__" with EX 30) to protect the database against repeated lookups …

    line 42
  7. Very specific:

    If a duplicate request arrives while "PROCESSING", return 409 Conflict.

    line 51

≈ “Common” is an estimate: at least 1 in 5 of the 364,980 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Shadow Cache (/shadow-cache) — Client/Server Caching, Invalidation & Realtime Sync

Use this skill when designing or debugging data fetching, caching layers (TanStack Query, SWR, Redis, HTTP Cache-Control), cache invalidation, optimistic UI updates, or request idempotency.


1. Client-Side Server-State Management (TanStack Query / SWR)

  1. Structured Query Keys Factory:
    • Never scatter ad-hoc string arrays across components. Define a typed query-key factory per domain:
      export const orderKeys = {
        all: ['orders'] as const,
        lists: () => [...orderKeys.all, 'list'] as const,
        list: (filters: OrderFilterDto) => [...orderKeys.lists(), filters] as const,
        details: () => [...orderKeys.all, 'detail'] as const,
        detail: (id: string) => [...orderKeys.details(), id] as const,
      };
  2. Deterministic Mutation Invalidation:
    • Every useMutation (POST, PUT, PATCH, DELETE) MUST invalidate affected list and detail keys in onSuccess (or roll back cleanly in onError when using optimistic updates).
  3. Never Duplicate Server State in Global Client Stores (Zustand/Redux):
    • Keep Zustand/Context strictly for true client UI state (sidebar open, theme, multi-step wizard draft); let TanStack Query own API responses, caching, background refetching, and pagination.

2. Server-Side Redis & HTTP Caching Patterns

  1. Key Naming & Tenant Isolation:
    • Prefix every Redis key with environment, tenant, domain, and schema version: prod:tenant_42:user_profile:v1:user_99.
    • Never cache tenant-specific data under a global key without tenant_id or user_id!
  2. Explicit TTL + Jitter (Anti-Cache-Avalanche):
    • Every cached key (except explicit persistent queues/locks) MUST have a TTL (EX). Add random jitter ($\pm 10%$) to TTLs on bulk-warmed keys so thousands of keys do not expire at the exact same second.
  3. Cache Stampede / Thundering Herd Protection:
    • For expensive aggregation/dashboard queries, use single-flight locking (mutex lock or stale-while-revalidate) so 200 concurrent requests on a cache miss trigger only 1 database query instead of 200.
  4. Negative Caching:
    • When an ID lookup misses in the DB, cache a short-lived sentinel ("__NULL__" with EX 30) to protect the database against repeated lookups for non-existent IDs.

3. Idempotency Keys for State-Mutating Endpoints (Idempotency-Key)

For payment, order creation, approval, or webhook endpoints:

  • Accept an Idempotency-Key header (or deterministic event ID).
  • Store { status: "PROCESSING" | "COMPLETED", statusCode, responseBody } in Redis/DB with a 24-hour TTL using atomic SET NX / unique constraint.
  • If a duplicate request arrives while "PROCESSING", return 409 Conflict; if "COMPLETED", return the stored response without re-executing the business transaction.
  1. Coherency Boundaries & Multi-Tier Invalidation:
    • Separate public catalog caches from tenant/user-authorized keys (user_id/tenant_id in cache key mandatory to prevent cross-user leakage).
    • Define exact invalidation triggers across CDN/edge, Redis, and client TanStack Query caches so stale authorization or balance views never persist past security/mutation events.

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.