UNPKG

tspace-mysql

Version:

Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.

66 lines 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RedisCache = void 0; const tools_1 = require("../../tools"); class RedisCache { _redis = tools_1.Tool.import('redis'); client; constructor(url) { this.client = this._redis.createClient({ url }); this.client.on("error", (err) => { if (err) { console.log(err); this.client.quit(); } }); this.client.connect(); } provider() { return 'redis'; } async all() { const cacheds = await this.client.keys('*'); const values = []; for (const cached in cacheds) { values.push(cached == null ? null : this._safetyJsonParse(cached)); } return values; } async exists(key) { const cached = await this.client.get(key); return cached != null; } async get(key) { const cached = await this.client.get(key); if (cached == null) return null; return this._safetyJsonParse(cached); } async set(key, value, ms) { await this.client.set(key, JSON.stringify(value), { PX: ms }); return; } async clear() { await this.client.flushAll(); return; } async delete(key) { await this.client.del(key); return; } _safetyJsonParse(value) { try { return JSON.parse(value); } catch (e) { return value; } } } exports.RedisCache = RedisCache; exports.default = RedisCache; //# sourceMappingURL=RedisCache.js.map