不能在浏览器只使用从先前的Ajax调用缓存?能在、缓存、先前、浏览器

2023-09-10 16:27:17 作者:旧街旧巷等旧人

我试图依靠浏览器缓存举行 返回的JSON数据从阿贾克斯jQuery的要求。

I am trying to rely upon the browser cache to hold JSON data returned from AJAX calls in jQuery.

普通浏览器活动依赖于浏览器缓存中所有的时间。 例如:JPG和GIF图像不重新获取一个页面重新加载

Normal browser activity relies upon the browser cache all the time. Example: jpg and gif images are not refetched on a page reload.

但是当我尝试使用jQuery的getJSON Ajax调用,我似乎无法避免从服务器获取数据。

But when I try using jQuery getJSON ajax calls, I cannot seem to avoid fetching the data from the server.

我返回头看起来像这样(与萤火虫确认):

My returned headers look like this (confirmed with firebug):

Transfer-Encoding: chunked
Date: Wed, 05 Aug 2009 02:55:39 GMT
Content-Type: text/plain; charset=ISO-8859-1
Expires: Wed, 05 Aug 2009 03:55:39 GMT
Cache-Control: max-age=3600

然而,页面立即刷新导致相同的请求命中服务器。

Yet an immediate refresh of the page causes identical requests to hit the server.

我已经看到了有关避免缓存行为,这不是我所需要的多个岗位。 我已经看到了关于利用缓存多个岗位,但这些似乎都依赖 在保存数据的DOM。我想要的东西,表现在重新加载页面,就像缓存图像做的。

I've seen several postings about avoiding caching behavior, which isn't what I need. I've seen several postings about utilizing caching, but those all seem to rely upon saving data in the DOM. I want something that behaves just like cached images do during a page reload.

广东话浏览器刚刚从它自己的高速缓存中获取呢?

Cant the browser just fetch it from it's own cache?

- X - X - X - X更新--x - X - X -

--x--x--x--x UPDATE --x--x--x--

但令我失望的是,一些可敬的人都认为这不仅是可能的。 有些人甚至认为,这不应该是(这还是令我感到困惑)。

Much to my disappointment, several respectable folks agree that this isn't just possible. Some even argue that it shouldn't be (which still baffles me).

Stubburn一个错误,我试过如下:

Stubburn to a fault, I tried the following:

我设置了​​ETag头在所有外发的网页我想缓存 (我挑选重新present我请求并只使用了ETag值数据几个选择URL参数)

I set the Etag header on all outgoing pages I want to be cached (I pick a few choice URL arguments that represent the data I'm requesting and just use that for the Etag value)

目前的下一个请求的开始,我只是检查如果 - 无 - 匹配标头请求。如果是这样的话,浏览器不缓存请求像我想要的东西,所以我发了304未修改响应。

At the beginning of the next request, I simply check if the 'If-None-Match' header is in the request. If so, then the browser isn't caching the request like I wanted, so I sent a 304 Not Modified response.

测试显示,Firefox将不缓存我的要求 (但我还是能避免我的CGI的获取昂贵的数据部分), 而IE6实际上将缓存它(甚至不会尝试从服务器获取回)。

Testing shows that Firefox won't cache my request (but I can still avoid the 'fetch the expensive data' part of my cgi), while IE6 will actually cache it (and wont even attempt fetching back from the server).

这不是一个pretty的答案,但它的工作对我来说,现在 (图数据的那些pesty全页面刷新不会这么慢或昂贵了)。

It's not a pretty answer, but it's working for me for now (those pesty full-page refreshes of graph data wont be so slow or expensive now).

(什么?我运行的IE6!妈呀!哦,看松鼠!)

(What? I'm running IE6! OMG! Oh look a squirrel!)

推荐答案

阿贾克斯缓存是可能的,predictable(至少在IE和Firefox)。

Ajax caching is possible and predictable (at least in IE and Firefox).

本博客文章讨论了阿贾克斯缓存并具有演示网页:

This blog post discusses Ajax caching and has a demo web page:

http://blog.httpwatch.com/2009/08/07/ajax-caching-two-important-facts/

还有对F5问题上跟进史蒂夫Souders的:

There's also a follow up by Steve Souders on the F5 issue:

http://stevesouders.com/tests/ajax_caching.php