2017-07-24 89 views
0

我對緩存很陌生,所以我一直在嘗試一些不同的方式來緩存我的網站。我現在已經決定使用HTTP緩存,因爲它對於零星更新是最合適的,並且許多用戶反覆瀏覽相同的頁面。rails 4 http緩存返回200 iso 304,即使使用相同的ETag和last_modified

但我努力讓它工作。該網站根據您是否登錄顯示了不同的內容,因此我必須根據current_user以及模型集合上的最新更新來使緩存無效。

如果我查看chrome檢查ETag和modified_since是相同的,但服務器返回的是200而不是304.我的代碼在開發環境中工作,所以我在如何排除故障時丟失了。另外一個僅基於模型集合無效的不同頁面(類似於最新更新),按預期工作。從控制器

代碼:

def index 
    ...#some code 

    # HTTTP caching: 
    last_mod = @scraps.order("updated_at").last.updated_at 
    user = current_user ? current_user.id : 0 

    fresh_when etag: user.to_s, last_modified: last_mod, public: false 
end 

從鉻輸出檢查

響應頭:

HTTP/1.1 200 OK 
Content-Type: text/html; charset=utf-8 
Transfer-Encoding: chunked 
Connection: keep-alive 
Vary: Accept-Encoding 
Status: 200 OK 
Last-Modified: Sun, 23 Jul 2017 20:40:53 GMT 
Cache-Control: max-age=0, private, must-revalidate 
ETag: W/"6e92592bdb6c3cf610020e2b076e64b4" 
X-Frame-Options: SAMEORIGIN 
X-XSS-Protection: 1; mode=block 
X-Content-Type-Options: nosniff 
X-Runtime: 3.187090 
X-Request-Id: c698c0c6-8a0d-44ba-8ca9-3f162b766478 
Date: Mon, 24 Jul 2017 14:49:38 GMT 
Set-Cookie: ... [edited out]; path=/; HttpOnly 
X-Powered-By: Phusion Passenger 5.0.30 
Server: nginx/1.10.1 + Phusion Passenger 5.0.30 
Content-Encoding: gzip 

請求報頭:

GET /scraps?page=3&price_max=100&price_min=0&producer=silk+scraps HTTP/1.1 
Host: www.picture-scraps.com 
Connection: keep-alive 
Accept: text/html, application/xhtml+xml, application/xml 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36 
X-XHR-Referer: https://www.picture-scraps.com/scraps?page=4&price_max=100&price_min=0&producer=silk+scraps 
Referer: https://www.picture-scraps.com/scraps?page=4&price_max=100&price_min=0&producer=silk+scraps 
Accept-Encoding: gzip, deflate, br 
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4,af;q=0.2 
Cookie: ... [edited out] 
If-None-Match: W/"6e92592bdb6c3cf610020e2b076e64b4" 
If-Modified-Since: Sun, 23 Jul 2017 20:40:53 GMT 

我可以想象一些額外的信息是需要的,所以請請求,我會加入到問題中。

+0

從哪裏開始尋找沒有任何提示? –

回答

0

今天想出來。 This post提供了答案。我看到服務器使用了弱etags,而在開發環境中使用了強etags。後者正如預期的那樣,弱etags只從軌道5向前推進。

如果您在rails 4上使用Nginx,您可能會遇到同樣的問題。安裝rails_weak_etags寶石爲我解決了它。

相關問題