molstar
Version:
A comprehensive macromolecular library.
88 lines (87 loc) • 3.34 kB
JavaScript
"use strict";
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Cache = void 0;
var console_logger_1 = require("../../../mol-util/console-logger");
var generic_1 = require("../../../mol-data/generic");
var config_1 = require("../config");
var Cache = /** @class */ (function () {
function Cache(keyGetter, sizeGetter) {
this.keyGetter = keyGetter;
this.sizeGetter = sizeGetter;
this.entries = (0, generic_1.LinkedList)();
this.entryMap = new Map();
this.approximateSize = 0;
}
Cache.prototype.clearTimeout = function (e) {
if (typeof e.value.timeoutId !== 'undefined') {
clearTimeout(e.value.timeoutId);
e.value.timeoutId = void 0;
}
};
Cache.prototype.dispose = function (e) {
this.clearTimeout(e);
if (e.inList) {
this.entries.remove(e);
this.approximateSize -= e.value.approximateSize;
}
this.entryMap.delete(e.value.key);
};
Cache.prototype.refresh = function (e) {
var _this = this;
this.clearTimeout(e);
e.value.timeoutId = setTimeout(function () { return _this.expireNode(e); }, config_1.ModelServerConfig.cacheEntryTimeoutMs);
this.entries.remove(e);
this.entries.addFirst(e.value);
};
Cache.prototype.expireNode = function (e, notify) {
if (notify === void 0) { notify = true; }
if (notify)
console_logger_1.ConsoleLogger.log('Cache', "".concat(e.value.key, " expired."));
this.dispose(e);
};
Cache.prototype.expireAll = function () {
for (var e = this.entries.first; e; e = e.next)
this.expireNode(e, false);
};
Cache.prototype.expire = function (key) {
var entry = this.entryMap.get(key);
if (!entry)
return;
this.expireNode(entry);
};
Cache.prototype.add = function (item) {
var key = this.keyGetter(item);
var approximateSize = this.sizeGetter(item);
if (this.entryMap.has(key))
this.dispose(this.entryMap.get(key));
if (config_1.ModelServerConfig.cacheMaxSizeInBytes < this.approximateSize + approximateSize) {
if (this.entries.last)
this.dispose(this.entries.last);
}
this.approximateSize += approximateSize;
var entry = { key: key, approximateSize: approximateSize, timeoutId: void 0, item: item };
var e = this.entries.addFirst(entry);
this.entryMap.set(key, e);
this.refresh(e);
console_logger_1.ConsoleLogger.log('Cache', "".concat(key, " added."));
return item;
};
Cache.prototype.has = function (key) {
return this.entryMap.has(key);
};
Cache.prototype.get = function (key) {
if (!this.entryMap.has(key))
return void 0;
var e = this.entryMap.get(key);
this.refresh(e);
console_logger_1.ConsoleLogger.log('Cache', "".concat(key, " accessed."));
return e.value.item;
};
return Cache;
}());
exports.Cache = Cache;