25 lines
545 B
TypeScript
25 lines
545 B
TypeScript
import Redis from "ioredis";
|
|
|
|
let redis: Redis | null = null;
|
|
|
|
function getRedisUrl(): string {
|
|
return useRuntimeConfig().redisUrl || "redis://localhost:6379";
|
|
}
|
|
|
|
export function getRedis(): Redis {
|
|
if (redis) return redis;
|
|
redis = new Redis(getRedisUrl(), { maxRetriesPerRequest: null });
|
|
return redis;
|
|
}
|
|
|
|
export function createRedisClient(): Redis {
|
|
return new Redis(getRedisUrl(), { maxRetriesPerRequest: null });
|
|
}
|
|
|
|
export async function closeRedis(): Promise<void> {
|
|
if (redis) {
|
|
await redis.quit();
|
|
redis = null;
|
|
}
|
|
}
|