The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | /** |
| 2 | * Shared vinext CacheHandler over a string KV-shaped store. |
| 3 | * |
| 4 | * Redis and S3 adapters only differ in how they persist strings. The |
| 5 | * ISR / `"use cache"` codec (tags, stale-while-revalidate, ArrayBuffers) |
| 6 | * lives here once so the three runtimes cannot drift. |
| 7 | */ |
| 8 | import { |
| 9 | isUnknownRecord, |
| 10 | keySpace, |
| 11 | readCacheControlNumberField, |
| 12 | readPositiveNumberField, |
| 13 | readStringArrayField, |
| 14 | restoreArrayBuffers, |
| 15 | serializeForJSON, |
| 16 | validateCacheEntry, |
| 17 | validateTag, |
| 18 | validUniqueTags, |
| 19 | type StoredCacheControl, |
| 20 | } from "./shared.ts"; |
| 21 | |
| 22 | export interface DataCacheStore { |
| 23 | getText(key: string): Promise<string | undefined>; |
| 24 | putText(key: string, value: string, ttlMs?: number): Promise<void>; |
| 25 | delete(key: string): Promise<void>; |
| 26 | } |
| 27 | |
| 28 | export interface DataCacheHandlerOptions { |
| 29 | readonly appPrefix?: string; |
| 30 | /** Default key TTL in seconds. @default 2592000 (30 days) */ |
| 31 | readonly ttlSeconds?: number; |
| 32 | readonly tagCacheTtlMs?: number; |
| 33 | } |
| 34 | |
| 35 | const DEFAULT_TTL_SECONDS = 2592000; |
| 36 | |
| 37 | export const makeDataCacheHandler = (store: DataCacheStore, options?: DataCacheHandlerOptions) => { |
| 38 | const keys = keySpace(options?.appPrefix); |
| 39 | const ttlSeconds = options?.ttlSeconds ?? DEFAULT_TTL_SECONDS; |
| 40 | const tagCacheTtl = options?.tagCacheTtlMs ?? 5_000; |
| 41 | const tagCache = new Map<string, { timestamp: number; fetchedAt: number }>(); |
| 42 | |
| 43 | const ttlMsFor = (expireAt: number | null, now: number) => |
| 44 | expireAt !== null ? Math.max(expireAt - now, 1000) : ttlSeconds * 1000; |
| 45 | |
| 46 | const hasRevalidatedTag = async (tags: Array<string>, lastModified: number) => { |
| 47 | if (tags.length === 0) return false; |
| 48 | const now = Date.now(); |
| 49 | const uncached: Array<string> = []; |
| 50 | for (const tag of tags) { |
| 51 | const cached = tagCache.get(tag); |
| 52 | if (cached && now - cached.fetchedAt < tagCacheTtl) { |
| 53 | if (Number.isNaN(cached.timestamp) || cached.timestamp >= lastModified) { |
| 54 | return true; |
| 55 | } |
| 56 | } else { |
| 57 | uncached.push(tag); |
| 58 | } |
| 59 | } |
| 60 | if (uncached.length === 0) return false; |
| 61 | const results = await Promise.all(uncached.map((tag) => store.getText(keys.tagKey(tag)))); |
| 62 | for (let i = 0; i < uncached.length; i++) { |
| 63 | const raw = results[i]; |
| 64 | const timestamp = raw !== undefined ? Number(raw) : 0; |
| 65 | tagCache.set(uncached[i]!, { timestamp, fetchedAt: now }); |
| 66 | if (timestamp !== 0 && (Number.isNaN(timestamp) || timestamp >= lastModified)) { |
| 67 | return true; |
| 68 | } |
| 69 | } |
| 70 | return false; |
| 71 | }; |
| 72 | |
| 73 | return { |
| 74 | async get(key: string, ctx?: Record<string, unknown>) { |
| 75 | const storageKey = keys.entryKey(key); |
| 76 | const raw = await store.getText(storageKey); |
| 77 | if (raw === undefined) return null; |
| 78 | let parsed: unknown; |
| 79 | try { |
| 80 | parsed = JSON.parse(raw); |
| 81 | } catch { |
| 82 | await store.delete(storageKey); |
| 83 | return null; |
| 84 | } |
| 85 | const entry = validateCacheEntry(parsed); |
| 86 | if (!entry) { |
| 87 | await store.delete(storageKey); |
| 88 | return null; |
| 89 | } |
| 90 | let restoredValue = null; |
| 91 | if (entry.value && isUnknownRecord(entry.value)) { |
| 92 | restoredValue = restoreArrayBuffers(entry.value); |
| 93 | if (!restoredValue) { |
| 94 | await store.delete(storageKey); |
| 95 | return null; |
| 96 | } |
| 97 | } |
| 98 | if (await hasRevalidatedTag(validUniqueTags(entry.tags), entry.lastModified)) { |
| 99 | await store.delete(storageKey); |
| 100 | return null; |
| 101 | } |
| 102 | const softTags = validUniqueTags(readStringArrayField(ctx, "softTags")); |
| 103 | if (await hasRevalidatedTag(softTags, entry.lastModified)) return null; |
| 104 | if (entry.expireAt !== null && Date.now() > entry.expireAt) { |
| 105 | await store.delete(storageKey); |
| 106 | return null; |
| 107 | } |
| 108 | const now = Date.now(); |
| 109 | const requestedRevalidate = readPositiveNumberField(ctx, "revalidate"); |
| 110 | const requestedRevalidateAt = |
| 111 | requestedRevalidate === undefined ? null : entry.lastModified + requestedRevalidate * 1000; |
| 112 | if ( |
| 113 | (entry.revalidateAt !== null && now > entry.revalidateAt) || |
| 114 | (requestedRevalidateAt !== null && now > requestedRevalidateAt) |
| 115 | ) { |
| 116 | return { |
| 117 | lastModified: entry.lastModified, |
| 118 | value: restoredValue, |
| 119 | cacheState: "stale", |
| 120 | cacheControl: entry.cacheControl, |
| 121 | }; |
| 122 | } |
| 123 | return { |
| 124 | lastModified: entry.lastModified, |
| 125 | value: restoredValue, |
| 126 | cacheControl: entry.cacheControl, |
| 127 | }; |
| 128 | }, |
| 129 | |
| 130 | async set(key: string, data: Record<string, unknown> | null, ctx?: Record<string, unknown>) { |
| 131 | const tagSet = new Set<string>(); |
| 132 | if (data && Array.isArray(data.tags)) { |
| 133 | for (const tag of data.tags) { |
| 134 | if (typeof tag === "string") { |
| 135 | const valid = validateTag(tag); |
| 136 | if (valid) tagSet.add(valid); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | if (ctx && Array.isArray(ctx.tags)) { |
| 141 | for (const tag of ctx.tags) { |
| 142 | if (typeof tag === "string") { |
| 143 | const valid = validateTag(tag); |
| 144 | if (valid) tagSet.add(valid); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | let effectiveRevalidate = readCacheControlNumberField(ctx, "revalidate"); |
| 149 | const effectiveExpire = readCacheControlNumberField(ctx, "expire"); |
| 150 | const effectiveStale = readCacheControlNumberField(ctx, "stale"); |
| 151 | if (data && typeof data.revalidate === "number") { |
| 152 | effectiveRevalidate = data.revalidate; |
| 153 | } |
| 154 | if (effectiveRevalidate === 0) return; |
| 155 | const now = Date.now(); |
| 156 | const revalidateAt = |
| 157 | typeof effectiveRevalidate === "number" && effectiveRevalidate > 0 |
| 158 | ? now + effectiveRevalidate * 1000 |
| 159 | : null; |
| 160 | const expireAt = |
| 161 | typeof effectiveExpire === "number" && effectiveExpire > 0 |
| 162 | ? now + effectiveExpire * 1000 |
| 163 | : null; |
| 164 | const cacheControl: StoredCacheControl | undefined = |
| 165 | typeof effectiveRevalidate === "number" |
| 166 | ? { |
| 167 | revalidate: effectiveRevalidate, |
| 168 | ...(effectiveExpire === undefined ? {} : { expire: effectiveExpire }), |
| 169 | ...(effectiveStale === undefined ? {} : { stale: effectiveStale }), |
| 170 | } |
| 171 | : undefined; |
| 172 | const entry = { |
| 173 | value: data ? serializeForJSON(data) : null, |
| 174 | tags: [...tagSet], |
| 175 | lastModified: now, |
| 176 | revalidateAt, |
| 177 | expireAt, |
| 178 | cacheControl, |
| 179 | }; |
| 180 | await store.putText(keys.entryKey(key), JSON.stringify(entry), ttlMsFor(expireAt, now)); |
| 181 | }, |
| 182 | |
| 183 | async revalidateTag(tags: string | Array<string>) { |
| 184 | const tagList = Array.isArray(tags) ? tags : [tags]; |
| 185 | const now = Date.now(); |
| 186 | const validTags = tagList.filter((tag) => validateTag(tag) !== null); |
| 187 | const ttlMs = ttlSeconds * 1000; |
| 188 | await Promise.all( |
| 189 | validTags.map((tag) => store.putText(keys.tagKey(tag), String(now), ttlMs)), |
| 190 | ); |
| 191 | for (const tag of validTags) { |
| 192 | tagCache.set(tag, { timestamp: now, fetchedAt: now }); |
| 193 | } |
| 194 | }, |
| 195 | |
| 196 | resetRequestCache() { |
| 197 | tagCache.clear(); |
| 198 | }, |
| 199 | }; |
| 200 | }; |