UNPKG

soccer-go

Version:

Soccer CLI for stats and results.

46 lines (45 loc) 1.82 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const config_1 = __importDefault(require("../config")); const CacheItem_1 = __importDefault(require("./CacheItem")); const errors_1 = require("../utils/errors"); class Cache { constructor(dir) { this.data = new Map(); this.add = (id, data) => { const res = this.data.set(id, new CacheItem_1.default(Date.now(), data)); return res; }; this.remove = (id) => { const res = this.data.delete(id); return res; }; this.get = (id) => this.data.get(id); this.has = (id) => this.data.has(id); this.persist = () => { fs_1.default.writeFileSync(this.file, JSON.stringify(Array.from(this.data))); }; this.file = path_1.default.join(dir, config_1.default.cache.fileName); try { const buffer = fs_1.default.readFileSync(this.file); this.data = new Map(JSON.parse(buffer.toString('utf-8'))); } catch (error) { if ((0, errors_1.isErrorNodeSystemError)(error) && error.code === 'ENOENT') { // Cache file does not exist, so create it. this.data = new Map(); fs_1.default.writeFileSync(this.file, JSON.stringify(Array.from(this.data))); } else { // Some other error occurred but since it's about caching // we will ignore it and let the application work without it. } } } } exports.default = Cache;