-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathredis.ts
More file actions
29 lines (21 loc) · 655 Bytes
/
redis.ts
File metadata and controls
29 lines (21 loc) · 655 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { Redis } from '@upstash/redis';
import { readServerEnv } from '@/lib/env/server-env';
let cachedRedis: Redis | null = null;
export function getRedisClient() {
if (cachedRedis) return cachedRedis;
const UPSTASH_REDIS_REST_URL = readServerEnv('UPSTASH_REDIS_REST_URL');
const UPSTASH_REDIS_REST_TOKEN = readServerEnv('UPSTASH_REDIS_REST_TOKEN');
if (!UPSTASH_REDIS_REST_URL || !UPSTASH_REDIS_REST_TOKEN) {
return null;
}
try {
new URL(UPSTASH_REDIS_REST_URL);
} catch {
return null;
}
cachedRedis = new Redis({
url: UPSTASH_REDIS_REST_URL,
token: UPSTASH_REDIS_REST_TOKEN,
});
return cachedRedis;
}