Remove unused translate/jpeg types packages; refresh patch updates; mark Redis/site-settings/gamedata hotel as server-only with a Vitest stub. Co-authored-by: Cursor <[email protected]>
43 lines
930 B
TypeScript
43 lines
930 B
TypeScript
import "server-only";
|
|
|
|
import Redis from "ioredis";
|
|
|
|
const globalForRedis = globalThis as unknown as { redis?: Redis | null };
|
|
|
|
function createRedis(): Redis | null {
|
|
const url = process.env.REDIS_URL;
|
|
if (!url) return null;
|
|
try {
|
|
const client = new Redis(url, {
|
|
maxRetriesPerRequest: 3,
|
|
retryStrategy(times) {
|
|
if (times > 3) return null;
|
|
return Math.min(times * 200, 2000);
|
|
},
|
|
lazyConnect: true,
|
|
});
|
|
client.on("error", () => {});
|
|
return client;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export const redis: Redis | null =
|
|
globalForRedis.redis !== undefined ? globalForRedis.redis : createRedis();
|
|
if (globalForRedis.redis === undefined) globalForRedis.redis = redis;
|
|
|
|
export async function withRedis<T>(
|
|
fallback: () => Promise<T>,
|
|
redisFn: (client: Redis) => Promise<T>,
|
|
): Promise<T> {
|
|
if (redis) {
|
|
try {
|
|
return await redisFn(redis);
|
|
} catch {
|
|
return fallback();
|
|
}
|
|
}
|
|
return fallback();
|
|
}
|