Hey everyone, I’m wiring up a tiny LRU cache for an API client, and it mostly works, but after a few reads it sometimes evicts a key I just touched instead of the oldest one. I was trying to keep it simple with Map insertion order instead of a linked list, but now I’m not sure if my update path is messing up recency.
class LRU {
constructor(limit) {
this.limit = limit;
this.map = new Map();
}
get(key) {
if (!this.map.has(key)) return -1;
const value = this.map.get(key);
this.map.set(key, value);
return value;
}
put(key, value) {
if (this.map.has(key)) {
this.map.delete(key);
}
this.map.set(key, value);
if (this.map.size > this.limit) {
const oldest = this.map.keys().next().value;
this.map.delete(oldest);
}
}
}
What am I missing about Map order here, and is there a clean fix without switching to a full linked-list implementation?
MechaPrime