http://railscasts.com/episodes/321-http-caching?view=asciicast
這篇來講說在rails 上實作HTTP Caching的機制
首先可以用 curl這個命令來看到說request後respond了什麼
terminal
$ curl -I http://localhost:3000/ HTTP/1.1 200 OK Content-Type: text/html; charset=utf-8 X-Ua-Compatible: IE=Edge Etag: "d3132af4f574ff53eb69e6fa5523fe2a" Cache-Control: max-age=0, private, must-revalidate X-Request-Id: 2cb31eeec2b1beda8e1342f8caa90acd X-Runtime: 0.018216 Content-Length: 0 Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09) Date: Fri, 03 Feb 2012 21:09:22 GMT Connection: Keep-Alive Set-Cookie: _store_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWQ2N2U5MGEwMWZhNGU4ZTI2NzIxOTYxOWE1ODYyNzcxBjsAVEkiEF9j
這邊要看的有幾個
1.Etag
2.Last-Modified
3.Cache-Control
Cache-Control: max-age=0, private, must-revalidate
程式碼
可透過fresh_when來設定
fresh_when does a couple of things
1.checks the Etag matches or not
2.If matches : send a 304 Not Modified response instead of rendering the actual template
3.If not matches , the page will rendered as normal
Rack:cache -->This is known as a reverse proxy cache or gateway cache
rails 已經自動將他包進來在production環境
如果你要在development上使用
/config/environments/development.rb
config.action_controller.perform_caching = true
如果是敏感性資料時
可以設定當目前為public時 不要顯現
/app/views/layouts/application.html.erb
<%= csrf_meta_tag unless response.cache_control[:public] %>
沒有留言:
張貼留言