Skip to content
redis.ts 704 B
Newer Older
Grant's avatar
Grant committed
import { RedisClientType } from "@redis/client";
import { createClient } from "redis";
import { Logger } from "./Logger";
Grant's avatar
Grant committed

class _Redis {
  isConnected = false;
  client: RedisClientType;
Grant's avatar
Grant committed

  constructor() {
    this.client = createClient({
      url: process.env.REDIS_HOST,
    });
Grant's avatar
Grant committed
  }

  async connect() {
    if (this.isConnected)
      throw new Error("Attempted to run Redis#connect when already connected");

    await this.client.connect();
    Logger.info("Connected to Redis");
    this.isConnected = true;
  }

  async getClient() {
    if (!this.isConnected) {
      await this.connect();
      this.isConnected = true;
    }

    return this.client;
  }
}

export const Redis = new _Redis();