How to cache a request to a js server?

  • 0
    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.
    * /

    To be honest, I don't understand how to cache the request? Maybe cookie should be used or LocalStorage.
    JavaScript Anonymous, May 30, 2019

  • 1 Answers
  • 0
    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), // тело запроса с фильтрами
    // только придется добиться, чтобы фильтры всегда были например по алфавиту
    response: Object

    and with a new request, see if there is already such a request in the array, and shutter timestamps

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!