# API Cache Middleware Implementation

> Implements API caching end to end: HTTP caching headers, Express/FastAPI middleware, and Redis cache-aside. Use when working with middleware implementation, redis cache aside or when the user mentions middleware implementation, redis cache aside.

- Skill: `opknock/api-cache-middleware-implementation` (Agent Skill)
- Install (CLI): `npx get-skill add opknock/api-cache-middleware-implementation`
- Raw SKILL.md: https://api.skillmd.com/api/skills/opknock/api-cache-middleware-implementation/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Integrations & APIs
- License: MIT
- Author: OpKnock (https://skillmd.com/u/opknock)
- Updated: 2026-11-23
- Page: https://skillmd.com/skills/opknock/api-cache-middleware-implementation

---


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 `knowledge` and `prerequisites: 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 doctor` patterns for your inputs

### 3. Act — execute with `api-cache-middleware-implementation` tools
- Tools: `Glob`, `Grep`, `Read`, `Bash`, `Redis-cli` (see frontmatter `tools`/`allowed-tools`)
- Use `safe_path` for 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

```bash
# 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
1. Check Redis: `redis-cli GET api:products:42`
2. On miss, read DB, then `SET ... EX 300`
3. 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 string
- `store` (string): Backing store: memory or redis

**Commands:**
- `npm install apicache`
- `npm install express-cache-middleware`
- `pip install cachetools`
- `pip install fastapi-cache2`
- `npm 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 key
- `ttlSeconds` (string): TTL in seconds

**Commands:**
- `redis-cli SET api:products:42 '{"id":42}' EX 300`
- `redis-cli GET api:products:42`
- `redis-cli TTL api:products:42`
- `redis-cli DEL api:products:42`
- `redis-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

## References
- [apicache](https://github.com/kwhitley/apicache)
- [fastapi-cache2](https://github.com/long2ice/fastapi-cache)
- [Redis Commands](https://redis.io/docs/latest/commands/)

