use-cache

Implements function-level caching with the "use cache" directive for RSC functions in @rangojs/router.

Updated Nov 7, 2025
One-click install
npx skills add https://github.com/rangojs/rango --skill use-cache-rangojs
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: use-cache
Source: https://github.com/rangojs/rango/tree/main/packages/rangojs-router/skills/use-cache
Command: npx skills add https://github.com/rangojs/rango --skill use-cache-rangojs

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Caching individual data-fetching functions or RSC components in a React Server Components router requires manual memoization logic, cache key management, and stale-while-revalidate handling. This Skill provides the "use cache" directive so a single function or component memoizes its own output with TTL and SWR semantics, without caching an entire route subtree or HTTP response. ## Core Features & Use Cases - Directive-based caching: Add "use cache" at file level or "use cache: <profile>" per function to cache return values, with named profiles (ttl, swr, tags, foregroundOnAction) defined in createRouter({ cacheProfiles }). - Request safety guards: cookies() and headers() throw inside cached functions to prevent cross-user data leaks, while tainted ctx objects are excluded from cache keys and route-identifying fields are folded in automatically. - Handle replay and inline server actions: ctx.use(Handle) side effects are captured on cache miss and replayed on hit, and inline "use server" actions embedded in cached components stay invocable with frozen closure values. - Use Case: Cache a database query like getProducts() with a short profile so repeated requests across routes reuse the result, while a mutation calls updateTag() to hard-purge the entry for read-your-own-writes consistency. ## Quick Start Add the "use cache" directive to my async data function and configure a named cache profile with ttl and swr in createRouter.

Frequently Asked Questions about use-cache

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I cache a single function in a React Server Components app?

Add the "use cache" directive at the top of the file or inside an individual async function, optionally with a named profile like "use cache: short". A Vite transform wraps the export with registerCachedFunction so its return value is memoized with TTL and stale-while-revalidate behavior.

What is the difference between "use cache" and the cache() route DSL?

"use cache" caches individual functions or components at runtime, while the cache() DSL caches an entire route segment subtree including children. Use "use cache" for granular data-fetch memoization and cache() when a whole rendered subtree should be stored.

Why do cookies() and headers() throw inside a "use cache" function?

Per-request values like cookies and headers are not part of the cache key, so reading them inside a cached function could serve one user's data to another. Extract the value before the cached function and pass it as an argument so it is included in the cache key.

Can I use a cached function as middleware or a Static handler?

No. Passing a cached function to middleware() throws at boot because middleware must run on every request, and using one as a Static() or Prerender() handler throws because build-time rendering already caches. Call the cached function inside the middleware or handler instead.

How do I invalidate cached entries after a mutation?

Tag entries via cache profiles or cacheTag(), then call updateTag(...tags) inside a server action for an awaitable hard purge with read-your-own-writes semantics. Use revalidateTag(...tags) for non-blocking background invalidation from route handlers or webhooks.