$npx -y skills add rshankras/claude-code-apple-skills --skill http-cacheGenerates an HTTP caching layer with Cache-Control parsing, ETag/conditional requests, and offline fallback. Use when user wants to add response caching, offline support, or reduce API calls.
| 1 | # HTTP Cache Generator |
| 2 | |
| 3 | Generate a production HTTP caching layer that integrates with your existing networking code. Supports Cache-Control directives, ETag/Last-Modified conditional requests, stale-while-revalidate, and offline fallback. |
| 4 | |
| 5 | ## When This Skill Activates |
| 6 | |
| 7 | Use this skill when the user: |
| 8 | - Asks to "add HTTP caching" or "cache API responses" |
| 9 | - Wants "offline support" or "offline fallback" |
| 10 | - Mentions "reduce API calls" or "cache network responses" |
| 11 | - Asks about "ETag" or "conditional requests" or "304 Not Modified" |
| 12 | - Wants "stale-while-revalidate" behavior |
| 13 | |
| 14 | ## Pre-Generation Checks |
| 15 | |
| 16 | ### 1. Project Context Detection |
| 17 | - [ ] Check Swift version (requires Swift 5.9+) |
| 18 | - [ ] Check deployment target (iOS 16+ / macOS 13+) |
| 19 | - [ ] Search for existing caching implementations |
| 20 | - [ ] Identify source file locations |
| 21 | |
| 22 | ### 2. Networking Layer Detection |
| 23 | Search for existing networking code: |
| 24 | ``` |
| 25 | Glob: **/*API*.swift, **/*Client*.swift, **/*Network*.swift |
| 26 | Grep: "APIClient" or "URLSession" or "HTTPURLResponse" |
| 27 | ``` |
| 28 | |
| 29 | If `networking-layer` generator was used, detect the `APIClient` protocol and generate a decorator that wraps it. |
| 30 | |
| 31 | ### 3. Conflict Detection |
| 32 | Search for existing caching: |
| 33 | ``` |
| 34 | Glob: **/*Cache*.swift |
| 35 | Grep: "URLCache" or "ResponseCache" or "CachePolicy" |
| 36 | ``` |
| 37 | |
| 38 | If found, ask user whether to replace or extend. |
| 39 | |
| 40 | ## Configuration Questions |
| 41 | |
| 42 | Ask user via AskUserQuestion: |
| 43 | |
| 44 | 1. **Cache storage sizes?** |
| 45 | - Small (10 MB memory / 50 MB disk) |
| 46 | - Medium (25 MB memory / 100 MB disk) — recommended |
| 47 | - Large (50 MB memory / 250 MB disk) |
| 48 | |
| 49 | 2. **Caching strategy?** |
| 50 | - Respect server Cache-Control headers (standard) |
| 51 | - Cache-first with background revalidation (stale-while-revalidate) |
| 52 | - Manual per-endpoint policies |
| 53 | |
| 54 | 3. **Offline support?** |
| 55 | - Yes — serve stale cache when network unavailable |
| 56 | - No — only cache while online |
| 57 | |
| 58 | 4. **Integration style?** |
| 59 | - Decorator wrapping existing APIClient (recommended if networking-layer exists) |
| 60 | - Standalone cache you call directly |
| 61 | |
| 62 | ## Generation Process |
| 63 | |
| 64 | ### Step 1: Read Templates |
| 65 | Read `http-cache-patterns.md` for architecture guidance. |
| 66 | Read `templates.md` for production Swift code. |
| 67 | |
| 68 | ### Step 2: Create Core Files |
| 69 | Generate these files: |
| 70 | 1. `HTTPCacheConfiguration.swift` — Memory/disk sizes, default policy |
| 71 | 2. `CachePolicy.swift` — Per-endpoint enum (default, noCache, forceCache, cacheFirst) |
| 72 | 3. `CacheControlHeader.swift` — Cache-Control header parser |
| 73 | 4. `ConditionalRequestHandler.swift` — ETag/Last-Modified/304 handling |
| 74 | 5. `HTTPResponseCache.swift` — Protocol + disk-backed response store |
| 75 | |
| 76 | ### Step 3: Create Integration Files |
| 77 | Based on configuration: |
| 78 | - `CachingAPIClient.swift` — Decorator wrapping existing APIClient (if decorator style) |
| 79 | - `NetworkReachability.swift` — NWPathMonitor wrapper (if offline support selected) |
| 80 | |
| 81 | ### Step 4: Determine File Location |
| 82 | Check project structure: |
| 83 | - If `Sources/Networking/` exists → `Sources/Networking/Cache/` |
| 84 | - If `App/Networking/` exists → `App/Networking/Cache/` |
| 85 | - If `Networking/` exists → `Networking/Cache/` |
| 86 | - Otherwise → `Cache/` |
| 87 | |
| 88 | ## Output Format |
| 89 | |
| 90 | After generation, provide: |
| 91 | |
| 92 | ### Files Created |
| 93 | ``` |
| 94 | Networking/Cache/ |
| 95 | ├── HTTPCacheConfiguration.swift # Memory/disk sizes, default policy |
| 96 | ├── CachePolicy.swift # Per-endpoint caching enum |
| 97 | ├── CacheControlHeader.swift # Cache-Control header parser |
| 98 | ├── ConditionalRequestHandler.swift # ETag/Last-Modified/304 |
| 99 | ├── HTTPResponseCache.swift # Protocol + disk implementation |
| 100 | ├── CachingAPIClient.swift # Decorator for existing APIClient |
| 101 | └── NetworkReachability.swift # NWPathMonitor (optional) |
| 102 | ``` |
| 103 | |
| 104 | ### Integration Steps |
| 105 | |
| 106 | **Wrap your existing client:** |
| 107 | ```swift |
| 108 | let baseClient = URLSessionAPIClient(configuration: .production) |
| 109 | let cachingClient = CachingAPIClient( |
| 110 | wrapping: baseClient, |
| 111 | cache: DiskHTTPResponseCache(), |
| 112 | configuration: .default |
| 113 | ) |
| 114 | |
| 115 | // Use cachingClient everywhere you used baseClient |
| 116 | let users = try await cachingClient.request(UsersEndpoint()) |
| 117 | ``` |
| 118 | |
| 119 | **Per-endpoint cache policy:** |
| 120 | ```swift |
| 121 | struct UsersEndpoint: APIEndpoint, CacheConfigurable { |
| 122 | var cachePolicy: CachePolicy { .cacheFirst(maxAge: 300) } |
| 123 | } |
| 124 | |
| 125 | struct OrdersEndpoint: APIEndpoint, CacheConfigurable { |
| 126 | var cachePolicy: CachePolicy { .noCache } |
| 127 | } |
| 128 | ``` |
| 129 | |
| 130 | **With SwiftUI:** |
| 131 | ```swift |
| 132 | struct UsersView: View { |
| 133 | @Environment(\.apiClient) private var apiClient |
| 134 | |
| 135 | var body: some View { |
| 136 | List(users) { user in |
| 137 | Text(user.name) |
| 138 | } |
| 139 | .task { |
| 140 | // Automatically uses cache if available |
| 141 | users = try await apiClient.request(UsersEndpoint()) |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | ``` |
| 146 | |
| 147 | ### Testing |
| 148 | |
| 149 | ```swift |