How to cache a request to a js server?
Hello everyone. When applying for a job, they sent the following problem:
/ * You need to write a f-yu that sends a request to the server.
* This request can be submitted with different filters.
* The function that will be written should be able to cache the request and prevent re-sending to the server with the current
* filter parameters.
* The cache has a lifetime (say 5 seconds), after which it will be destroyed.
* This means that after a second request with the current filter, a request will be made to the server and this data will be cached again.
cache the request and prevent it from being resubmitted to the server with the current filter settings.
do you need it to remain after reloading the page, or really for 5 seconds, in order to avoid a bunch of repeated requests from one user?
you can stupidly make an array of objects, and there, for example,
request: JSON.stringify(body), // тело запроса с фильтрами
// только придется добиться, чтобы фильтры всегда были например по алфавиту
and with a new request, see if there is already such a request in the array, and shutter timestampsAnonymous
To place the code, please use CodePen or similar tool. Thanks you!