HTTP cache on Rails?

紅寶鐵軌客
Join to follow...
Follow/Unfollow Writer: 紅寶鐵軌客
By following, you’ll receive notifications when this author publishes new articles.
Don't wait! Sign up to follow this writer.
WriterShelf is a privacy-oriented writing platform. Unleash the power of your voice. It's free!
Sign up. Join WriterShelf now! Already a member. Login to WriterShelf.
寫程式中、折磨中、享受中 ......
574   0  
·
2021/04/25
·
3 mins read


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

我想大家辛苦做出來的網站上線後,不管是被 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





WriterShelf™ is a unique multiple pen name blogging and forum platform. Protect relationships and your privacy. Take your writing in new directions. ** Join WriterShelf**
WriterShelf™ is an open writing platform. The views, information and opinions in this article are those of the author.


Article info

This article is part of:
Categories:
Tags:
Date:
Published: 2021/04/25 - Updated: 2021/04/25
Total: 881 words


Share this article:
About the Author

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




Join the discussion now!
Don't wait! Sign up to join the discussion.
WriterShelf is a privacy-oriented writing platform. Unleash the power of your voice. It's free!
Sign up. Join WriterShelf now! Already a member. Login to WriterShelf.