HTTP cache on Rails?

紅寶鐵軌客
來關注...
關注/停止關注:紅寶鐵軌客
關注有什麼好處?:當作者有新文章發佈時,「思書」就會自動通知您,讓您更容易與作者互動。
現在就加入《思書》,你就可以關注本作者了!
《思書》是一個每個人的寫作與論壇平台,特有的隱私管理,讓你寫作不再受限,討論更深入真實,而且免費。 趕快來試試!
還未加入《思書》? 現在就登錄! 已經加入《思書》── 登入
寫程式中、折磨中、享受中 ......
460   0  
·
2021/04/25
·
3分鐘


寫這個很心虛,因為有太多的不確定,我也是剛在摸索,我當作是紀錄,有緣的讀者就當作是笑話來看吧。

我想大家辛苦做出來的網站上線後,不管是被 PageSpeed Insights 建議,或是要用 CDN,或是自己突然想到,大概都會把 HTTP cache 打開,剛開始的時候,看到 Rails 的簡介,就覺得很簡單啊,在  Applications Controller 上加個 before_action 呼叫以下的 :set_cache_headers,然後,美好的日子就開始了,cache 運作了,網站變快了 ⋯⋯

def set_cache_headers

  # max-age 14 days (=60x60x24x14=1209600)
  response.headers["Cache-Control"] = "private, no-cache, max-age=1209600, must-revalidate"
  response.headers["Pragma"] = "no-cache"
  response.headers["Expires"] = 30.days.from_now.httpdate

end

可是,很快就會發現,奇怪,為什麼有寫好像沒寫,根本沒用,除非你是用 Heroku,可愛的 PageSpeed Insights 還是會告訴你:

Serve static assets with an efficient cache policy XX resources found

睡一覺起床後,就會想到,對ㄟ,HTTP cache 不是應該由 web server 來做嗎?!沒錯,HTTP Cache 就是要透過 Apache 或是 Nginx 這些妖魔鬼怪來運作的啦,不然,這些 validate 動作要由誰來做? 我只會用一點  Apache,也是剛學的,以下就是我的

最精簡版 Apache2 on Ubuntu Http cache 設定的過程:

要先安裝四個 Mod 及一個 Utility 

sudo a2enmod cache
sudo a2enmod cache_disk
sudo a2enmod expires
sudo a2enmod headers
sudo apt-get install apache2-utils

再來就是要修改 site conf,就是那個在 sites-available 裡,一般叫做 default-xxx.conf 的那個,把以下的碼加上去:

CacheQuickHandler off
CacheLock on
CacheLockPath /tmp/mod_cache-lock
CacheLockMaxAge 5
CacheIgnoreHeaders Set-Cookie

<Location />
  CacheEnable disk
  CacheHeader on

  CacheDefaultExpire 600
  CacheMaxExpire 86400
  CacheLastModifiedFactor 0.5
  ExpiresActive on
  ExpiresDefault "access plus 60 minutes"

  Header merge Cache-Control private
  FileETag All
</Location>

詳細說明請看最下面的參考,最主要的是 15 行,就是那裡設定了 max-age: 60 分鐘,也就是 max-age=60x60=3600,還有就是第 17 行,會無條件加上 private,可以改成 public,看你需要,總之,裝了這麼多 Mod,又寫這麼多行字後,你只不過就等於是把 Http cache 的 cache-control 設定成:

Cache-Control: private, max-age=3600 

改好了?不要忘記用 configtest 測試一下,就可以重新啟動 Apache 了

sudo apachectl configtest
$ Syntax OK
sudo service apache2 restart

就這樣,最精簡版的 Apache http cache 設定完成了,覺得很麻煩嗎?這已經是最精簡版了,沒有更簡單的方法了,沒錯,cache 就是這麼難,在電腦科學中,Cache 從來都不是一件簡單的事,光在 Apache2 上面的 cache 設定,就有一大堆選項,我看的頭都昏了,所以才想要寫下最精簡版,不寫,我想三天後我就忘光光了,至於要不要了解其他的設定,我想,等到出事了再說吧。

Q:可愛的 PageSpeed Insights 喜歡嗎?
A:喜翻,一個實例:Serve static assets with an efficient cache policy 從 18 變成 4。
A:可是分數沒變高⋯⋯(憂鬱)

還有:http cache 不要亂設,小心客戶端變成永遠不會更新,一開始,少少的幾分鐘的就好。

最後:HTTP Cache 只要會設定 Apache 或是 Nginx 的 cache 就好了嗎?哈哈哈哈,你做夢!漫漫長路等著你。


參考:

這一篇寫得又簡單又清楚,很棒,如果你要懂 Http cache on Apache2,請細讀:

How To Configure Apache Content Caching on Ubuntu 14.04 | DigitalOcean — Web caching is a method of improving server performance by allowing commonly requested content to be temporarily stored in a way that allows for faster access.  DigitalOcean

Rails 5 以後,Assets 可以設定 Http header 了:

Rails 5 allows setting custom HTTP Headers for assets | BigBinary Blog — Rails 5 series | Ability to set custom HTTP header is needed to have better control over the delivery of assets. In this blog we'll see how that can be done. BigBinary





喜歡作者的文章嗎?馬上按「關注」,當作者發佈新文章時,思書™就會 email 通知您。

思書是公開的寫作平台,創新的多筆名寫作方式,能用不同的筆名探索不同的寫作內容,無限寫作創意,如果您喜歡寫作分享,一定要來試試! 《 加入思書》

思書™是自由寫作平台,本文為作者之個人意見。


文章資訊

本文摘自:
Categories:
Tags:
Date:
Published: 2021/04/25 - Updated: 2021/04/25
Total: 881 words


分享這篇文章:
關於作者

很久以前就是個「寫程式的」,其實,什麼程式都不熟⋯⋯
就,這會一點點,那會一點點⋯⋯




參與討論!
現在就加入《思書》,馬上參與討論!
《思書》是一個每個人的寫作與論壇平台,特有的隱私管理,用筆名來區隔你討論內容,讓你的討論更深入,而且免費。 趕快來試試!
還未加入《思書》? 現在就登錄! 已經加入《思書》── 登入


看看作者的其他文章


看看思書的其他文章



×
登入
申請帳號

需要幫助
關於思書

暗黑模式?
字體大小
成人內容未過濾
更改語言版本?