Axios is a Promise based HTTP client: github.com/axios/axios
I would like to implement a cache control for GET requests. So that I don't query the same URL twice during a page load. That would be a cache that works for 10-20 seconds.
I would like to implement it as transparent as possible. Right now I call Axios by window.axios.get(url).then(response => {}) and the best would be to override the .get() method to inject the cache system.
Marco Alka
Software Engineer, Technical Consultant & Mentor
I wrote a cache system once, which works like this:
Mapshould work) if it contains the result data and when the call was made (timestamp)Map, which buffers in-transit requestPromisesPromiseand store an array with the next item containing refs to thePromise's resolve and reject methods.Promises by calling the correct method from the bufferMap.Maptogether with a timestamp.Hashing the parameters should be simple, if you JSON-stringify them and then use something like this for the hash:
// stackoverflow.com/a/34842797 const hash = str => str.split('').reduce((prevHash, currVal) => (((prevHash << 5) - prevHash) + currVal.charCodeAt(0))|0, 0);