molstar
Version:
A comprehensive macromolecular library.
85 lines • 3.17 kB
JavaScript
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { ConsoleLogger } from '../../../mol-util/console-logger';
import { LinkedList } from '../../../mol-data/generic';
import { ModelServerConfig as ServerConfig } from '../config';
var Cache = /** @class */ (function () {
function Cache(keyGetter, sizeGetter) {
this.keyGetter = keyGetter;
this.sizeGetter = sizeGetter;
this.entries = 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); }, ServerConfig.cacheEntryTimeoutMs);
this.entries.remove(e);
this.entries.addFirst(e.value);
};
Cache.prototype.expireNode = function (e, notify) {
if (notify === void 0) { notify = true; }
if (notify)
ConsoleLogger.log('Cache', 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 (ServerConfig.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);
ConsoleLogger.log('Cache', 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);
ConsoleLogger.log('Cache', key + " accessed.");
return e.value.item;
};
return Cache;
}());
export { Cache };
//# sourceMappingURL=cache.js.map