1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- class LRUMap {
- constructor( _maxSize) {this._maxSize = _maxSize;
- this._cache = new Map();
- }
-
- get size() {
- return this._cache.size;
- }
-
- get(key) {
- const value = this._cache.get(key);
- if (value === undefined) {
- return undefined;
- }
-
- this._cache.delete(key);
- this._cache.set(key, value);
- return value;
- }
-
- set(key, value) {
- if (this._cache.size >= this._maxSize) {
-
- this._cache.delete(this._cache.keys().next().value);
- }
- this._cache.set(key, value);
- }
-
- remove(key) {
- const value = this._cache.get(key);
- if (value) {
- this._cache.delete(key);
- }
- return value;
- }
-
- clear() {
- this._cache.clear();
- }
-
- keys() {
- return Array.from(this._cache.keys());
- }
-
- values() {
- const values = [];
- this._cache.forEach(value => values.push(value));
- return values;
- }
- }
- export { LRUMap };
|