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 | * vinext data-cache adapter backed by Redis. Default export is the |
| 3 | * factory `virtual:vinext-cache-adapters` calls as |
| 4 | * `({ env, options }) => CacheHandler`. |
| 5 | * |
| 6 | * Speaks RESP through `connect` from `@alchemy.run/redis` — the same client |
| 7 | * Fly/Railway `*RedisHttp` layers use. One process-lifetime connection |
| 8 | * per URL. Prerender artifacts are seeded into Redis in the background. |
| 9 | */ |
| 10 | import { connect, type Connection } from "@alchemy.run/redis"; |
| 11 | import type { Arg, Reply } from "@alchemy.run/redis"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as Scope from "effect/Scope"; |
| 14 | import { makeDataCacheHandler, type DataCacheStore } from "./handler.ts"; |
| 15 | import { seedStoreFromPrerenderLogged } from "./seed.ts"; |
| 16 | import { readEnvString } from "./shared.ts"; |
| 17 | |
| 18 | export interface RedisAdapterOptions extends Record<string, unknown> { |
| 19 | /** Env var holding the Redis URL. @default "REDIS_URL" */ |
| 20 | readonly urlEnv?: string; |
| 21 | readonly appPrefix?: string; |
| 22 | /** Default key TTL in seconds. @default 2592000 (30 days) */ |
| 23 | readonly ttlSeconds?: number; |
| 24 | readonly tagCacheTtlMs?: number; |
| 25 | } |
| 26 | |
| 27 | const DEFAULT_URL_ENV = "REDIS_URL"; |
| 28 | |
| 29 | const asBulk = (reply: Reply): string | null => (typeof reply === "string" ? reply : null); |
| 30 | |
| 31 | const send = (connection: Connection, command: string, args: readonly Arg[] = []): Promise<Reply> => |
| 32 | Effect.runPromise(connection.send(command, args)); |
| 33 | |
| 34 | const connections = new Map<string, Promise<Connection>>(); |
| 35 | |
| 36 | const open = (url: string): Promise<Connection> => { |
| 37 | const existing = connections.get(url); |
| 38 | if (existing) return existing; |
| 39 | const scope = Scope.makeUnsafe(); |
| 40 | const created = Effect.runPromise(connect(url).pipe(Effect.provideService(Scope.Scope, scope))); |
| 41 | connections.set(url, created); |
| 42 | created.catch(() => connections.delete(url)); |
| 43 | return created; |
| 44 | }; |
| 45 | |
| 46 | const redisStore = (connection: Connection): DataCacheStore => ({ |
| 47 | async getText(key) { |
| 48 | return asBulk(await send(connection, "GET", [key])) ?? undefined; |
| 49 | }, |
| 50 | async putText(key, value, ttlMs) { |
| 51 | const args: Arg[] = ttlMs !== undefined && ttlMs > 0 ? [key, value, "PX", ttlMs] : [key, value]; |
| 52 | await send(connection, "SET", args); |
| 53 | }, |
| 54 | async delete(key) { |
| 55 | await send(connection, "DEL", [key]); |
| 56 | }, |
| 57 | }); |
| 58 | |
| 59 | const createRedisDataCacheAdapter = ({ |
| 60 | env, |
| 61 | options, |
| 62 | }: { |
| 63 | env?: Record<string, unknown>; |
| 64 | options?: RedisAdapterOptions; |
| 65 | }) => { |
| 66 | const urlEnv = options?.urlEnv ?? DEFAULT_URL_ENV; |
| 67 | const url = readEnvString(env, urlEnv); |
| 68 | if (url === undefined) { |
| 69 | throw new Error( |
| 70 | `[vinext] The Redis data cache adapter requires \`${urlEnv}\` in the process environment.`, |
| 71 | ); |
| 72 | } |
| 73 | let handler: ReturnType<typeof makeDataCacheHandler> | undefined; |
| 74 | const ready = open(url).then(async (connection) => { |
| 75 | const store = redisStore(connection); |
| 76 | await seedStoreFromPrerenderLogged(store, "Redis"); |
| 77 | handler = makeDataCacheHandler(store, options); |
| 78 | return handler; |
| 79 | }); |
| 80 | return { |
| 81 | async get(key: string, ctx?: Record<string, unknown>) { |
| 82 | return (handler ?? (await ready)).get(key, ctx); |
| 83 | }, |
| 84 | async set(key: string, data: Record<string, unknown> | null, ctx?: Record<string, unknown>) { |
| 85 | return (handler ?? (await ready)).set(key, data, ctx); |
| 86 | }, |
| 87 | async revalidateTag(tags: string | Array<string>) { |
| 88 | return (handler ?? (await ready)).revalidateTag(tags); |
| 89 | }, |
| 90 | resetRequestCache() { |
| 91 | handler?.resetRequestCache(); |
| 92 | }, |
| 93 | }; |
| 94 | }; |
| 95 | |
| 96 | export default createRedisDataCacheAdapter; |