@web-atoms/core
Version:
84 lines (83 loc) • 2.5 kB
JavaScript
System.register(["tslib", "../App", "../di/DISingleton", "../di/Inject"], function (_export, _context) {
"use strict";
var __awaiter, __decorate, __metadata, __param, App, DISingleton, Inject, CacheService;
return {
setters: [function (_tslib) {
__awaiter = _tslib.__awaiter;
__decorate = _tslib.__decorate;
__metadata = _tslib.__metadata;
__param = _tslib.__param;
}, function (_App) {
App = _App.App;
}, function (_diDISingleton) {
DISingleton = _diDISingleton.default;
}, function (_diInject) {
Inject = _diInject.Inject;
}],
execute: function () {
CacheService = class CacheService {
constructor(app) {
this.app = app;
this.cache = {};
}
remove(key) {
const v = this.cache[key];
if (v) {
this.clear(v);
return v.value;
}
return null;
}
getOrCreate(key, task) {
return __awaiter(this, void 0, void 0, function* () {
const c = this.cache[key] || (this.cache[key] = {
key,
finalTTL: 3600
});
if (!c.value) {
c.value = task(c);
}
let v = null;
try {
v = yield c.value;
} catch (e) {
this.clear(c);
throw e;
}
if (c.ttlSeconds !== undefined) {
if (typeof c.ttlSeconds === "number") {
c.finalTTL = c.ttlSeconds;
} else {
c.finalTTL = c.ttlSeconds(v);
}
}
if (c.timeout) {
clearTimeout(c.timeout);
}
if (c.finalTTL) {
this.cache[key] = c;
c.timeout = setTimeout(() => {
c.timeout = 0;
this.clear(c);
}, c.finalTTL * 1000);
} else {
this.clear(c);
}
return yield c.value;
});
}
clear(ci) {
if (ci.timeout) {
clearTimeout(ci.timeout);
ci.timeout = 0;
}
this.cache[ci.key] = null;
delete this.cache[ci.key];
}
};
CacheService = __decorate([DISingleton(), __param(0, Inject), __metadata("design:paramtypes", [App])], CacheService);
_export("default", CacheService);
}
};
});
//# sourceMappingURL=CacheService.js.map