Cache or not cache

I have an ajax application in which the client can often search for a bunch of data (say, by key stroke), the data is updated on the server side once or twice a day at fixed moments by the daemon. In order not to visit the server often, I store the data in an XML file, so the client loads it once, when the page loads first, and then views the data from the local data file through javascript. But the user can load the page shortly before the changes, and then start using it without refreshing the page, so the data file is never updated, so keep telling the user that new data is not available. How to solve this problem?

+3
source share
5 answers

You must set the appropriate HTTP cache headers for this generated XML file so that any client request receives a new version during this time and caches it locally, like any other static content.

+3
source

If the data load is not very big ... Include the data in the main document as an XML island. Either form it during the creation of the document (aspx, php, whatever), or fill out (via ajax-calls) the reserved node document at startup. Thus, your user always has the latest data, you do not need to worry about caching, and life is much easier.

If it's large, populate this node as needed through ajax calls.

0
source

- AJAX, x . , - , , , . , ( x ).

HEAD, .

0

, FIXED? , , javascript, , , , , , :

timer = {   run: function() {       if (now + minuteToUpdate > updateTime - startVisitTime) {

        // make ajax request here to update XML file
    }
},
interval: //you can determine this since this will run in client-side

}

, .

SESSION,

0

...

Or, since the update time is fixed and infrequent, then when you service your XML, also include the cache expiration time as an element or a custom header. Thus, if your user visits the site 1 minute before updating the XML, you can encode your client to expire its cache and update itself at the next request made after this mark of 1 minute.

0
source

Source: https://habr.com/ru/post/1712735/


All Articles