Squid refresh_pattern 相关
作者: 曲文庆 日期: 2008-10-16 17:07
Basically a cached object is:
FRESH | if expires < now, else STALE |
STALE | if age > max |
FRESH | if lm-factor < percent, else STALE |
FRESH | if age < min |
else | STALE |
The refresh_pattern lines are checked in the order listed here. The first entry which matches is used. If none of the entries match, then the default will be used.
上述是官方解释,对于某些方面还是很笼统的,例如percent、lm-factor等等
You have to follow the pseudocode through until you get a
STALE/FRESH result, and then stop. If you can compute an lm-factor,
then the "age < min" line can't be reached.
Objects without an LM time cannot be refreshed, they have to be
refetched; without a min setting such objects would not be cached at
all.
There are basically three classes of object:
1. with explicit expiry information (max/min/percent are ignored)
2. with LM, but no expiry - governed by percent and max.
3 with neither - governed by min
( Note that all of the above assumes that no refresh rule override
options have been set.)
请注意这张图的起始时间坐标:Last-Modified,这个是由squid读取的原始web数据所规定的。
然后就是Date,这个是原始数据进入squid的缓冲的时间。
最后就是Expires,这个就是原始数据在squid中的缓冲过期时间。
可以很容易的得出结论,对于LM-factor算法来说,原始数据在squid中的缓冲时间为
(原始数据进入squid的缓冲的时间-原始web数据所规定的Last-Modified时间)*percent
所以,我们可以郑重得出结论,在squid的refresh_pattern设置中,percent与Min、Max两个值是完全没有关系!
本人注:squid的过期,由percent or Min,Max来决定.看清哦。是or.是由这二个其中一个那个先到期,那个就先生效。后面的那个不会生效.
resource age : LM = abs(Last-Modified Time - Date) in headers.
LM factor = (Current_Date-Date)/LM
LM就是页面Header里时间(Date)和Last-Modified时间的差。Date一般是Squid从后面取页面的时间,Last-Modified 一般是页面生成时间。
resource age(LM) =对象进入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) * 百分比,一直到被重新确认。
根据上面的解释,或许大体明白了refesh_pattern是如何工作的,但是细心的朋友不难发现,上述中没有说明关于header中不包含last_modified这项的处理方法,通过查看资料获悉,那些没有Last-Modified或Expires头部的响应。squid默认不cache这样的响应。然而,在refresh_pattern规则里使用非零的最小时间,会让squid去cache这些响应,并在该数量的时间范围内,将响应作为cache命中处理。超过MIN时间的,做为STALE状态处理。