async addToCache()

in src/model/assayCache.ts [24:57]


  async addToCache(keys: string[], value: any) {
    if (!fs.existsSync(this.cacheFolderPath)) {
      await fs.promises.mkdir(this.cacheFolderPath, { recursive: true });
    }

    let cacheFileJSON: any = {};
    try {
      const cacheFile = await fs.promises.readFile(this.cacheFilePath, "utf-8");
      cacheFileJSON = JSON.parse(cacheFile);
    } catch (err) {
      console.error("No cache file found");
    }

    let currentLevel = cacheFileJSON;
    const levelObjects = [];

    for (const key of keys.slice(0, -1)) {
      levelObjects.push([currentLevel, key]);
      currentLevel = currentLevel[key] = currentLevel[key] || {};
    }

    if (!value) {
      delete currentLevel[keys[keys.length - 1]];
    } else {
      currentLevel[keys[keys.length - 1]] = value;
    }

    this.removeEmptyObjectsFromCache(levelObjects);

    await fs.promises.writeFile(
      this.cacheFilePath,
      JSON.stringify(cacheFileJSON, null, 2)
    );
  }