简体   繁体   English

Heroku Cedar Stack-机架缓存头

[英]Heroku Cedar Stack - Rack Cache Headers

I can't figure this out for the life of me. 我无法一生解决这个问题。 Trying to use Rack::Cache to cache some of my static public pages on Heroku, in addition to doing action caching in case it gets past the reverse proxy. 尝试使用Rack :: Cache在Heroku上缓存我的一些静态公共页面,此外还要进行动作缓存以防它超过反向代理。

For example, here's the code in my "home" action: 例如,这是我的“ 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

For all intents and purposes, this should have a public cache-control tag with max-age 10 no? 出于所有目的和目的,应该有一个最大寿命为10的公共缓存控制标签?

$ 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

Am I doing something horribly wrong? 我做错什么了吗? I feel like there is something up with that stale, invalid cache response... that is about the 4th time I've hit the page. 我觉得这种过时的,无效的缓存响应有什么问题……大约是我第四次访问该页面。

Config Info: 配置信息:

# 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']}"#,
}

# OLD : Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true
config.static_cache_control = "public, max-age=2592000"

(Maybe there's a way to manually set the cache-control header? Seems like there should be an easier way though). (也许有一种方法可以手动设置缓存控制标头?不过似乎应该有一种更简单的方法)。

UPDATE 更新

I have even tried taking the controller action down to the bare minimum : 我什至尝试将控制器动作降低到最低限度:

def home
  expires_in 10.seconds, :public => true
  #last_modified = File.mtime("#{Rails.root}/app/views/static_pages/home.html.haml")
  #fresh_when last_modified: last_modified , public: true, etag: last_modified
end

And it doesn't work... 而且不起作用...

HTTP/1.1 200 OK
Server: nginx
Date: Thu, 24 May 2012 19:15:18 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Status: 200 OK
X-Ua-Compatible: IE=Edge,chrome=1
Etag: "733798214c652f39ae79b4037e9111dc"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: b33087fe0c2ae986c4cac88f14420b7c
X-Runtime: 0.006000
X-Rack-Cache: stale, invalid
Vary: Accept-Encoding
X-Varnish: 349105873
Age: 0
Via: 1.1 varnish

!

Maybe rethink the whole thing and just use rails caching. 也许需要重新考虑整个过程,而只是使用Rails缓存。 The new cache_digests gem should make it trivial to do what you want at the rails level: https://github.com/rails/cache_digests 新的cache_digests宝石应使其在Rails级别上轻松完成所需的工作: https : //github.com/rails/cache_digests

I'm willing to bet this will beat your method that includes doing a filesystem call to check the file timestamp every 10 seconds. 我敢打赌这将击败您的方法,该方法包括每10秒执行一次文件系统调用以检查文件时间戳记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM