How to Make AJAX Cacheable

How to Make AJAX Cacheable

One of the cited benefits of Ajax is that it provides instantaneous feedback to the user because it requests information asynchronously from the backend web server. However, using Ajax is no guarantee that the user won’t be twiddling his thumbs waiting for those asynchronous JavaScript and XML responses to return. In many applications, whether or not the user is kept waiting depends on how Ajax is used. For example, in a web-based email client the user will be kept waiting for the results of an Ajax request to find all the email messages that match their search criteria. It’s important to remember that “asynchronous” does not imply “instantaneous”.

Now How to Make AJAX Cacheable:

The browser will cache AJAX queries just as it would regular HTTP queries on the client side. To enable this behavior, your application must make use of the “cache control” headers, and the “expires” header when serving the AJAX response. You can also use the last fetch time that’s sent as a request header to determine if you can respond with a 304 response. If you’re mostly concerned with the database fetch time, you can use a query caching system like memcached to speed up responses, or even cache results so that you can quickly send the same body back on the same request set.

It turns out that these techniques are exactly the same as standard HTTP, because AJAX is nothing more than regular old HTTP calls that return (typically) JSON, XML or partial HTML content instead of full HTML pages.

The following two tabs change content below.

Hamza Basharat

Hi. I'm Hamza Basharat, I love to find solutions and help people. I'm a senior writer at TheHelptimes and AboutEngg, and currently studying computer engineering.