The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import * as Duration from "effect/Duration"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Schema from "effect/Schema"; |
| 5 | import { Cache } from "./Bindings.ts"; |
| 6 | |
| 7 | /** KV's floor for both `expirationTtl` and `cacheTtl`. */ |
| 8 | const MIN_TTL = Duration.minutes(1); |
| 9 | |
| 10 | const seconds = (duration: Duration.Duration) => Math.floor(Duration.toSeconds(duration)); |
| 11 | |
| 12 | /** |
| 13 | * A lookup-through cache on the registry's KV namespace, shared by every |
| 14 | * isolate of the Worker. |
| 15 | * |
| 16 | * Concurrent misses each run the lookup and each write; the last write wins. |
| 17 | * That is deliberate: a cache that shares an in-flight lookup, like Effect's |
| 18 | * `Cache`, has later callers join the first caller's fiber, and workerd drops |
| 19 | * a fiber resumed from another request's context once that request |
| 20 | * completes, which surfaces as the joining request hanging. |
| 21 | * |
| 22 | * Reads set `cacheTtl`, so a value can still be served from the colo's edge |
| 23 | * cache for up to a minute after it expired; a lookup's `ttl` must leave |
| 24 | * that margin. Values with less than a minute left are not stored. KV |
| 25 | * failures are logged and fall through to the lookup. |
| 26 | */ |
| 27 | export const make = <K, A, I, E, R>( |
| 28 | key: (input: K) => string, |
| 29 | value: Schema.Codec<A, I>, |
| 30 | lookup: (input: K) => Effect.Effect<{ value: A; ttl: Duration.Duration }, E, R>, |
| 31 | ) => { |
| 32 | const json = Schema.fromJsonString(value); |
| 33 | const decode = Schema.decodeUnknownEffect(json); |
| 34 | const encode = Schema.encodeEffect(json); |
| 35 | return (input: K) => |
| 36 | Effect.gen(function* () { |
| 37 | const kv = yield* Cloudflare.KV.ReadWriteNamespace(Cache); |
| 38 | const id = key(input); |
| 39 | const hit = yield* kv.get(id, { cacheTtl: seconds(MIN_TTL) }).pipe( |
| 40 | Effect.flatMap((text) => (text === null ? Effect.undefined : decode(text))), |
| 41 | Effect.catch((e) => |
| 42 | Effect.logWarning(`cache read of ${id} failed: ${e.message}`).pipe(Effect.as(undefined)), |
| 43 | ), |
| 44 | ); |
| 45 | if (hit !== undefined) { |
| 46 | return hit; |
| 47 | } |
| 48 | const fresh = yield* lookup(input); |
| 49 | if (Duration.isGreaterThanOrEqualTo(fresh.ttl, MIN_TTL)) { |
| 50 | yield* encode(fresh.value).pipe( |
| 51 | Effect.flatMap((text) => kv.put(id, text, { expirationTtl: seconds(fresh.ttl) })), |
| 52 | Effect.catch((e) => Effect.logWarning(`cache write of ${id} failed: ${e.message}`)), |
| 53 | ); |
| 54 | } |
| 55 | return fresh.value; |
| 56 | }); |
| 57 | }; |