@mojojs/core
Version:
Real-time web framework
45 lines • 959 B
JavaScript
/**
* Cache class.
*/
export class Cache {
constructor() {
/**
* Maximum number of values in cache, defaults to `100`.
*/
this.max = 100;
this._cache = {};
this._queue = [];
}
/**
* Get cached value.
*/
get(key) {
return this._cache[key];
}
/**
* Add value to the cache.
*/
set(key, value) {
const { max } = this;
if (max <= 0)
return;
const cache = this._cache;
const queue = this._queue;
while (queue.length >= max) {
const key = queue.shift();
if (key !== undefined)
delete cache[key];
}
if (cache[key] === undefined) {
queue.push(key);
cache[key] = value;
}
}
/**
* Current size of the cache.
*/
get size() {
return this._queue.length;
}
}
//# sourceMappingURL=cache.js.map