Bra*_*don 3 caching reverse-proxy ruby-on-rails rack-cache
下午好,
我遇到了一些问题,试图将HTTP缓存与Rack :: Cache和动作缓存相结合(在我的Heroku托管的应用程序上).
单独使用它们似乎正在起作用.启用操作缓存后,页面加载很快,日志会建议缓存.通过控制器中的HTTP缓存(eTag,last_modified和fresh_when),似乎设置了正确的标头.
但是,当我尝试将两者结合起来时,它似乎是动作缓存,但HTTP标头总是max_age:0,must_revalidate.为什么是这样?难道我做错了什么?
例如,这是我的"home"动作中的代码:
class StaticPagesController < ApplicationController
layout 'public'
caches_action :about, :contact, ......, :home, .....
......
def home
last_modified = File.mtime("#{Rails.root}/app/views/static_pages/home.html.haml")
fresh_when last_modified: last_modified , public: true, etag: last_modified
expires_in 10.seconds, :public => true
end
Run Code Online (Sandbox Code Playgroud)
对于所有意图和目的,这应该有一个公共缓存控制标记,max-age 10 no?
$ curl -I http://myapp-staging.herokuapp.com/
HTTP/1.1 200 OK
Cache-Control: max-age=0, private, must-revalidate
Content-Type: text/html; charset=utf-8
Date: Thu, 24 May 2012 06:50:45 GMT
Etag: "997dacac05aa4c73f5a6861c9f5a9db0"
Status: 200 OK
Vary: Accept-Encoding
X-Rack-Cache: stale, invalid
X-Request-Id: 078d86423f234da1ac41b418825618c2
X-Runtime: 0.005902
X-Ua-Compatible: IE=Edge,chrome=1
Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)
配置信息:
# Use a different cache store in production
config.cache_store = :dalli_store
config.action_dispatch.rack_cache = {
:verbose => true,
:metastore => "memcached://#{ENV['MEMCACHE_SERVERS']}",
:entitystore => "memcached://#{ENV['MEMCACHE_SERVERS']}"#,
}
Run Code Online (Sandbox Code Playgroud)
在我看来,您应该能够使用动作缓存以及反向代理吗?我知道他们做了相似的事情(如果页面发生变化,代理和动作缓存都将无效并需要重新生成),但我觉得我应该能够同时拥有这两者.或者我应该摆脱一个?
UPDATE
谢谢你的回答!它似乎工作.但是为了避免为每个控制器动作编写set_XXX_cache_header方法,你有没有看到为什么这不起作用的原因?
before_filter :set_http_cache_headers
.....
def set_http_cache_headers
expires_in 10.seconds, :public => true
last_modified = File.mtime("#{Rails.root}/app/views/static_pages/#{params[:action]}.html.haml")
fresh_when last_modified: last_modified , public: true, etag: last_modified
end
Run Code Online (Sandbox Code Playgroud)
小智 5
使用操作缓存时,仅缓存响应正文和内容类型.对后续请求不会对响应进行任何其他更改.
但是,即使在缓存操作本身时,操作缓存也会在过滤器之前运行.
所以,你可以这样做:
class StaticPagesController < ApplicationController
layout 'public'
before_filter :set_home_cache_headers, :only => [:home]
caches_action :about, :contact, ......, :home, .....
......
def set_home_cache_headers
last_modified = File.mtime("#{Rails.root}/app/views/static_pages/home.html.haml")
fresh_when last_modified: last_modified , public: true, etag: last_modified
expires_in 10.seconds, public: true
end
Run Code Online (Sandbox Code Playgroud)