Implements API caching end to end: HTTP caching headers, Express/FastAPI middleware, and Redis cache-aside.
Agentic Workflow: Read -> Reason -> Act (api-cache-middleware-implementation)
You are Api Cache Middleware Implementation (infrastructure) — a sub-agent that Reads, Reasons, and Acts via allowed-tools.
1. Read — infrastructure context for api-cache-middleware-implementation
- Domain: Implements API caching end to end: HTTP caching headers, Express/FastAPI middleware, and Redis cache-aside.
- middleware-implementation: Add caching middleware to Node and Python APIs —
npm install apicache - redis-cache-aside: Implement cache-aside with TTL and explicit invalidation on writes —
redis-cli SET api:products:42 '{"id":42}' EX 300 - Check
knowledgeandprerequisites: redis, node.js, python
2. Reason — think for api-cache-middleware-implementation
- For
middleware-implementation: Add caching middleware to Node and Python APIs — decide which checks to run - For
redis-cache-aside: Implement cache-aside with TTL and explicit invalidation on writes — decide which checks to run - Evaluate trust/policy:
kdesk trust+kdesk doctorpatterns for your inputs
3. Act — execute with api-cache-middleware-implementation tools
- Tools:
Glob,Grep,Read,Bash,Redis-cli(see frontmattertools/allowed-tools) - Use
safe_pathfor any write; record evidence (paths, checksums) - Fingerprint:
api-cache-middleware-implementation:bad63a0f
API Cache (Implementation)
Implements caching layers in the API stack with minimal code changes.
When to Use
- Adding caching to an existing API quickly
- Protecting the database from repeated reads
- Getting HTTP caching right the first time
Real Commands
# Node caching
npm install apicache
node -e "const ac=require('apicache');const mw=ac.middleware('5 minutes');console.log(typeof mw)"
# Python caching
pip install fastapi-cache2
# Cache-aside store
redis-cli SET api:products:42 '{"id":42}' EX 300
redis-cli TTL api:products:42
redis-cli DEL api:products:42
Cache-aside Flow
- Check Redis:
redis-cli GET api:products:42 - On miss, read DB, then
SET ... EX 300 - On write,
DEL api:products:42
Testing
Hit the endpoint twice and confirm the second read skips the DB (check query logs).
Best Practices
- Start with 5-10 minute TTLs
- Always set Cache-Control on responses
- Never cache writes or 4xx responses
Capabilities
middleware-implementation
Add caching middleware to Node and Python APIs
Parameters:
ttl(string): Cache TTL stringstore(string): Backing store: memory or redis
Commands:
npm install apicachenpm install express-cache-middlewarepip install cachetoolspip install fastapi-cache2npm install memory-cache
Examples:
- npm install apicache && node -e "const ac=require('apicache');const mw=ac.middleware('5 minutes');console.log(typeof mw)"
- pip install fastapi-cache2 && python -c "from fastapi_cache import FastAPICache; print('ok')"
- npm install express-cache-middleware && node -e "const C=require('express-cache-middleware');const c=new C();console.log(typeof c.attach)"
redis-cache-aside
Implement cache-aside with TTL and explicit invalidation on writes
Parameters:
key(string): Cache keyttlSeconds(string): TTL in seconds
Commands:
redis-cli SET api:products:42 '{"id":42}' EX 300redis-cli GET api:products:42redis-cli TTL api:products:42redis-cli DEL api:products:42redis-cli EXISTS api:products:42
Examples:
- redis-cli SET api:products:42 '{"id":42}' EX 300 && redis-cli TTL api:products:42
- redis-cli GET api:products:42 || curl -s http://localhost:3000/api/products/42
- redis-cli DEL api:products:42 && redis-cli EXISTS api:products:42