@lakutata-component/cacher
Version:
Lakutata Cacher Component
201 lines • 8.87 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacherComponent = void 0;
const core_1 = require("@lakutata/core");
const cache_manager_1 = __importDefault(require("cache-manager"));
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const cache_manager_ioredis_1 = __importDefault(require("cache-manager-ioredis"));
const cache_manager_fs_hash_1 = __importDefault(require("cache-manager-fs-hash"));
const cache_manager_memcached_store_1 = __importDefault(require("cache-manager-memcached-store"));
class CacherComponent extends core_1.Component {
constructor() {
super(...arguments);
this.ttl = Infinity;
}
async initialize() {
if (!this.options) {
this.cache = cache_manager_1.default.caching({ store: 'memory', ttl: this.ttl });
}
else {
const cacheOptions = this.options;
switch (cacheOptions.type) {
case 'memory':
{
this.ttl = cacheOptions.ttl ? cacheOptions.ttl : this.ttl;
this.cache = cache_manager_1.default.caching({
store: 'memory',
ttl: this.ttl
});
}
break;
case 'fs':
{
this.ttl = cacheOptions.ttl ? cacheOptions.ttl : this.ttl;
this.cache = cache_manager_1.default.caching({
store: cache_manager_fs_hash_1.default,
ttl: this.ttl,
options: {
path: cacheOptions.directory ? cacheOptions.directory : path_1.default.resolve(os_1.default.tmpdir(), './._LCC_CACHE_'),
ttl: this.ttl,
subdirs: cacheOptions.subdirectories === undefined ? false : cacheOptions.subdirectories,
zip: cacheOptions.compression === undefined ? false : cacheOptions.compression
}
});
}
break;
case 'memcached':
{
this.ttl = cacheOptions.ttl ? cacheOptions.ttl : this.ttl;
const Memcache = require('memcache-plus');
this.cache = cache_manager_1.default.caching({
store: cache_manager_memcached_store_1.default,
driver: Memcache,
options: {
hosts: Array.isArray(cacheOptions.hosts) ? cacheOptions.hosts : [cacheOptions.hosts],
autodiscover: cacheOptions.autodiscover === undefined ? false : cacheOptions.autodiscover,
backoffLimit: cacheOptions.backoffLimit === undefined ? 10000 : cacheOptions.backoffLimit,
bufferBeforeError: cacheOptions.bufferBeforeError === undefined ? 1000 : cacheOptions.bufferBeforeError,
disabled: cacheOptions.disabled === undefined ? false : cacheOptions.disabled,
maxValueSize: cacheOptions.maxValueSize === undefined ? 1048576 : cacheOptions.maxValueSize,
queue: cacheOptions.queue === undefined ? true : cacheOptions.queue,
netTimeout: cacheOptions.netTimeout === undefined ? 500 : cacheOptions.netTimeout,
reconnect: cacheOptions.reconnect === undefined ? true : cacheOptions.reconnect,
onNetError: cacheOptions.onNetError === undefined ? (err) => {
} : cacheOptions.onNetError
},
ttl: this.ttl
});
}
break;
case 'redis':
{
this.ttl = cacheOptions.ttl ? cacheOptions.ttl : this.ttl;
this.cache = cache_manager_1.default.caching({
store: cache_manager_ioredis_1.default,
host: cacheOptions.host,
port: cacheOptions.port,
password: cacheOptions.password,
db: cacheOptions.db,
ttl: this.ttl
});
}
break;
default: {
this.cache = cache_manager_1.default.caching({
store: 'memory',
ttl: this.ttl
});
}
}
}
}
generateCacheKey(key) {
if (this.prefix) {
return `${this.app.getID()}_${this.prefix.toString()}_${key}`;
}
else {
return `${this.app.getID()}_${key}`;
}
}
async set(key, value) {
return new Promise(resolve => {
this.cache.set(this.generateCacheKey(key), value, { ttl: this.ttl }, error => {
if (error) {
resolve(false);
}
else {
resolve(true);
}
});
});
}
async get(key) {
return new Promise(resolve => {
this.cache.get(this.generateCacheKey(key), (error, result) => {
if (error) {
resolve(null);
}
else {
if (result === undefined) {
resolve(null);
}
else {
resolve(result);
}
}
});
});
}
async getOrSet(key, value) {
let cachedValue = await this.get(key);
if (cachedValue === null) {
await this.set(key, value);
cachedValue = value;
}
return cachedValue;
}
async del(key) {
return new Promise(resolve => {
this.cache.del(this.generateCacheKey(key), error => {
if (error) {
resolve(false);
}
else {
resolve(true);
}
});
});
}
async reset() {
return new Promise(resolve => {
this.cache.reset(() => {
resolve();
});
});
}
}
__decorate([
(0, core_1.Inject)(core_1.Application),
__metadata("design:type", core_1.Application)
], CacherComponent.prototype, "app", void 0);
__decorate([
(0, core_1.Configurable)(),
__metadata("design:type", String)
], CacherComponent.prototype, "prefix", void 0);
__decorate([
(0, core_1.Configurable)(),
__metadata("design:type", Object)
], CacherComponent.prototype, "options", void 0);
__decorate([
(0, core_1.Accept)([core_1.Validator.String, core_1.Validator.Any], { strict: true }),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String, Object]),
__metadata("design:returntype", Promise)
], CacherComponent.prototype, "set", null);
__decorate([
(0, core_1.Accept)(core_1.Validator.String, { strict: true }),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String]),
__metadata("design:returntype", Promise)
], CacherComponent.prototype, "get", null);
__decorate([
(0, core_1.Accept)(core_1.Validator.String, { strict: true }),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String]),
__metadata("design:returntype", Promise)
], CacherComponent.prototype, "del", null);
exports.CacherComponent = CacherComponent;
//# sourceMappingURL=CacherComponent.js.map