Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with cached result when sending a XMLHttpRequest?

I'm new to the idea of AJAX as well as caching.

On the AJAX - Send a Request To a Server from W3Schools, it says you should add "?t=" + Math.random() to the end of the URL of the script to run to prevent caching.

On Wikipedia, the simple definition of "cache" is:

In computer science, a cache is a component that transparently stores data so that future requests for that data can be served faster. The data that is stored within a cache might be values that have been computed earlier or duplicates of original values that are stored elsewhere.

But, shouldn't this be better? The script will run faster if the computer already has some duplicate data stored. Also, the first example on the tutorial page, without the addition to the URL, worked fine.

Can somebody please tell me the reason behind using "?t=" + Math.random()?

like image 797
Jonathan Lam Avatar asked Feb 14 '23 19:02

Jonathan Lam


2 Answers

But, shouldn't this be better?

Yes it's better to have a cache system for perfomance reason, your application pages will load quickly because the elements loaded once will be retrieved without making each time a HTTP request to the server.

Can somebody please tell me the reason behind using "?t=" + Math.random()?

Adding this "?t=" + Math.random() is like renaming the URL of the script each time you reload it. The caching system will see it as a new element and not as an old one he as already stored even if nothing as really changed. So it's forcing to reload the element from the server.

Generally, we may want to do that on elements (like images, scripts) that are often updated. For example, it's the case for a profile picture in a website that a user could change, if the old picture file is in cache, the user will not see the new picture appear immediatly if we don't use that trick of the random number. The user could think his upload didn't work. He would have to empty the cache manually in his browser, which is not always very intuitive.

A second reason could be that it's good to do it while we are developping because we don't need to empty the cache every minutes that our code changes are taken into account...

However don't use this trick on elements you are sure will don't change or very rarely.

like image 191
tomahim Avatar answered Feb 17 '23 10:02

tomahim


The reason behind adding some random element to the end of a web service request like that is because in many cases you want the data to always be fresh. If you are caching it, it is possible the data won't be fresh.

For example, say you have an AJAX request which gives you the current high score of a game. You call it with http://example.com/get_high_score.php. Say it returns 100. Now, say you wait 5 seconds and call this again (or the user refreshes their page). If that request was cached, it may return 100 again. However, in that time, the score may actually now be 125.

If you call http://example.com/get_high_score.php?t=12345786, the score would be the latest value, because it wasn't cached.

url + "?t=" + Math.random() is just one means of doing this. I actually prefer to use a timestamp instead, as that is guaranteed to always be unique.

url + "?t=" + (new Date()).getTime()

On the flip side, if you don't need the data to always be fresh (e.g., you are just sending a list of menu item options which almost never change), then caching is okay and you'd want to leave off the extra bit.

like image 45
samanime Avatar answered Feb 17 '23 11:02

samanime