cache: replace with a middleware-type solution
So, in hindsight, while I needed the functionality of the cache system that was implemented, implementing it in the way that I did was pretty dumb. So, this is a note to remind me that I need to un-do it, and allow a method for taking care of it in whatever application uses mws-advanced, instead of doing it inside mws-advanced.
I definitely think that if you're going to be doing a large number of requests, and there's any potential that you may repeat some of them over a short period of time, that it's a good idea to implement some kind of a cache. I'd very much like to add a middleware type of thing to mws-advanced, which also gives me some good ideas for restructuring some parts of the library.
Unfortunately, I don't see myself getting to implementing it in the very near future, so if anyone else wants to, cool. Here's what I ended up doing in my application that runs as a server, and makes a lot of calls to mws:
function getCachedResult(cacheName, func, ...opts) {
return new Promise(async (resolve, reject) => {
const cacheKey = `${cacheName}${JSON.stringify(opts)}`;
if (lookupCache[cacheKey]) {
if (Date.now() - lookupCache[cacheKey].cachedAt < CACHE_EXPIRE_TIME) {
return resolve(lookupCache[cacheKey].data);
}
setTimeout(expireCache, 5000);
}
try {
const data = await func.call(this, ...opts);
lookupCache[cacheKey] = {
data,
cachedAt: Date.now(),
};
return resolve(data);
} catch (err) {
return reject(err);
}
});
}
Then you can await getCachedResult('some-identifier', mws.getLowestPricedOffersForASIN, { ...options }) and it should match any recent requests (i defined 30 minutes as the cache time) to the cache, and pull them out.