UNPKG

lakutata

Version:

An IoC-based universal application framework.

144 lines (131 loc) 3.42 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const e = require("events"); const t = require("memjs"); const s = require("./Package.65.cjs"); require("buffer"); const r = e => e && e.__esModule ? e : { default: e }; const i = r(e); const a = r(t); var n = class extends i.default { ttlSupport=true; namespace; client; opts; constructor(e, t) { super(); t = { ...typeof e === "string" ? { uri: e } : e, ...t }; if (t.uri && t.url === void 0) { t.url = t.uri; } if (e === void 0) { e = "localhost:11211"; t.url = t.uri = e; } this.opts = t; this.client = a.default.Client.create(e, t); } _getNamespace() { return `namespace:${this.namespace}`; } async get(e) { return new Promise(((t, r) => { this.client.get(this.formatKey(e), ((e, i) => { if (e) { this.emit("error", e); r(e); } else { let e; if (i === null) { e = { value: void 0, expires: 0 }; } else { e = this.opts.deserialize ? this.opts.deserialize(i) : s.defaultDeserialize(i); } t(e); } })); })); } async getMany(e) { const t = []; for (const s of e) { t.push(this.get(s)); } return Promise.allSettled(t).then((e => { const t = []; for (const s of e) { t.push(s.value); } return t; })); } async set(e, t, s) { const r = {}; if (s !== void 0) { r.expires = r.ttl = Math.floor(s / 1e3); } await this.client.set(this.formatKey(e), t, r); } async delete(e) { return new Promise(((t, s) => { this.client.delete(this.formatKey(e), ((e, r) => { if (e) { this.emit("error", e); s(e); } else { t(Boolean(r)); } })); })); } async deleteMany(e) { const t = e.map((async e => this.delete(e))); const s = await Promise.allSettled(t); return s.every((e => e.value === true)); } async clear() { return new Promise(((e, t) => { this.client.flush((s => { if (s) { this.emit("error", s); t(s); } else { e(void 0); } })); })); } formatKey(e) { let t = e; if (this.namespace) { t = this.namespace.trim() + ":" + e.trim(); } return t; } async has(e) { return new Promise((t => { this.client.get(this.formatKey(e), ((e, s) => { if (e) { t(false); } else { t(s !== null); } })); })); } }; var o = n; exports.KeyvMemcache = n; exports.default = o;