2012-05-31 3 views
3

안녕하세요, 나는 (내 Heroku가 호스트 응용 프로그램에) 약간의 랙과 HTTP 캐싱을 결합하는 시도 문제 : 캐시와 행동 캐싱으로 실행했습니다레일 3.2 랙 :: 캐시 HTTP 헤더와 액션 캐싱

.

개별적으로 사용하면 효과가있는 것 같습니다. 액션 캐싱을 사용하도록 설정하면 페이지로드가 원활하지 않으며 로그에 캐싱이 제안됩니다. 컨트롤러 (eTag, last_modified 및 fresh_when)에 HTTP 캐싱을 사용하면 적절한 헤더가 설정되어있는 것처럼 보입니다.

그러나 둘을 결합하려고하면 액션 캐싱 인 것처럼 보이지만 HTTP 헤더는 항상 max_age : 0, must_revalidate입니다. 왜 이런거야? 내가 뭔가 잘못하고 있는거야?

예를 들어, 다음 코드는 내 "집"행동의 모든 의도와 목적을 위해

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 

, 이것은 최대-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 

구성 정보 : 내 마음에

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

, 당신은 올바른 역방향 프록시뿐만 아니라 액션 캐싱을 사용 할 수 있어야한다? 나는 그들이 페이지가 바뀌면 프록시와 액션 캐시가 모두 무효화되어 다시 생성 될 필요가 있음을 알 수있다.하지만 둘 다 가질 수 있어야한다고 생각한다. 아니면 하나 없애야합니까? 아래의 답변

UPDATE

감사합니다! 작동하는 것 같습니다. 그러나 모든 컨트롤러 조치에 대해 set_XXX_cache_header 메소드를 작성하지 않으려면 이것이 작동하지 않는 이유가 있습니까?

before_filter :set_http_cache_headers 

..... 

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

답변

5

작업 캐싱을 사용하면 응답 본문과 콘텐츠 형식 만 캐시됩니다. 후속 요청에서 응답의 다른 변경 사항은 발생하지 않습니다.

그러나 작업 캐싱은 작업 자체가 캐시 된 경우에도 필터 이전의 모든 항목을 실행합니다.

그래서, 당신은 같은 것을 할 수 있습니다 : 입력에 대한

class StaticPagesController < ApplicationController 
    layout 'public' 

    before_filter :set_home_cache_headers, :only => [:home] 

    caches_action :about, :contact, ......, :home, ..... 

    ...... 

    def set_home_cache_headers 
    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 
+0

감사합니다! 나는 그 장면을 줄 것이고 그것이 나를 위해 어떻게 작용하는지 보게 될 것이다. – Brandon

+1

이것이 작동하는 것 같습니다! 그러나 체크 아웃하는 것을 신경 쓰지 않는다면 업데이트에 대한 빠른 질문이 있습니다 (18 가지 별도의 메서드 작성을 피할 수있는 잠재적 방법). – Brandon

+0

'before_filter'는 필터가 올바른 순서로 실행되도록하기 위해'caches_action'에 대한 호출 전에 나열되어야 함을주의하십시오. –