Import source: vault/lib/server-redis

Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality.
This commit is contained in:
arsam
2026-09-18 23:18:56 +03:30
parent 83be0ccef2
commit f0eaeb877f
+36
View File
@@ -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<RedisClientType | null> | null = null;
let disabled = false;
export async function getRedis(): Promise<RedisClientType | null> {
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;
}