From f0eaeb877fc398b19515c32251fccfa2d24f5e81 Mon Sep 17 00:00:00 2001 From: arsam Date: Fri, 18 Sep 2026 23:18:56 +0330 Subject: [PATCH] Import source: vault/lib/server-redis Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality. --- vault/lib/server-redis.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 vault/lib/server-redis.ts diff --git a/vault/lib/server-redis.ts b/vault/lib/server-redis.ts new file mode 100644 index 0000000..c61dd5c --- /dev/null +++ b/vault/lib/server-redis.ts @@ -0,0 +1,36 @@ +import "server-only"; + +import { createClient, type RedisClientType } from "redis"; + +const REDIS_URL = process.env.REDIS_URL ?? "redis://127.0.0.1:6379"; + +let client: RedisClientType | null = null; +let connection: Promise | null = null; +let disabled = false; + +export async function getRedis(): Promise { + if (disabled) return null; + if (client?.isOpen) return client; + if (connection) return connection; + + connection = (async () => { + const redis = createClient({ url: REDIS_URL }); + redis.on("error", (error) => { + console.error("Redis error", error); + }); + + try { + await redis.connect(); + client = redis as RedisClientType; + return client; + } catch (error) { + console.error("Redis unavailable, using in-memory fallback", error); + disabled = true; + return null; + } finally { + connection = null; + } + })(); + + return connection; +}