A
A
Anton2021-03-27 16:59:34
AJAX
Anton, 2021-03-27 16:59:34

How to cache data?

There is a form with a drop-down list, which is filled from an external service. How to implement data caching in order to:
1) If the cache file (arbitrary format, for example text) is missing, make a request to the service and save the result to a file.
2) If the cache file is present and the last date of its modification was today, then get the list of cities from the file.
3) If the file exists, but the modification date is not today, then re-request the list of cities from the service and save it to a file.
With what help it is necessary to implement js or php? Where to start?

function init() {
    let url = "http://exercise.develop.maximaster.ru/service/city/";
    fetch(url)
      .then((response) => {
        return response.json();
      })
      .then((data) => {
        data.forEach((item) => {
          let option = document.createElement("option");
          option.innerHTML = item;
          option.setAttribute("name", "option");
          selected.appendChild(option);
        });
      });
  }
  init();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-03-27
@Miron9900

Don't confuse cache and hash.
You need to do this on the backend. What do you have there - PHP or Node.js, that's it.
But I would make a separate script that updates the data in the database by cron.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question