当发送带有缓存的结果问题一XMLHtt prequest? [英] Problems with cached result when sending a XMLHttpRequest?

查看:122
本文介绍了当发送带有缓存的结果问题一XMLHtt prequest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的AJAX的想法以及缓存。

AJAX - 发送一个请求到从W3Schools的服务器 ,它说你应该添加?T =+的Math.random()到脚本的网址的结尾运行到prevent缓存。

在维基百科,缓存的简单定义是:

  

在计算机科学中,缓存是透明地存储数据,以便为将来的数据请求可以提供更快的一个组成部分。存储的高速缓存内的数据可能是存储在别处已较早计算原始值的重复值,或者

但是,不应该这样会更好?该脚本将运行得更快,如果计算机已经存储了一些重复的数据。此外,本教程页面上的第一个例子中,不添加到URL,工作得很好。

有人可以告诉我用背后的原因?T =+的Math.random()

解决方案
  

但是,不应该这样会更好?

是的,它最好有一个缓存系统的性能比较的原因,你的应用程序页面将加载快,因为一旦装的元素未做每次HTTP请求到服务器进行检索。

  

有人可以告诉我用背后的原因?T =+的Math.random()?

添加此?T =+的Math.random()就像重命名脚本的每次重新加载时间的URL。该缓存系统会认为这是一个新的元素,而不是作为一个旧的,他作为已存储,即使没有作为真正改变。因此它迫使重新加载来自服务器的元素

通常情况下,我们可能会想这样做的元素(如图片,脚本)经常更新。例如,它是用于在网站,用户可以改变配置文件图象的情况下,如果旧图像文件是在高速缓存中,用户将不会看到新的图像出现立刻如果我们不使用随机数的该特技。用户可以认为他的上传没有工作。他将不得不手动清空缓存在他的浏览器,这并不总是很直观。

第二个原因可能是因为这是很好的做到这一点,而我们正在深化发展,因为我们不需要清空缓存,我们的code的变化都考虑到每分钟...

不过不要你肯定会不改变或很少的元素用这一招。

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()?

解决方案

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.

这篇关于当发送带有缓存的结果问题一XMLHtt prequest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆