SQUID refresh_pattern詳解
refresh_pattern 大概是 squid 最有意思但最不好懂的配置參數了,經過看書,大概明白如何使用,寫出來貢獻。
記住refresh_pattern 隻對後端沒設定Expires過期時間的頁面起作用,比如論壇頁面;而對類似apache mod_expires 設定過的頁面不起作用。
說明之前,先将個概念LM,LM就是頁面Header裡時間(Date)和Last-Modified時間的差。Date一般是Squid從後面取頁面的時間,Last-Modified 一般是頁面生成時間。
refresh_pattern 的文法是
Code:
refresh_pattern [-i] regexp min percent max [options]
regexp 就不講了,大家都明白的;)
min, max的機關是分鐘,percent就是百分比。
refresh_pattern 的算法如下:(目前時間定義為CURRENT_DATE)
1) If ((CURRENT_DATE-DATE(就是LM裡定義的時間)) < min),cache是新鮮的
2) else if ((CURRENT_DATE-DATE) < (min + (max-min)*percent),cache是新鮮的
3) else cache是過期的
cache過期就需要從後面server取新鮮内容。
如果希望頁面一進入cache就不删除,直到被主動purge掉為止,可以加上ignore-reload選項
一般情況可以使用 reload-into-ims。
舉例:
refresh_pattern -i /.gif$ 1440 50% 2880 ignore-reload
refresh_pattern -i /.jpg$ 1440 50% 2880 ignore-reload
refresh_pattern -i /.png$ 1440 50% 2880 ignore-reload
refresh_pattern -i /.mp3$ 1440 50% 2880 ignore-reload
refresh_pattern -i /.wmv$ 1440 50% 2880 ignore-reload
refresh_pattern -i /.rm$ 1440 50% 2880 ignore-reload
refresh_pattern -i /.swf$ 1440 50% 2880 ignore-reload
refresh_pattern -i /.mpeg$ 1440 50% 2880 ignore-reload
refresh_pattern -i /.wma$ 1440 50% 2880 ignore-reload
refresh_pattern -i /.css$ 10 50% 60 reload-into-ims
refresh_pattern -i /.js$ 10 50% 60 reload-into-ims
refresh_pattern -i /.xml$ 10 50% 30 reload-into-ims
refresh_pattern的作用:
用于确定一個頁面進入cache後,它在cache中停留的時間。
文法:
幾個概念:
resource age =對象進入cache的時間-對象的last_modified
response age =目前時間-對象進入cache的時間
LM-factor=(response age)/(resource age)
舉個例子,這裡隻考慮percent, 不考慮min 和 max
例如:refresh_pattern 20%
squid上 proxy.aaa.com/index.htm index.htm進入cache的時間 2007-04-10 03:00:00
1)如果目前時間 2007-04-10 03:00:00
resource age =3點-2點=60分鐘
response age =0分鐘
index.htm還可以在cache停留的時間(resource age)*20%=12分鐘
也就是說,index.htm進入cache後,可以停留12分鐘,才被重新确認。
2)如果目前時間 2007-04-10 03:05:00
response age =5分鐘
index.htm還可以在cache停留的時間(resource age)*20%=12分鐘-5=7
LM-factor=5/60=8.3%<20%
一直到2007-04-10 03:12:00 LM-factor=12/60=20% 之後,cache中的頁面index.htm終于stale。
如果這時沒有index.htm的請求,index.htm會一直在緩存中,如果有index.htm請求,squid收到該請求後,由于已經過期,squid會向源伺服器發一個index.htm是否有改變的請求,源伺服器收到後,如果index.htm沒有更新,squid就不用更新緩存,直接把緩存的内容放回給用戶端,同時,重置對象進入cache的時間為與源伺服器确認的時間,比如2007-04-10 03:13:00,如果正好在這個後重新确認了頁面。重置後,resource age變長,相應在cache中存活的時間也變長。
如果有改變則把最新的index.htm傳回給squid,squid收到會更新緩存,然後把新的index.htm傳回給用戶端,同時根據新頁面中的Last_Modified和取頁面的時間,重新計算resource age,進一步計算出存活時間。
實際上,一個頁面進入cache後,他的存活時間就确定了,即 (resource age) * 百分比,一直到被重新确認。
了解了百分比後,min max就好了解了
squid收到一個頁面請求時:
1、計算出response age,
2、如果response age<min 則 fresh 如果response age>max 則 stale
3、如果response age在之間,如果response時間<存活時間,fresh,否則stale