缓存& GAE上的GZip(社区Wiki) [英] Caching & GZip on GAE (Community Wiki)

查看:114
本文介绍了缓存& GAE上的GZip(社区Wiki)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Google App Engine似乎没有在我的CSS样式表和JavaScript文件上设置适当的缓存友好标题(如远期到期日期)? GAE什么时候gzip这些文件?我的 app.yaml 将相应的目录标记为 static_dir s,所以缺少远期过期日期是种类

这是一个社区维基,用于展示关于静态文件缓存和GAE上gzipping的最佳实践!

解决方案

GAE如何处理缓存?



GAE设置了将来缓存过期时间,但确实使用 etag 标题。这是用来让浏览器可以问的:自从它有一个 etag X68f0o ??听到没有 - 304未修改回应。



与远期到期日相反,有以下权衡:


  • 即使您的最终用户拥有相同的名称,也会获得最新的资源副本(不同于远期到期)。这是良好

  • 然而,您的最终用户仍然需要检查该文件的状态。这确实会减慢你的网站,并且在内容没有改变时是纯粹的开销。这是不理想的。



选择远期缓存过期而不是(仅) etag



使用远期到期日期需要两个步骤,并且需要一些理解。


  1. 您必须手动更新您的应用以请求新版本的资源,例如命名文件如 mysitesstyles.2011-02-11T0411.css 而不是 mysitestyles.css 。有些工具可以帮助实现自动化,但我并不知道任何与GAE直接相关的工具。

  2. 配置GAE以设置所需的过期时间在 app.yaml 中使用 default_expiration 和/或过期关于静态文件的GAE文档




第三种选择:应用程序清单



缓存清单是HTML5功能,缓存标题。 MDN文章 DiveIntoHTML5 W3C 。但是,这不仅影响脚本和样式文件的缓存。小心使用!



何时GAE gzip



根据 Google的常见问题


Google App Engine尽最大努力将gzip内容提供给支持它的浏览器。我们使用请求头(Accept-Encoding,User-Agent)和响应头(Content(内容))的组合。 -Type)来确定最终用户是否可以利用gzip内容。这种方法避免了在流行浏览器中使用gzip内容的一些众所周知的错误。为了强制gzip内容被提供,客户端可以提供'gzip'作为Accept-Encoding和User-Agent请求头的值。如果没有Accept-Encoding头文件,内容将不会被压缩。



运行时环境文档( google.com/appengine/docs/java/runtime.html#Responsesrel =noreferrer> Java | Python )。


一些真实世界的观察显示一般都是如此。假设一个支持gzip的浏览器:




  • GAE gzips实际页面(如果它们有正确的 content-type 头文件,如 text / html; charset = utf-8

  • GAE在<$ c $中gz脚本和样式c> static_dir s(在 app.yaml 中定义)。

  • 请注意,您不应指望GAE将图片像GIF或JPEG那样gzip压缩,因为它们已被压缩。


Why does it seem like Google App Engine isn’t setting appropriate cache-friendly headers (like far-future expiration dates) on my CSS stylesheets and JavaScript files? When does GAE gzip those files? My app.yaml marks the respective directories as static_dirs, so the lack of far-future expiration dates is kind of surprising to me.

This is a community wiki to showcase the best practices regarding static file caching and gzipping on GAE!

解决方案

How does GAE handle caching?

It seems GAE sets near-future cache expiration times, but does use the etag header. This is used so browsers can ask, "Has this file changed since when it had a etag of X68f0o?" and hear "Nope – 304 Not Modified" back in response.

As opposed to far-future expiration dates, this has the following trade-offs:

  • Your end users will get the latest copies of your resources, even if they have the same name (unlike far-future expiration). This is good.
  • Your end users will however still have to make a request to check on the status of that file. This does slow down your site, and is "pure overhead" when the content hasn’t changed. This is not ideal.

Opting for far-future cache expiration instead of (just) etag

To use far-future expiration dates takes two steps and a bit of understanding.

  1. You have to manually update your app to request new versions of resources, by e.g. naming files like mysitesstyles.2011-02-11T0411.css instead of mysitestyles.css. There are tools to help automate this, but I’m not aware of any that directly relate to GAE.

  2. Configure GAE to set the expiration times you want by using default_expiration and/or expiration in app.yaml. GAE docs on static files

A third option: Application manifests

Cache manifests are an HTML5 feature that overrides cache headers. MDN article, DiveIntoHTML5, W3C. This affects more than just your script and style files' caching, however. Use with care!

When does GAE gzip?

According to Google’s FAQ,

Google App Engine does its best to serve gzipped content to browsers that support it. Taking advantage of this scheme is automatic and requires no modifications to applications.

We use a combination of request headers (Accept-Encoding, User-Agent) and response headers (Content-Type) to determine whether or not the end-user can take advantage of gzipped content. This approach avoids some well-known bugs with gzipped content in popular browsers. To force gzipped content to be served, clients may supply 'gzip' as the value of both the Accept-Encoding and User-Agent request headers. Content will never be gzipped if no Accept-Encoding header is present.

This is covered further in the runtime environment documentation (Java | Python).

Some real-world observations do show this to generally be true. Assuming a gzip-capable browser:

  • GAE gzips actual pages (if they have proper content-type headers like text/html; charset=utf-8)
  • GAE gzips scripts and styles in static_dirs (defined in app.yaml).
  • Note that you should not expect GAE to gzip images like GIFs or JPEGs as they are already compressed.

这篇关于缓存&amp; GAE上的GZip(社区Wiki)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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