branch-remover
Version:
A small application for quickly removing unnecessary branches from GitHub.
76 lines • 2.42 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileCacheProvider = void 0;
const fs_1 = __importDefault(require("fs"));
const node_cache_1 = __importDefault(require("node-cache"));
class FileCacheProvider {
constructor(path) {
this._cache = new node_cache_1.default();
this._path = path;
this.has = this.has.bind(this);
this.keys = this.keys.bind(this);
this.get = this.get.bind(this);
this.getTtl = this.getTtl.bind(this);
this.add = this.add.bind(this);
this.remove = this.remove.bind(this);
this.clearAll = this.clearAll.bind(this);
this.load = this.load.bind(this);
this.save = this.save.bind(this);
}
has(key) {
return this._cache.has(key);
}
keys() {
return this._cache.keys();
}
get(key) {
return this._cache.get(key);
}
getTtl(key) {
return this._cache.getTtl(key);
}
add(key, value, ttl) {
return this._cache.set(key, value, ttl);
}
remove(keys) {
return this._cache.del(keys);
}
clearAll() {
this._cache.flushAll();
}
async load() {
if (!this._path) {
throw new Error('"path" is required. Value must not be null or empty.');
}
this.clearAll();
if (!fs_1.default.existsSync(this._path)) {
return;
}
const data = JSON.parse(await fs_1.default.readFileSync(this._path, 'utf8')) || [];
data.forEach((item) => {
this._cache.set(item.key, item.value, (item.ttl - new Date().getTime()) / 1000);
});
}
async save() {
if (!this._path) {
throw new Error('"path" is required. Value must not be null or empty.');
}
const keys = this.keys();
const data = new Array();
keys.forEach((key) => {
data.push({
key,
ttl: this._cache.getTtl(key),
value: this._cache.get(key),
});
});
await fs_1.default.writeFileSync(this._path, JSON.stringify(data), {
encoding: 'utf8',
});
}
}
exports.FileCacheProvider = FileCacheProvider;
//# sourceMappingURL=FileCacheProvider.js.map