Hard
Your team is building a caching layer for database queries. What will be the final cache state after these operations?
1class QueryCache {2 #cache = new Map();3 #maxSize;4 #accessLog = new Map();56 constructor(maxSize = 3) {7 this.#maxSize = maxSize;8 }910 set(key, value) {11 if (this.#cache.size >= this.#maxSize && !this.#cache.has(key)) {12 const leastUsed = this.#findLeastUsed();13 this.#cache.delete(leastUsed);14 this.#accessLog.delete(leastUsed);15 }1617 this.#cache.set(key, value);18 this.#updateAccess(key);19 }2021 get(key) {22 if (this.#cache.has(key)) {23 this.#updateAccess(key);24 return this.#cache.get(key);25 }26 return null;27 }2829 #updateAccess(key) {30 this.#accessLog.set(key, Date.now());31 }3233 #findLeastUsed() {34 let leastUsedKey = null;35 let leastUsedTime = Infinity;3637 for (const [key, time] of this.#accessLog) {38 if (time < leastUsedTime) {39 leastUsedTime = time;40 leastUsedKey = key;41 }42 }4344 return leastUsedKey;45 }4647 getState() {48 return Array.from(this.#cache.entries());49 }50}5152const cache = new QueryCache(3);53cache.set('query1', 'result1');54cache.set('query2', 'result2');55cache.set('query3', 'result3');56cache.get('query1');57cache.set('query4', 'result4');5859console.log(cache.getState());