index.js (738B)
1 class Cache { 2 constructor(maxSize) { 3 this._maxSize = maxSize; 4 this._store = {}; 5 this._size = 0; 6 } 7 8 get(key) { 9 if (key in this._store) { 10 const value = this._store[key]; 11 delete this._store[key]; 12 this._store[key] = value; 13 return {value, found: true}; 14 } 15 return {found: false}; 16 } 17 18 put(key, value) { 19 if (key in this._store) { 20 delete this._store[key]; 21 } else { 22 this._size += 1; 23 } 24 this._store[key] = value; 25 if (this._size > this._maxSize) { 26 delete this._store[Object.keys(this._store)[0]]; 27 this._size -= 1; 28 } 29 } 30 } 31 32 export {Cache};