【问题标题】:Rails page caching with subdomain带有子域的 Rails 页面缓存
【发布时间】:2012-05-23 08:21:03
【问题描述】:

有没有人用子域成功实现过 Rails 页面缓存?

现在,由于 rails 无法识别子域已更改的事实,因此在子域之间提供相同的文件。

我希望我的页面缓存看起来像这样:

/public/cache/subdomain1/index.html
/public/cache/subdomain1/page2.html
/public/cache/subdomain2/index.html
/public/cache/subdomain2/page2.html

我正在使用 nginx 来提供这些页面,因此需要更改其配置文件以在缓存这些文件后找到这些文件,这不会有问题。目前对我来说,症结在于轨道末端。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 caching subdomain


    【解决方案1】:

    您需要根据正在使用的子域更新缓存位置。

    您可以添加before_filter 来执行此操作。

    一个例子是:

    class ApplicationController < ActionController::Base
      before_filter :update_cache_location
    
      def update_cache_location
        if current_subdomain.present?
          ActionController::Base.page_cache_directory = "#{Rails.public_path}/cache/#{current_subdomain.directory_name}"
        end
      end
    end
    

    【讨论】:

    • 动态更改 page_cache_directory 似乎有点危险。这不是类变量吗?我担心两个请求可能同时进入,这可能导致为错误的子域写入错误的缓存。
    【解决方案2】:

    我使用了这个人的post(稍作修改)

    class ApplicationController < ActionController::Base
    
      # Cache pages with the subdomain
      def cache_page(content = nil, options = nil, gzip = Zlib::BEST_COMPRESSION)
        path = [nil, request.subdomains, nil].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
    

    然后就使用通常的 caches_page 类方法。

    这样做的缺点是,我还需要破解 expire_page (帖子中没有提到)。如果存在,Rails 也不会使用现有的页面缓存,因为它不会在默认路径中找到它。

    【讨论】:

      猜你喜欢
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 2012-08-31
      • 2012-03-22
      • 2021-04-17
      • 1970-01-01
      相关资源
      最近更新 更多