@jupyterlab/coreutils
Version:
JupyterLab - Core Utilities
47 lines • 1.32 kB
JavaScript
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.LruCache = void 0;
const DEFAULT_MAX_SIZE = 128;
/** A least-recently-used cache. */
class LruCache {
constructor(options = {}) {
this._map = new Map();
this._maxSize = (options === null || options === void 0 ? void 0 : options.maxSize) || DEFAULT_MAX_SIZE;
}
/**
* Return the current size of the cache.
*/
get size() {
return this._map.size;
}
/**
* Clear the values in the cache.
*/
clear() {
this._map.clear();
}
/**
* Get a value (or null) from the cache, pushing the item to the front of the cache.
*/
get(key) {
const item = this._map.get(key) || null;
if (item != null) {
this._map.delete(key);
this._map.set(key, item);
}
return item;
}
/**
* Set a value in the cache, potentially evicting an old item.
*/
set(key, value) {
if (this._map.size >= this._maxSize) {
this._map.delete(this._map.keys().next().value);
}
this._map.set(key, value);
}
}
exports.LruCache = LruCache;
//# sourceMappingURL=lru.js.map
;