src/utils/MemoryCache.js
const hasOwnProperty = Object.prototype.hasOwnProperty;
const cache = {};
export default class MemoryCache {
constructor(name) {
this.name = name;
this.fetch.bind(this);
this.clear.bind(this);
cache[name] = cache[name] || {};
}
fetch(key, miss) {
if (hasOwnProperty.call(cache[this.name], key)) {
return cache[this.name][key];
}
cache[this.name][key] = miss(Object.keys(cache[this.name]));
return cache[this.name][key];
}
clear() {
cache[this.name] = {};
}
}