对于存储在数据中的图像发送“304未修改”时,是否可以设置“CacheControl:public”

2024-10-06 12:25:45 发布

您现在位置:Python中文网/ 问答频道 /正文

在问了一个关于sending “304 Not Modified” for images stored in the in the Google App Engine datastore的问题后,我现在有一个关于Cache-Control的问题。在

我的应用程序现在发送Last-ModifiedEtag,但默认情况下GAE也发送Cache-Control: no-cache。根据this page

The “no-cache” directive, according to the RFC, tells the browser that it should revalidate with the server before serving the page from the cache. [...] In practice, IE and Firefox have started treating the no-cache directive as if it instructs the browser not to even cache the page.

由于我确实希望浏览器缓存图像,我在代码中添加了以下行:

self.response.headers['Cache-Control'] = "public"

根据同一页:

The “cache-control: public” directive [...] tells the browser and proxies [...] that the page may be cached. This is good for non-sensitive pages, as caching improves performance.

问题是这是否会在某种程度上对应用程序有害?最好发送Cache-Control: must-revalidate来“强制”浏览器重新验证(我想这就是发送Cache-Control: no-cache背后的原因)

This directive insists that the browser must revalidate the page against the server before serving it from cache. Note that it implicitly lets the browser cache the page.


Tags: thenoinbrowser应用程序cacheforthat
3条回答

请参见http://www.kyle-jensen.com/proxy-caching-on-google-appengine,很好地解释了为GAE设置缓存控制头。在

这不会对您的应用程序有害,该页中描述的唯一风险相当于公共代理(如ISP使用的代理)缓存您的映像。如果图像是机密的或特定于用户的,您不希望发生这种情况。在所有其他情况下,缓存正是您想要的。在

没有必要设置Cache-Control: public,除非您的内容受HTTP身份验证或SSL保护。在

尝试设置Cache-Control: max-age=nn(其中nn是整数秒数,您希望缓存考虑刷新响应)。AppEngine应该删除no缓存。在

相关问题 更多 >