@leyyo/cache
Version:
Common cache library
134 lines (133 loc) • 5.47 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.CacheSetAbstract = void 0;
const util_1 = require("../util");
const config_1 = require("../config");
// noinspection DuplicatedCode,JSUnusedGlobalSymbols
class CacheSetAbstract {
// endregion properties
// region constructor
constructor(channel) {
this.channel = channel;
util_1.cacheUtil.bindAll(this);
}
// endregion constructor
// region region
add(key, members) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prop.enabled) {
return this.invalidator.disabledNumber(config_1.CACHE_DISABLED);
}
const { full } = this.format.key(key);
if (!full) {
return this.invalidator.ignoreNumber(config_1.CACHE_EMPTY_KEY);
}
const { shorts } = this.format.memberShorts(members);
if (shorts.length < 1) {
return this.invalidator.ignoreNumber(config_1.CACHE_EMPTY_VALUE, 'Empty members');
}
return this.invalidator.success(yield this.$add(full, shorts), [full]);
});
}
existMore(key, members) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prop.enabled) {
return this.invalidator.disabledArray();
}
const { full } = this.format.key(key);
if (!full) {
return this.invalidator.ignoreArray('Empty key');
}
const checkedMembers = this.format.members(members);
if (checkedMembers.length < 1) {
return this.invalidator.ignoreArray('Empty members');
}
const promises = checkedMembers.map(m => this.$exist(full, m));
const results = yield Promise.all(promises);
return this.invalidator.success(results.map(i => i ? 1 : 0), [full]);
});
}
exists(key, member) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prop.enabled) {
return this.invalidator.disabledNumber(config_1.CACHE_DISABLED);
}
const { full } = this.format.key(key);
if (!full) {
return this.invalidator.ignoreNumber(config_1.CACHE_EMPTY_KEY, 'Empty key');
}
const checkedMember = this.format.member(member);
if (!checkedMember) {
return this.invalidator.ignoreNumber(config_1.CACHE_EMPTY_VALUE, 'Empty member');
}
return this.invalidator.success((yield this.$exist(full, checkedMember)) ? 1 : 0, [full]);
});
}
getLength(key) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prop.enabled) {
return this.invalidator.disabledNumber(config_1.CACHE_DISABLED);
}
const { full } = this.format.key(key);
if (!full) {
return this.invalidator.ignoreNumber(config_1.CACHE_EMPTY_KEY, 'Empty key');
}
return this.invalidator.success(yield this.$length(full), [full]);
});
}
listMembers(key) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prop.enabled) {
return this.invalidator.disabledArray();
}
const { full } = this.format.key(key);
if (!full) {
return this.invalidator.ignoreArray('Empty key');
}
return this.invalidator.success(yield this.$list(full), [full]);
});
}
remove(key, members) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.prop.enabled) {
return this.invalidator.disabledNumber(config_1.CACHE_DISABLED);
}
const { full } = this.format.key(key);
if (!full) {
return this.invalidator.ignoreNumber(config_1.CACHE_EMPTY_VALUE);
}
const { shorts } = this.format.memberShorts(members);
if (shorts.length < 1) {
return this.invalidator.ignoreNumber(config_1.CACHE_EMPTY_VALUE, 'Empty members');
}
return this.invalidator.success(yield this.$remove(full, shorts), [full]);
});
}
// endregion region
// 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;
}
}
exports.CacheSetAbstract = CacheSetAbstract;