Rails page caching with subdomain

笑着哭i 提交于 2019-12-06 05:28:23

You need to update the cache location based on which subdomain is in use.

You can add a before_filter to do this.

An example would be:

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

I used this guy's post (with a few modifications)

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

Then just use the usual caches_page class method.

The downfall of this is that, I need to hack the expire_page as well (which was not mentioned in the post). Also Rails won't use existing page cache if it exists, since it won't find it in the default path.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!