2012-05-23 3 views
2

누구나 서브 도메인으로 레일 페이지 캐싱을 성공적으로 구현 했습니까?서브 도메인으로 레일 페이지 캐싱

레일스가 서브 도메인이 변경되었다는 사실을 인식하지 못하므로 동일한 파일이 서브 도메인간에 제공됩니다.

나는이 같은 것을보고 내 페이지 캐시를하고 싶습니다

: 그래서 그들이있어 일단 이러한 파일을 찾기 위해 설정 파일의 변경해야합니다

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

나는이 페이지를 봉사의 nginx를 사용하고 있습니다를 캐시 된, 그건 문제가되지 않습니다. 그 순간 나를 괴롭히는 것은 레일 끝에있다.

답변

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 
+3

즉석에서 page_cache_directory를 변경하는 것이 위험합니다. 이것은 클래스 변수가 아닌가? 두 가지 요청이 동시에 들어올 수있어 잘못된 하위 캐시에 잘못된 캐시가 기록 될 수 있다는 것이 내 걱정입니다. – KJF

1

내가 사용하는이 남자의 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는 기존 페이지 캐시가 있으면 기본 경로에서 찾지 않으므로 기존 페이지 캐시를 사용하지 않습니다.