cache.ts 777 Bytes
Newer Older
silver47gin's avatar
silver47gin committed
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 30 31 32 33 34 35 36 37 38 39
import { existsSync, mkdirSync, writeFileSync, readFileSync } from "fs";
import { join } from "path";

interface Cache {
  access_token?: string;
}

class CacheManager {
  cache: Cache = {};
  TEMP_DIR = join(process.cwd(), ".temp");
  TEMP_FILE = join(this.TEMP_DIR, "server_cache");

  constructor() {
    this.init();
  }

  init() {
    if (!existsSync(this.TEMP_DIR)) {
      mkdirSync(this.TEMP_DIR);
    }
    if (!existsSync(this.TEMP_FILE)) {
      writeFileSync(this.TEMP_FILE, "{}", {
        flag: "w",
      });
    }
  }

  load() {
    this.cache = JSON.parse(readFileSync(this.TEMP_FILE).toString());
  }

  save() {
    writeFileSync(this.TEMP_FILE, JSON.stringify(this.cache), {
      flag: "w",
    });
  }
}

export const cacheManager = new CacheManager();