@leyyo/cache
Version:
Common cache library
254 lines (253 loc) • 9.59 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacheAliasAbstract = void 0;
const util_1 = require("../util");
const config_1 = require("../config");
// noinspection DuplicatedCode,JSUnusedGlobalSymbols
class CacheAliasAbstract {
// endregion properties
// region constructor
constructor(channel) {
this.channel = channel;
util_1.cacheUtil.bindAll(this);
}
// endregion constructor
// region regular
delete(alias) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prop.enabled) {
return config_1.CACHE_DISABLED;
}
const aliasRec = this.format.alias(alias);
if (!aliasRec.full) {
return config_1.CACHE_EMPTY_KEY;
}
const owner = yield this.getOwner(alias);
if (owner) {
const ownerRec = this.format.owner(owner);
yield this.setSecure.$remove(ownerRec.full, [aliasRec.short]);
}
return (yield this.basicSecure.$delete(aliasRec.full)) ? 1 : 0;
});
}
deleteMore(aliases) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prop.enabled) {
return config_1.CACHE_DISABLED;
}
const aliasRec = this.format.aliases(aliases);
if (aliasRec.fulls.length < 1) {
return config_1.CACHE_EMPTY_KEY;
}
for (const alias of aliases) {
const aliasRec2 = this.format.alias(alias);
if (aliasRec2.short) {
const owner = yield this.getOwner(alias);
if (owner) {
const ownerRec = this.format.owner(owner);
yield this.setSecure.$remove(ownerRec.full, [aliasRec2.short]);
}
}
}
return this.basicSecure.$deleteMore(aliasRec.fulls);
});
}
exists(alias) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prop.enabled) {
return config_1.CACHE_DISABLED;
}
const aliasRec = this.format.alias(alias);
if (!aliasRec.full) {
return config_1.CACHE_EMPTY_KEY;
}
return (yield this.basicSecure.$exists(aliasRec.full)) ? 1 : 0;
});
}
existMore(aliases) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prop.enabled) {
return [];
}
const { fulls } = this.format.aliases(aliases);
if (fulls.length < 1) {
return [];
}
return (yield Promise.all(fulls.map(f => this.basicSecure.$exists(f)))).map(i => i ? 1 : 0);
});
}
getDoc(alias) {
return __awaiter(this, void 0, void 0, function* () {
const owner = yield this.getOwner(alias);
if (!owner) {
return null;
}
return (yield this.basic.getDoc(owner)).result;
});
}
getOwner(alias) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prop.enabled) {
return null;
}
const aliasRec = this.format.alias(alias);
if (!aliasRec.full) {
return null;
}
return this.basicSecure.$get(aliasRec.full);
});
}
getRaw(alias) {
return __awaiter(this, void 0, void 0, function* () {
const owner = yield this.getOwner(alias);
if (!owner) {
return null;
}
return (yield this.basic.getRaw(owner)).result;
});
}
hasAlias(owner) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prop.enabled) {
return config_1.CACHE_DISABLED;
}
const ownerRec = this.format.owner(owner);
if (!ownerRec.full) {
return config_1.CACHE_EMPTY_KEY;
}
return (yield this.basicSecure.$exists(ownerRec.full)) ? 1 : 0;
});
}
listAliases(owner) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prop.enabled) {
return [];
}
const ownerRec = this.format.owner(owner);
if (!ownerRec.full) {
return [];
}
return util_1.cacheUtil.asArray(yield this.setSecure.$list(ownerRec.full));
});
}
listDocs(aliases) {
return __awaiter(this, void 0, void 0, function* () {
const owners = yield this.listOwners(aliases);
if ((owners === null || owners === void 0 ? void 0 : owners.length) < 1) {
return [];
}
return (yield this.basic.listDocs(owners)).result;
});
}
listOwners(aliases) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prop.enabled) {
return [];
}
const aliasRec = this.format.aliases(aliases);
if (aliasRec.fulls.length < 1) {
return [];
}
return util_1.cacheUtil.asArray(yield this.basicSecure.$getMore(aliasRec.fulls));
});
}
listRaws(aliases) {
return __awaiter(this, void 0, void 0, function* () {
const owners = yield this.listOwners(aliases);
if ((owners === null || owners === void 0 ? void 0 : owners.length) < 1) {
return [];
}
return (yield this.basic.listRaws(owners)).result;
});
}
setOwner(alias, owner, opt) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prop.enabled) {
return config_1.CACHE_DISABLED;
}
const aliasRec = this.format.alias(alias);
if (!aliasRec.full) {
return config_1.CACHE_EMPTY_KEY;
}
owner = this.format.basic(owner);
const ownerRec = this.format.owner(owner);
if (!ownerRec.full) {
return config_1.CACHE_EMPTY_VALUE;
}
this.setSecure.$add(ownerRec.full, [aliasRec.short])
.then(() => {
if ((opt === null || opt === void 0 ? void 0 : opt.expiry) || (opt === null || opt === void 0 ? void 0 : opt.span)) {
const milliseconds = this.check.$timestamp(opt.expiry);
if (milliseconds > 0) {
switch (this.check.$saveSpan(opt.span)) {
case "persistent":
this.basicSecure.$persist(ownerRec.full).then();
break;
case "timestamp":
this.basicSecure.$setTimestamp(ownerRec.full, milliseconds).then();
break;
case "ttl":
this.basicSecure.$setTtl(ownerRec.full, milliseconds).then();
break;
}
}
}
});
return ((yield this.basicSecure.$set(aliasRec.full, owner)) === 'OK') ? 1 : 0;
});
}
unlink(alias) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prop.enabled) {
return config_1.CACHE_DISABLED;
}
const aliasRec = this.format.alias(alias);
if (!aliasRec.full) {
return config_1.CACHE_EMPTY_KEY;
}
return (yield this.basicSecure.$unlink(aliasRec.full)) ? 1 : 0;
});
}
unlinkMore(aliases) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prop.enabled) {
return config_1.CACHE_DISABLED;
}
const aliasRec = this.format.aliases(aliases);
if (aliasRec.fulls.length < 1) {
return config_1.CACHE_EMPTY_KEY;
}
return this.basicSecure.$unlinkMore(aliasRec.fulls);
});
}
// endregion regular
// region secure
get $flat() {
return this;
}
get $secure() {
return this;
}
get $back() {
return this;
}
$init() {
this.format = this.channel.format;
this.invalidator = this.channel.invalidator;
this.prop = this.channel.prop;
this.check = this.channel.prop.$secure;
this.basic = this.channel.basic;
this.basicSecure = this.channel.basic.$secure;
this.setSecure = this.channel.set.$secure;
}
}
exports.CacheAliasAbstract = CacheAliasAbstract;