refresh_pattern的作用:
用於確定一個頁面進入cache後,它在cache中停留的時間。refresh_pattern規則僅僅應用到沒有明確過時期限的響應。原始伺服器能使用Expires頭部,或者Cache-Control:max-age指令來指定過時期限。
文法:refresh_pattern [-i] regexp min percent max [options]
min參數是分鐘數量。它是過時響應的最低時間限制。如果某個響應駐留在cache裡的時間沒有超過這個最低限制,那麼它不會到期。類似的,max參數是存活響應的最高時間限制。如果某個響應駐留在cache裡的時間高於這個最高限制,那麼它必須被重新整理。
在最低和最高時間限制之間的響應,會面對squid的最後修改係數LM-factor演算法LM-factor=(response age)/(resource age)。對這樣的響應,squid計算響應的年齡和最後修改係數,然後將它作為百分比值進行比較。響應年齡簡單的就是從原始伺服器產生,或最後一次驗證響應後,經曆的時間數量。源年齡在Last-Modified和Date頭部之間是不同的。LM-factor是響應年齡與源年齡的比率。
常用的幾個參數的意思
override-expire
該選項導致squid在檢查Expires頭部之前,先檢查min值。這樣,一個非零的min時間讓squid返回一個未確認的cache命中,即使該響應準備到期。
override-lastmod
改選項導致squid在檢查LM-factor百分比之前先檢查min值。
reload-into-ims
該選項讓squid在確認請求裡,以no-cache指令傳送一個請求。換句話說,squid在轉寄請求之前,對該請求增加一個If-Modified- Since頭部。注意這點僅僅在目標有Last-Modified時間戳記時才能工作。外面進來的請求保留no-cache指令,以便它到達原始伺服器。
一般情況可以使用 reload-into-ims。它其實是強行控制對象的逾時時間,這違反了http協議的精神,但是在頻寬較窄的場合,可以提高明顯系統相應時間。
舉例:
refresh_pattern -i \.css$ 1440 50% 129600 reload-into-ims
refresh_pattern -i \.xml$ 1440 50% 129600 reload-into-ims
refresh_pattern -i \.html$ 1440 90% 129600 reload-into-ims-
refresh_pattern -i \.shtml$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.hml$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.jpg$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.png$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.gif$ 1440 90% 129600 ignore-reload
refresh_pattern -i \.bmp$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.js$ 1440 90% 129600 reload-into-ims
ignore-reload
該選項導致squid忽略請求裡的任何no-cache指令。
所以。如果希望內容一進入cache就不刪除,直到被主動purge掉為止,可以加上ignore-reload選項,這個我們常用在mp3,wma,wmv,gif之類。
Examples:
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
resource age =對象進入cache的時間-對象的last_modified
response age =目前時間-對象進入cache的時間
LM-factor=(response age)/(resource age)
舉個例子,這裡只考慮percent, 不考慮min 和max
例如:refresh_pattern 20%
假設原始伺服器上www.aaa.com/index.htm -----lastmodified 是 2007-04-10 02:00:00
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
resource age =3點-2點=60分鐘
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) * 百分比,一直到被重新確認。
以上就介紹了urlpattern 對squid中refresh_pattern的一些理解和建議,包括了urlpattern方面的內容,希望對PHP教程有興趣的朋友有所協助。