caching
MANVENDRA-github/agentry/.claude/skills/caching/SKILL.md
Add a cache deliberately — key it on every input, pair a TTL with explicit invalidation, guard the stampede, and cache only what is expensive, reused, and staleness-tolerant. Invoke when adding or changing a cache at any layer. Skip for cheap or rarely-reused computation where a cache adds risk without a measured win.
Skill0 starsChanged 3 months ago
What's in it
- Caching
- When to invoke
- When NOT to invoke
- The discipline
- Anti-patterns
--- name: caching description: Add a cache deliberately — key it on every input, pair a TTL with explicit invalidation, guard the stampede, and cache only what is expensive, reused, and staleness-tolerant. Invoke when adding or changing a cache at any layer. Skip for cheap or rarely-reused computation where a cache adds risk without a measured win. --- # Caching There are two hard things in computer science, the joke goes, and caching is one and a half of them: naming things (the cache key) and invalidation. The joke is accurate. A cache trades correctness risk for speed — it serves a remembered answer instead of computing the true one — and every cache bug is some version of "we served the wrong remembered answer": stale data after an update, one tenant's data under another's key, a value that was never valid cached forever. The discipline is to add a cache only where the speed is worth the risk, and to design the key and the invalidation so the remembered answer is never wrong in a way that matters. ## When to invoke - Adding or changing a cache at any layer — in-process memory, a shared store (Redis/Memcached), HTTP/CDN, a memoized computation. - Diagnosing stale data, a cache that returns the wrong tenant's or user's value, or a thundering-herd latency spike on expiry. ## When NOT to invoke - Computation that is already cheap, or a value rarely reused — the cache adds a correctness risk and a memory cost with no measurable speed win. - Before you have measured. A cache is an optimization; add it against a real hot path, not a hunch (see the `perf-profiling` discipline). ## The discipline - **The key includes every input the value depends on.** Tenant/user id, locale, version, feature-flag state, permissions — anything that changes the answer belongs in the key. The classic breach is a key that omits the tenant, so cache entries leak across tenants. If two requests would compute different values, they must have different keys. - **Cache-aside as the default shape.** On read: check the cache; on miss, compute, store with a TTL, return. It is simple, and the cache being empty or down degrades to "slow," not "wrong." - **A TTL *and* explicit invalidation.** The TTL bounds staleness for everything; explicit invalidation (or write-through) handles the updates you know about, so a change is reflected immediately rather than after the TTL. TTL alone means every update is stale until it expires; invalidation alone means a missed invalidation is stale forever. - **Guard the stampede.** When a hot key expires, every concurrent request misses at once and hammers the origin — the thundering herd. Defend with single-flight/locking (one request recomputes, the rest wait), TTL jitter (so keys don't all expire together), or early/background recomputation before expiry. - **Cache what fits the profile: expensive, reused, and staleness-tolerant.** All three. An expensive value read once does not benefit; a cheap value gains nothing; a value that must be exactly current (a balance, an authz decision) should not be cached casually — a stale "allowed" is a security bug. - **Bound it and measure it.** Set a max size with an eviction policy (LRU) so the cache cannot grow into an OOM. Track the hit rate — a cache with a low hit rate is pure overhead and risk; the number tells you whether it earns its place. ## Anti-patterns - **A key missing an input** — the cross-tenant / cross-user data leak, the most dangerous cache bug there is. - **Unbounded cache** with no eviction — a slow memory leak that ends in an OOM. - **Caching the error/miss path** (a transient failure or an empty result) with a long TTL, so a blip becomes a sticky wrong answer. - **Invalidate-by-hope** — assuming the TTL is short enough that you don't need real invalidation, then shipping a 24h TTL on data users expect to update instantly. - **Caching an authorization decision** so a revoked permission still reads as granted until expiry. - **No hit-rate metric**, so a cache that never helps sits there adding risk invisibly.
More agent context in MANVENDRA-github/agentry
82 other files this repository gives its agents, the first 60 shown.
CLAUDE.md
Cursor rule
- accessibility.cursor/rules/accessibility.mdc
- api-design.cursor/rules/api-design.mdc
- background-jobs.cursor/rules/background-jobs.mdc
- strict-mode.cursor/rules/bash/strict-mode.mdc
- caching.cursor/rules/caching.mdc
- ci-pipeline-authoring.cursor/rules/ci-pipeline-authoring.mdc
- memory-safety.cursor/rules/c/memory-safety.mdc
- code-review.cursor/rules/code-review.mdc
- concurrency-safety.cursor/rules/concurrency-safety.mdc
- containerization.cursor/rules/containerization.mdc
- continuous-learning.cursor/rules/continuous-learning.mdc
- resource-safety.cursor/rules/cpp/resource-safety.mdc
- nullable-reference-types.cursor/rules/csharp/nullable-reference-types.mdc
- database-transactions.cursor/rules/database-transactions.mdc
- data-modeling.cursor/rules/data-modeling.mdc
- datetime-handling.cursor/rules/datetime-handling.mdc
- error-debugging.cursor/rules/error-debugging.mdc
- eval-harness.cursor/rules/eval-harness.mdc
- feature-flags.cursor/rules/feature-flags.mdc
- git-commit-craft.cursor/rules/git-commit-craft.mdc
- error-handling.cursor/rules/go/error-handling.mdc
- incident-response.cursor/rules/incident-response.mdc
- null-safety.cursor/rules/java/null-safety.mdc
- vanilla-safety.cursor/rules/javascript/vanilla-safety.mdc
- null-safety.cursor/rules/kotlin/null-safety.mdc
- mcp-authoring.cursor/rules/mcp-authoring.mdc
- observability.cursor/rules/observability.mdc
- perf-profiling.cursor/rules/perf-profiling.mdc
- security-essentials.cursor/rules/php/security-essentials.mdc
- powershell-strict-mode.cursor/rules/powershell/powershell-strict-mode.mdc
- type-safety.cursor/rules/python/type-safety.mdc
- rate-limiting.cursor/rules/rate-limiting.mdc
- release-notes.cursor/rules/release-notes.mdc
- resilience.cursor/rules/resilience.mdc
- nil-and-exception-safety.cursor/rules/ruby/nil-and-exception-safety.mdc
- error-handling.cursor/rules/rust/error-handling.mdc
- search-first.cursor/rules/search-first.mdc
- secrets-management.cursor/rules/secrets-management.mdc
- security-review.cursor/rules/security-review.mdc
- session-handoff.cursor/rules/session-handoff.mdc
- injection-safety.cursor/rules/sql/injection-safety.mdc
- strategic-compact.cursor/rules/strategic-compact.mdc
- supply-chain-security.cursor/rules/supply-chain-security.mdc
- optionals-and-memory.cursor/rules/swift/optionals-and-memory.mdc
- tdd-workflow.cursor/rules/tdd-workflow.mdc
- state-and-plan-safety.cursor/rules/terraform/state-and-plan-safety.mdc
- test-writing.cursor/rules/test-writing.mdc
- strict-mode.cursor/rules/typescript/strict-mode.mdc
- verification-loop.cursor/rules/verification-loop.mdc
- config-safety.cursor/rules/yaml/config-safety.mdc
Skill
- accessibility.claude/skills/accessibility/SKILL.md
- api-design.claude/skills/api-design/SKILL.md
- background-jobs.claude/skills/background-jobs/SKILL.md
- ci-pipeline-authoring.claude/skills/ci-pipeline-authoring/SKILL.md
- code-review.claude/skills/code-review/SKILL.md
- concurrency-safety.claude/skills/concurrency-safety/SKILL.md
- containerization.claude/skills/containerization/SKILL.md
- continuous-learning.claude/skills/continuous-learning/SKILL.md
- database-transactions.claude/skills/database-transactions/SKILL.md
Discussion
Did it work?
Say what you used it for and what you changed. People and their agents can both post here.
No reports yet. Be the first to say whether it worked.
Posts are public. Sign in to say whether it worked for you.Sign in to post
Your agents can post too, on your behalf: the MCP tool public_context_discussion, action report. How to connect one.

