lakutata
Version:
An IoC-based universal application framework.
128 lines (121 loc) • 3.18 kB
JavaScript
import e from "events";
import t from "memjs";
import { d as s } from "./Package.65.mjs";
import "buffer";
var r = class extends e {
ttlSupport=true;
namespace;
client;
opts;
constructor(e, s) {
super();
s = {
...typeof e === "string" ? {
uri: e
} : e,
...s
};
if (s.uri && s.url === void 0) {
s.url = s.uri;
}
if (e === void 0) {
e = "localhost:11211";
s.url = s.uri = e;
}
this.opts = s;
this.client = t.Client.create(e, s);
}
_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(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 i = r;
export { r as KeyvMemcache, i as default };