简体   繁体   English

禁用浏览器缓存

[英]Disable browser cache

I implemented a REST service and i'm using a web page as client. 我实现了一个REST服务,我正在使用一个网页作为客户端。 My page has some javascript functions that performs several times the same http get request to REST server and process the replies. 我的页面有一些javascript函数,它们对REST服务器执行多次相同的http get请求并处理回复。

My problem is that the browser caches the first reply and not actualy sends the following requests.. 我的问题是浏览器缓存第一个回复而不是实际发送以下请求..

Is there some way to force the browser execute all the requests without caching? 有没有办法强制浏览器执行所有请求而不缓存? I'm using internet explorer 8.0 我正在使用Internet Explorer 8.0

Thanks 谢谢

Not sure if it can help you, but sometimes, I add a random parameter in the URL of my request in order to avoid being cached. 不确定它是否可以帮到你,但有时,我在我的请求的URL中添加一个随机参数,以避免被缓存。

So instead of having: 所以不要:

http://my-server:8080/myApp/foo?bar=baz

I will use: 我会用:

http://my-server:8080/myApp/foo?bar=baz&random=123456789

of course, the value of the random is different for every request. 当然, random的值对于每个请求都是不同的。 You can use the current time in milliseconds for that. 您可以使用当前时间(以毫秒为单位)。

Not really. 并不是的。 This is a known issue with IE, the classic solution is to append a random parameter at the end of the query string for every request. 这是IE的一个已知问题,经典的解决方案是在每个请求的查询字符串末尾附加一个随机参数。 Most JS libraries do this natively if you ask them to (jQuery's cache:false AJAX option, for instance) 如果你要求它们(jQuery的cache:false例如, cache:false AJAX选项),大多数JS库都本机地执行此操作

Well, of course you don't actually want to disable the browser cache entirely; 好吧,当然你实际上并不想完全禁用浏览器缓存; correct caching is a key part of REST and the fact that it can (if properly followed by both client and server) allow for a high degree of caching while also giving fine control over the cache expiry and revalidation is one of the key advantages. 正确的缓存是REST的一个关键部分,它可以(如果客户端和服务器都适当地遵循)允许高度缓存,同时还可以对缓存到期进行精确控制,并且重新验证是关键优势之一。

There is though an issue, as you have spotted, with subsequent GETs to the same URI from the same document (as in DOM document lifetime, reload the page and you'll get another go at that XMLHttpRequest request). 正如您所发现的那样,存在一个问题,即后续GET与同一文档中的相同URI(在DOM文档生命周期中,重新加载页面,您将再次获得该XMLHttpRequest请求)。 Pretty much IE seems to treat it as it would a request for more than one copy of the same image or other related resource in a web page; IE似乎很好地对待它,因为它会在网页中请求同一图像或其他相关资源的多个副本; it uses the cached version even if the entity isn't cacheable. 它使用缓存版本,即使实体不可缓存。

Firefox has the opposite problem, and will send a subsequent request even when caching information says that it shouldn't! Firefox有相反的问题,即使缓存信息说它不应该发送后续请求!

We could add a random or time-stamped bogus parameter at the end of a query string for each request. 我们可以在每个请求的查询字符串末尾添加一个随机或带时间戳的伪参数。 However, this is a bit like screaming "THIS IS SPARTA!" 然而,这有点像尖叫“这是SPARTA!” and kicking our hard-won download into a deep pit that no Health & Safety inspector considered putting a safety rail around. 并将我们来之不易的下载放入一个深坑中,没有健康与安全检查员考虑过安全导轨。 We obviously don't want to repeat a full unconditional request when we don't need to. 当我们不需要时,我们显然不想重复完整的无条件请求。

However, this behaviour has a time component. 但是,此行为具有时间组件。 If we delay the subsequent request by a second, then IE will re-request when appropriate while Firefox will honour the max-age and expires headers and not re-request when needless. 如果我们将后续请求延迟一秒,那么IE将在适当的时候重新请求,而Firefox将遵守max-age并过期标头,而不是在不必要时重新请求。

Hence, if two requests could be within a second of each other (either we know they are called from the same function, or there's the chance of two events triggering it in close succession) using setTimeout to delay the second request by a second after the first has completed will make it use the cache correctly, rather than in the two different sorts of incorrect behaviour. 因此,如果两个请求可能在彼此的一秒内(我们知道它们是从相同的函数调用,或者有两个事件可能在紧密连续中触发它),则使用setTimeout将第二个请求延迟一秒钟后首先完成将使其正确使用缓存,而不是两种不同的错误行为。

Of course, a second's delay is a second's delay. 当然,第二次延迟是第二次延迟。 This could be a big deal or not, depending primarily on the size of the downloaded entity. 这可能是一个大问题,主要取决于下载的实体的大小。

Another possibility is that something that changes so rapidly shouldn't be modelled as GETting the state of a resource at all, but as POSTing a request for a current status to a resource. 另一种可能性是,变化如此之快的东西不应该被建模为完全获取资源的状态,而是将当前状态的请求发布到资源。 This does smell heavily of abusing REST and POSTing what should really be a GET though. 这确实惹恼了滥用REST和发布真正应该是GET的东西。

Which can mean that on balance the THIS IS SPARTA approach of appending random stuff to query strings is the way to go. 这可能意味着总的来说,这就是将随机内容附加到查询字符串的THIS IS SPARTA方法。 It depends, really. 这取决于,真的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM