Skip to content

commandBackend and APIs

Add Caching

Adds caching to a hot read path with clear keys, an expiry and invalidation on writes, after proving the path is hot.

npx workspacetemplates add add-caching --tool claude-code

Writes 1 file to .claude/commands/

.claude/commands/add-caching.md16 lines · 1.1 KBFile
---
description: Adds caching to a hot read path with clear keys, an expiry and invalidation on writes, after proving the path is hot.
argument-hint: "[function or route to cache]"
---

Add caching to: $ARGUMENTS

1. Read the code and its callers. Confirm it is worth caching: estimate how often it is read versus written, and how costly each call is. If it is cheap or write-heavy, say so and stop.
2. Use the cache already in the project. Do not add Redis or a new package without asking.
3. Build the key from every input that changes the result (ids, locale, filters, tenant, user role). Prefix it with a version string so a format change can invalidate everything.
4. Pick a TTL from how stale the data may be, and add jitter so many keys do not expire at once. Protect against a stampede with a lock or request coalescing when the miss is expensive.
5. Invalidate on every write path that changes the source data. List those paths explicitly.
6. Never cache errors or empty results from failures.
7. Add tests for hit, miss, expiry and invalidation, then run them.

Report the key format, TTL, invalidation points and the risk of stale reads.

What it does

A cache is only a good idea when you know what makes it stale. This command asks where the read is hot, picks a key that includes everything the result depends on, and ties invalidation to the writes that change the data.

It uses the cache layer already in the project, such as Redis, an in-process map or a CDN header, and avoids caching anything that is per-user unless the user id is in the key.

Example

you › /add-caching getProductPrice in src/pricing/service.ts

agent › Read:write ratio is about 200:1. Key v1:price:{sku}:{currency}, TTL 300s with 10% jitter.
Invalidated in updatePrice() and importCatalog(). 4 tests pass.
Risk: price changes through the admin SQL console would stay stale for up to 5 minutes.

Questions

Does it add Redis for me?
No. It reuses the cache you already run and asks before adding any new infrastructure.
How does it avoid showing one user another user's data?
Anything that depends on the user, role or tenant goes into the cache key, and it refuses to cache results it cannot key safely.
  • Add Endpoint

    CommandBackend and APIs

    Free
  • Add Health Check

    CommandBackend and APIs

    Free
  • Add Pagination

    CommandBackend and APIs

    Free
  • Add Rate Limit

    CommandBackend and APIs

    Free