lakutata
Version:
An IoC-based universal application framework.
724 lines (600 loc) • 19.1 kB
JavaScript
/* Build Date: Mon Jan 05 2026 23:52:23 GMT+0800 (China Standard Time) */
import { _ as e, a as t } from "../../../vendor/Package.internal.3.mjs";
import r from "events";
import "buffer";
import { K as o } from "../../../vendor/Package.internal.524.mjs";
import { Component as i } from "../../lib/core/Component.mjs";
import { Configurable as s } from "../../decorators/di/Configurable.mjs";
import { As as a } from "../../lib/helpers/As.mjs";
import { CreateFileCacheAdapter as n } from "./adapters/CreateFileCacheAdapter.mjs";
import { CreateRedisCacheAdapter as c } from "./adapters/CreateRedisCacheAdapter.mjs";
import { CreateMemcacheCacheAdapter as m } from "./adapters/CreateMemcacheCacheAdapter.mjs";
import { CreateMongoCacheAdapter as l } from "./adapters/CreateMongoCacheAdapter.mjs";
import { CreateSqliteCacheAdapter as p } from "./adapters/CreateSqliteCacheAdapter.mjs";
import { CreatePostgresCacheAdapter as h } from "./adapters/CreatePostgresCacheAdapter.mjs";
import { CreateMysqlCacheAdapter as d } from "./adapters/CreateMysqlCacheAdapter.mjs";
import { D as u } from "../../../vendor/Package.internal.7.mjs";
import { FileCacheOptions as y } from "./options/FileCacheOptions.mjs";
import { RedisCacheOptions as f } from "./options/RedisCacheOptions.mjs";
import { MemcacheCacheOptions as j } from "./options/MemcacheCacheOptions.mjs";
import { MongoCacheOptions as b } from "./options/MongoCacheOptions.mjs";
import { SqliteCacheOptions as v } from "./options/SqliteCacheOptions.mjs";
import { PostgresCacheOptions as g } from "./options/PostgresCacheOptions.mjs";
import { MysqlCacheOptions as k } from "./options/MysqlCacheOptions.mjs";
import "../../../vendor/Package.internal.4.mjs";
import "../../lib/base/async-constructor/AsyncConstructor.mjs";
import "../../lib/base/async-constructor/Append.mjs";
import "../../lib/helpers/IsPromiseLike.mjs";
import "../../lib/helpers/IsPromise.mjs";
import "util/types";
import "../../decorators/di/Lifetime.mjs";
import "../../lib/base/internal/ObjectLifetime.mjs";
import "../../constants/DIMetadataKey.mjs";
import "../../../vendor/Package.internal.1.mjs";
import "../../../vendor/Package.internal.2.mjs";
import "../../exceptions/di/LifetimeLockedException.mjs";
import "../../lib/base/abstracts/Exception.mjs";
import "../../../vendor/Package.internal.5.mjs";
import "../../../vendor/Package.internal.6.mjs";
import "../../lib/base/internal/ThrowWarning.mjs";
import "../../lib/helpers/Templating.mjs";
import "../../lib/base/internal/CamelCase.mjs";
import "../../lib/helpers/NoCase.mjs";
import "../../lib/helpers/DevNull.mjs";
import "../../exceptions/MethodNotFoundException.mjs";
import "../../lib/ioc/DependencyInjectionContainer.mjs";
import "util";
import "../../lib/ioc/Errors.mjs";
import "../../exceptions/di/DependencyInjectionException.mjs";
import "../../lib/base/internal/ConstructorSymbol.mjs";
import "node:crypto";
import "../../lib/base/internal/DataValidator.mjs";
import "node:util/types";
import "../../lib/validation/VLD.mjs";
import "url";
import "../../exceptions/dto/InvalidValueException.mjs";
import "../../../vendor/Package.internal.8.mjs";
import "../../lib/helpers/IsHtml.mjs";
import "../../lib/helpers/IsXML.mjs";
import "../../constants/DTOMetadataKey.mjs";
import "../../lib/helpers/ObjectConstructor.mjs";
import "../../lib/helpers/ObjectParentConstructors.mjs";
import "../../lib/helpers/ObjectParentConstructor.mjs";
import "../../lib/helpers/ObjectPrototype.mjs";
import "../../lib/ioc/InjectionMode.mjs";
import "../../lib/ioc/Lifetime.mjs";
import "../../lib/ioc/ListModules.mjs";
import "os";
import "path";
import "stream";
import "fs";
import "../../lib/ioc/Utils.mjs";
import "../../lib/ioc/FunctionTokenizer.mjs";
import "../../lib/ioc/LoadModules.mjs";
import "../../lib/ioc/Resolvers.mjs";
import "../../lib/ioc/ParamParser.mjs";
import "../../lib/base/internal/ObjectWeakRefs.mjs";
import "../../lib/base/internal/ConfigurableRecordsInjection.mjs";
import "../../lib/base/internal/ObjectConfiguration.mjs";
import "../../lib/base/internal/ObjectInjection.mjs";
import "../../lib/base/internal/ObjectContainer.mjs";
import "../../decorators/dto/Expect.mjs";
import "../../decorators/dto/IndexSignature.mjs";
import "../../decorators/dto/Accept.mjs";
import "../../lib/base/internal/MethodValidation.mjs";
import "../../exceptions/dto/InvalidMethodAcceptException.mjs";
import "../../exceptions/dto/InvalidMethodReturnException.mjs";
import "../../lib/helpers/IsEmptyObject.mjs";
import "../../lib/base/EventEmitter.mjs";
import "../../lib/core/Provider.mjs";
import "../../../vendor/Package.internal.310.mjs";
import "fs/promises";
import "./lib/IsDriverPackageInstalled.mjs";
import "./exceptions/CacheDriverNotFoundException.mjs";
import "../../lib/helpers/URLBuilder.mjs";
var C = new Map;
function P(e) {
return C.has(e);
}
function w(e) {
C.set(e, []);
}
function S(e) {
C.delete(e);
}
function x(e, t) {
const r = I(e);
r.push(t);
C.set(e, r);
}
function I(e) {
return C.get(e) ?? [];
}
async function A(e) {
return new Promise((t, r) => {
const o = {
resolve: t,
reject: r
};
x(e, o);
});
}
function M(e) {
const t = I(e);
S(e);
return t;
}
function O(e) {
const {key: t, error: r, result: o} = e;
for (const e of M(t)) {
if (r) {
e.reject(r);
} else {
e.resolve(o);
}
}
}
async function L(e, t) {
if (!P(e)) {
w(e);
try {
const r = await Promise.resolve(t());
O({
key: e,
result: r
});
return r;
} catch (t) {
O({
key: e,
error: t
});
throw t;
}
}
return A(e);
}
function T(e) {
return e !== null && typeof e === "object" && !Array.isArray(e);
}
function D(e, t) {
return typeof e === "number" && typeof t === "number" ? e < t : false;
}
function E(e, ...t) {
return typeof e === "function" ? e(...t) : e;
}
var B = e => e === 0 ? "primary" : `secondary:${e - 1}`;
var N = e => {
const t = new r;
const i = new o;
i.serialize = void 0;
i.deserialize = void 0;
const s = e?.stores?.length ? e.stores : [ i ];
const a = e?.nonBlocking ?? false;
const n = e?.cacheId ?? Math.random().toString(36).slice(2);
const c = async e => {
let r;
if (a) {
try {
r = await Promise.race(s.map(async t => t.get(e)));
if (r === void 0) {
return void 0;
}
} catch (r) {
t.emit("get", {
key: e,
error: r
});
}
} else {
for (let o = 0; o < s.length; o++) {
const i = s[o];
try {
const s = await i.get(e);
if (s !== void 0) {
r = s;
t.emit("get", {
key: e,
value: r,
store: B(o)
});
break;
}
} catch (r) {
t.emit("get", {
key: e,
error: r,
store: B(o)
});
}
}
}
return r;
};
const m = async e => {
let r = e.map(() => void 0);
if (a) {
try {
r = await Promise.race(s.map(async t => t.getMany(e)));
} catch (r) {
t.emit("mget", {
keys: e,
error: r
});
}
} else {
for (const o of s) {
try {
const t = r.map((t, r) => t === void 0 ? {
originalIndex: r,
key: e[r]
} : void 0).filter(e => e !== void 0);
if (t.length === 0) {
break;
}
const i = t.map(e => e.key);
const s = await o.getMany(i);
for (const [e, o] of s.entries()) {
if (o === void 0) {
continue;
}
const {originalIndex: i} = t[e];
r[i] = o;
}
} catch (r) {
t.emit("mget", {
keys: e,
error: r
});
}
}
}
t.emit("mget", {
keys: e,
values: r
});
return r;
};
const l = async e => {
let r;
if (a) {
try {
r = await Promise.race(s.map(async t => t.get(e, {
raw: true
})));
if (r === void 0) {
return void 0;
}
} catch (r) {
t.emit("ttl", {
key: e,
error: r
});
}
} else {
for (const o of s) {
try {
const i = await o.get(e, {
raw: true
});
if (i !== void 0) {
r = i;
t.emit("ttl", {
key: e,
value: r
});
break;
}
} catch (r) {
t.emit("ttl", {
key: e,
error: r
});
}
}
}
if (r?.expires) {
return r.expires;
}
return void 0;
};
const p = async (r, o, i, s) => {
try {
const n = r.map(async (r, a) => {
await r.set(o, i, s ?? e?.ttl);
t.emit("set", {
key: o,
value: i,
store: B(a)
});
});
if (a) {
Promise.all(n);
t.emit("set", {
key: o,
value: i
});
return i;
}
await Promise.all(n);
t.emit("set", {
key: o,
value: i
});
return i;
} catch (e) {
t.emit("set", {
key: o,
value: i,
error: e
});
return Promise.reject(e);
}
};
const h = async (r, o) => {
const i = o.map(({key: t, value: r, ttl: o}) => ({
key: t,
value: r,
ttl: o ?? e?.ttl
}));
try {
const e = r.map(async e => e.setMany(i));
if (a) {
Promise.all(e);
t.emit("mset", {
list: i
});
return i;
}
await Promise.all(e);
t.emit("mset", {
list: i
});
return i;
} catch (e) {
t.emit("mset", {
list: i,
error: e
});
return Promise.reject(e);
}
};
const d = async e => {
try {
if (a) {
Promise.all(s.map(async t => t.delete(e)));
t.emit("del", {
key: e
});
return true;
}
await Promise.all(s.map(async t => t.delete(e)));
t.emit("del", {
key: e
});
return true;
} catch (r) {
t.emit("del", {
key: e,
error: r
});
return Promise.reject(r);
}
};
const u = async e => {
try {
const r = [];
for (const t of e) {
r.push(...s.map(async e => e.delete(t)));
}
if (a) {
Promise.all(r);
t.emit("mdel", {
keys: e
});
return true;
}
await Promise.all(r);
t.emit("mdel", {
keys: e
});
return true;
} catch (r) {
t.emit("mdel", {
keys: e,
error: r
});
return Promise.reject(r);
}
};
const y = async () => {
try {
if (a) {
Promise.all(s.map(async e => e.clear()));
t.emit("clear");
return true;
}
await Promise.all(s.map(async e => e.clear()));
t.emit("clear");
return true;
} catch (e) {
t.emit("clear", e);
return Promise.reject(e);
}
};
const f = async (r, o, i, a) => L(`${n}::${r}`, async () => {
let c;
let m;
let l = 0;
let h;
const {ttl: d, refreshThreshold: u, raw: y} = T(i) ? i : {
ttl: i,
refreshThreshold: a
};
const f = t => E(d, t) ?? e?.ttl;
for (;l < s.length; l++) {
try {
const e = await s[l].get(r, {
raw: true
});
if (e !== void 0) {
c = e.value;
m = e;
if (typeof e.expires === "number") {
h = Math.max(0, e.expires - Date.now());
}
break;
}
} catch {}
}
if (c === void 0) {
const e = await o();
const t = f(e);
await p(s, r, e, t);
return y ? {
value: e,
expires: Date.now() + t
} : e;
}
const j = D(h, E(u, c) ?? e?.refreshThreshold);
if (j) {
L(`+++${n}__${r}`, o).then(async o => {
try {
await p(e?.refreshAllStores ? s : s.slice(0, l + 1), r, o, f(o));
t.emit("refresh", {
key: r,
value: o
});
} catch (e) {
t.emit("refresh", {
key: r,
value: c,
error: e
});
}
}).catch(e => {
t.emit("refresh", {
key: r,
value: c,
error: e
});
});
}
if (!j && l > 0) {
await p(s.slice(0, l), r, c, f(c));
}
return y ? m : c;
});
const j = (e, r) => t.addListener(e, r);
const b = (e, r) => t.removeListener(e, r);
const v = async () => {
try {
await Promise.all(s.map(async e => e.disconnect()));
} catch (e) {
return Promise.reject(e);
}
};
const g = () => n;
return {
get: c,
mget: m,
ttl: l,
set: async (e, t, r) => p(s, e, t, r),
mset: async e => h(s, e),
del: d,
mdel: u,
clear: y,
wrap: f,
on: j,
off: b,
disconnect: v,
cacheId: g,
stores: s
};
};
const R = e => ({
class: Cacher,
stores: e?.stores,
ttl: e?.ttl,
refreshThreshold: e?.refreshThreshold,
refreshAllStores: e?.refreshAllStores,
nonBlocking: e?.nonBlocking,
cacheId: e?.cacheId
});
class Cacher extends i {
async init() {
const e = this.stores ? Array.isArray(this.stores) ? this.stores : [ this.stores ] : [];
const t = await Promise.all(e.map(e => {
switch (e.type) {
case "file":
return n(e);
case "redis":
return c(e);
case "memcache":
return m(e);
case "mongo":
return l(e);
case "sqlite":
return p(e);
case "postgres":
return h(e);
case "mysql":
return d(e);
default:
return undefined;
}
}));
const r = t.filter(e => !!e);
this.cache = N({
stores: r.length ? r : undefined,
ttl: this.ttl,
refreshThreshold: this.refreshThreshold,
refreshAllStores: this.refreshAllStores,
nonBlocking: this.nonBlocking,
cacheId: this.cacheId || this.$uuid
});
this.cache.on("set", e => this.emit("set", e));
this.cache.on("del", e => this.emit("del", e));
this.cache.on("clear", e => this.emit("clear", e));
this.cache.on("refresh", e => this.emit("refresh", e));
}
async destroy() {
await this.cache.disconnect();
}
async set(e, t, r) {
return await this.cache.set(e, t, r);
}
async multipleSet(e) {
return await this.cache.mset(e);
}
async get(e) {
return a(await this.cache.get(e));
}
async multipleGet(e) {
return await this.cache.mget(e);
}
async getTTL(e) {
const t = await this.cache.ttl(e);
if (t === undefined) return -1;
return t;
}
async del(e) {
return await this.cache.del(e);
}
async multipleDel(e) {
return await this.cache.mdel(e);
}
async clear() {
return await this.cache.clear();
}
on(e, t) {
return super.on(e, t);
}
}
e([ s(u.Alternatives(u.Array(u.Alternatives(y.Schema(), f.Schema(), j.Schema(), b.Schema(), v.Schema(), g.Schema(), k.Schema())), y.Schema(), f.Schema(), j.Schema(), b.Schema(), v.Schema(), g.Schema(), k.Schema()).optional()), t("design:type", Object) ], Cacher.prototype, "stores", void 0);
e([ s(u.Number().positive().integer().optional()), t("design:type", Number) ], Cacher.prototype, "ttl", void 0);
e([ s(u.Number().positive().integer().optional()), t("design:type", Number) ], Cacher.prototype, "refreshThreshold", void 0);
e([ s(u.Boolean().optional()), t("design:type", Boolean) ], Cacher.prototype, "refreshAllStores", void 0);
e([ s(u.Boolean().optional()), t("design:type", Boolean) ], Cacher.prototype, "nonBlocking", void 0);
e([ s(u.String().optional()), t("design:type", String) ], Cacher.prototype, "cacheId", void 0);
export { R as BuildCacherOptions, Cacher };