UNPKG

1.32 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Jupyter Development Team.
3// Distributed under the terms of the Modified BSD License.
4Object.defineProperty(exports, "__esModule", { value: true });
5exports.LruCache = void 0;
6const DEFAULT_MAX_SIZE = 128;
7/** A least-recently-used cache. */
8class LruCache {
9 constructor(options = {}) {
10 this._map = new Map();
11 this._maxSize = (options === null || options === void 0 ? void 0 : options.maxSize) || DEFAULT_MAX_SIZE;
12 }
13 /**
14 * Return the current size of the cache.
15 */
16 get size() {
17 return this._map.size;
18 }
19 /**
20 * Clear the values in the cache.
21 */
22 clear() {
23 this._map.clear();
24 }
25 /**
26 * Get a value (or null) from the cache, pushing the item to the front of the cache.
27 */
28 get(key) {
29 const item = this._map.get(key) || null;
30 if (item != null) {
31 this._map.delete(key);
32 this._map.set(key, item);
33 }
34 return item;
35 }
36 /**
37 * Set a value in the cache, potentially evicting an old item.
38 */
39 set(key, value) {
40 if (this._map.size >= this._maxSize) {
41 this._map.delete(this._map.keys().next().value);
42 }
43 this._map.set(key, value);
44 }
45}
46exports.LruCache = LruCache;
47//# sourceMappingURL=lru.js.map
\No newline at end of file