Reputation: 2850
I want to create a large in-memory cache for my REST API. How can I make it so that when the cache gets too large, older objects are purged?
I am using nodejs for this project.
Edit: I made a cache class for the time being, is this a scalable and fully optimized approach?
The approach taken here is that there exists in the object a pointer value (_index
) where cache resources will be put. After that the pointer is incremented. Once the pointer reaches the limit
value it is set back to zero and the process continues, except this time values at the pointer are being overridden.
class Cache {
constructor(limit = 2048) {
if (typeof limit !== 'number' || limit <= 0) { limit = Infinity; }
this.limit = limit;
this.purge();
}
purge() {
this._index = 0;
this._values = [];
this._keys = [];
}
put(key, value) {
if ((let existingIndex = this._indexOf(key)) !== undefined) {
this._keys[existingIndex] = key;
this._values[existingIndex] = value;
} else {
this._keys[this._index] = key;
this._values[this._index] = value;
this._nextIndex();
}
}
get(key) {
let index = this._indexOf(key);
if (index === undefined) { return; }
return this._values[index];
}
delete(key) {
let index = this._indexOf(key);
if (index === undefined) { return false; }
this._keys[index] = null;
this._values[index] = null;
return true;
}
has(key) {
return this._indexOf(key) !== undefined;
}
_indexOf(key) {
let i = this.limit;
while (i--) {
if (this._keys[i] === key) {
return i;
}
}
}
_nextIndex() {
this._index += 1;
if (this._index > this.limit) { this._index = 0; }
}
}
export default Cache;
Upvotes: 2
Views: 3822
Reputation: 1750
You're looking for what's called a Least Recently Used (LRU) Cache. It will get rid of the oldest accessed data once the size of the cache reaches a specified threshold. This one is pretty popular: https://www.npmjs.com/package/lru-cache
Upvotes: 2