Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
31 lines
639 B
Plaintext
31 lines
639 B
Plaintext
class LRUCache {
|
|
constructor(maxSize) {
|
|
this.maxSize = maxSize;
|
|
this.cache = new Map();
|
|
}
|
|
set(key, value) {
|
|
const isNewKey = !this.cache.has(key);
|
|
if (isNewKey && this.cache.size >= this.maxSize) {
|
|
const lruKey = this.cache.keys().next().value;
|
|
if (lruKey !== undefined) {
|
|
this.cache.delete(lruKey);
|
|
}
|
|
}
|
|
this.cache.set(key, {
|
|
key,
|
|
value
|
|
});
|
|
}
|
|
get(key) {
|
|
const item = this.cache.get(key);
|
|
if (item) {
|
|
this.cache.delete(key);
|
|
this.cache.set(key, item);
|
|
return item.value;
|
|
}
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
export { LRUCache as default };
|