是否应该为每个服务器(动态)响应禁用缓存?
作为问题标题,应通过以下方式禁用缓存:
在请求中附加唯一的 javascript 生成的字符串:
/REST/data.php?u
=32dajasda98s1641k801发送缓存控制标头:
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
< em>每次使用ajax从服务器向客户端发送动态内容?什么时候有必要,什么时候不需要?
As question title, caching should be disabled either by:
appending unique javascript generated string in request:
/REST/data.php?u=32dajasda98s1641k801
sending Cache-control header:
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
every time you send dynamic content from the server to the client using ajax? When it's necessary and when it's not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,每次您希望保证下次请求数据时从服务器获得“新鲜”响应时,您都需要以一种或另一种方式禁用缓存。如果不这样做,您和服务器之间的任何代理或服务器本身都可以缓存响应,并在下一个请求时再次将其返回给您。
这可以直接在脚本中完成,如您的示例或在(大多数?所有?)Web 服务器中,通过配置不应缓存的 URL,服务器将为您处理它。
您提到的两种方法中更“礼貌”的肯定是缓存控制标头,它使代理根本不缓存。动态生成的可能仍会发送允许代理缓存的标头,从而不必要地填充缓存,因为每个 URL 只发出一个请求。
Basically you'll need to in one way or another disable caching every time you want to be guaranteed to get a "fresh" response from the server the next time you request data. If you don't, any proxy between you and the server or the server itself is allowed to cache the response and return it to you again on the next request.
This can be done straight in the script as in your examples or in (most? all?) web servers by configuring URLs that shouldn't be cached and the server will handle it for you.
The more "polite" of the two methods you mention is definitely the Cache Control Header which makes the proxies not cache at all. The dynamically generated one may still send headers allowing the proxy to cache, filling the cache up un-necessarily since only one request is made per URL.