UNPKG

@herbertgao/surgio

Version:

Generating rules for Surge, Clash, Quantumult like a PRO

98 lines 3.15 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.cleanCaches = exports.unifiedCache = exports.UnifiedCache = void 0; const ms_1 = __importDefault(require("ms")); const cache_manager_1 = require("cache-manager"); const cache_manager_ioredis_yet_1 = require("cache-manager-ioredis-yet"); const config_1 = require("../config"); const redis_1 = __importDefault(require("../redis")); class UnifiedCache { #type; #backend; async prepare() { if (!this.#type) { this.#type = (0, config_1.getConfig)()?.cache?.type || 'default'; } if (this.#backend) { return this.#backend; } else { switch (this.#type) { case 'redis': this.#backend = (0, cache_manager_1.caching)((0, cache_manager_ioredis_yet_1.redisInsStore)(redis_1.default.getRedis())); break; default: this.#backend = (0, cache_manager_1.caching)('memory', { ttl: (0, ms_1.default)('1d'), }); } return this.#backend; } } async getType() { await this.prepare(); return this.#type; } async getBackend() { return this.prepare(); } get = async (...args) => { const cache = await this.prepare(); return cache.get(...args); }; set = async (...args) => { const cache = await this.prepare(); return cache.set(...args); }; del = async (...args) => { const cache = await this.prepare(); return cache.del(...args); }; reset = async (...args) => { const cache = await this.prepare(); return cache.reset(...args); }; wrap = async (...args) => { const cache = await this.prepare(); return cache.wrap(...args); }; keys = async (...args) => { const cache = await this.prepare(); return cache.store.keys(...args); }; mset = async (...args) => { const cache = await this.prepare(); return cache.store.mset(...args); }; mget = async (...args) => { const cache = await this.prepare(); return cache.store.mget(...args); }; mdel = async (...args) => { const cache = await this.prepare(); return cache.store.mdel(...args); }; async has(key) { await this.prepare(); if (this.#type === 'redis') { const keys = await this.keys(); return keys.includes(key); } else { const redisClient = redis_1.default.getRedis(); const value = await redisClient.exists(key); return value === 1; } } } exports.UnifiedCache = UnifiedCache; exports.unifiedCache = new UnifiedCache(); // istanbul ignore next const cleanCaches = async () => { await exports.unifiedCache.reset(); }; exports.cleanCaches = cleanCaches; //# sourceMappingURL=cache.js.map