0

我有一个相同的 Rails 应用程序,其中包含许多域名和细微的 UI 差异。每个域可能有不同的语言环境(en、es、fr 等)。

caches_page用来缓存users控制器的所有对象。我已经尝试了这个解决方案,并稍作修改以使用域和语言环境而不是子域。

  def self.page_cache_path(path,extension)  
    MyApp::Application.config.action_controller.page_cache_directory + '/cache' + @root_site.to_s + "/" + path + extension
  end

  def cache_page(content = nil, options = nil, gzip = Zlib::BEST_COMPRESSION)
    path = [@root_site, I18n.locale].join("/") # nil would add slash to 2 ends
    path << case options
    when Hash
      url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :format => params[:format]))
    when String
      options
    else
      if request.path.empty? || request.path == '/'
        '/index'
      else
        request.path
      end
    end
    super(content, path, gzip)
  end

这段代码似乎可以很好地写入缓存文件,将域名作为第一个文件夹:

Write page /Users/user/Sites/myapp/public/cache/domain1.com/en/users/john.html (1.7ms)
Completed 200 OK 

我看到的问题是当我访问任何缓存页面时它没有检索缓存:

Started GET "/users/john" for 127.0.0.1 at 2013-01-16 19:04:18 -0200
Processing by UsersController#show as HTML
...
Write page /Users/user/Sites/myapp/public/cache/domain1.com/en/users/john.html (1.7ms)
Completed 200 OK 

在我的users控制下,我只有这个

caches_page :show

任何人都知道是什么阻止了从缓存文件中读取以及如何解决它?

4

1 回答 1

2

如果您希望 Rails 按主机名缓存 + 按主机名提供正确的缓存,那么您应该caches_action :show改用。缓存将存储在 Rails.cache 中,并由 Rails 自己的机制提供服务。

动作缓存在内部使用片段缓存和一个环绕过滤器来完成这项工作。片段缓存根据请求的主机和路径命名。在http://david.example.com/lists/show/1访问的页面将生成一个名为 david.example.com/lists/show/1 的片段。 http://api.rubyonrails.org/classes/ActionController/Caching/Actions.html

但是,如果您想保留页面缓存并在提供缓存内容时完全避免使用 Rails,您应该配置您的Web 服务器以通过组合主机名在自定义路径中查找静态文件。

例如,引用nginx conf 的page_cache_fu示例

if (-f $document_root/cache/$host/$uri/index.html) {
  rewrite (.*) /cache/$host/$1/index.html break;
}

if (-f $document_root/cache/$host/$uri.html) {
  rewrite (.*) /cache/$host/$1.html break;
}

我没有测试过上面的conf,但这就是想法。在大多数网络服务器上都应该可以实现

于 2013-01-17T04:49:26.533 回答