您的位置:首页 > 运维架构 > 反向代理

如何让squid实现动态缓存

2010-12-08 18:27 323 查看
Refresh_pattern 指令间接的控制磁盘缓存。宽松的设置增加了cache的命中率,同样也增加了用户接受过时相应的几率; 保守的设置,降低了cache的命中率和过时响应
Refresh_pattern 规则仅仅应用到没有明确过时期限响应。原始服务器能使用Expires 头部,或者使用Cache-Control:max-age指令来设置过时期限,当然在squid主配置文件中配置refresh_pattern 配置任意数量,squid是按照顺序进行查找以匹配正则表达式,。一旦squid找到一个匹配时,squid会使用相应的值来决定,某个缓存响应是存活还是过期,当正则表达式之一被匹配URI时,squid 就会停止搜索

Refresh_pattern 的语法

Refresh _pattern [-i] regexp min percent max [Option]

一 regexp 参数是大小写敏感的正则表达式,- i 选项是忽略大小写,

二 min 参数是分钟数量,它是过时响应的最低是时间限制。如果魔鬼响应驻留在cache里的时间没有超过这个最低限制,那么它不会过期。同样max 参数是存活响应的最高时间限制。如果某个响应驻留在cache里的时间高于这个最高限制,那么它必须被刷新
三 在min 和max 时间限制之间的响应,会面对squid的最后修改系统LM-factor算法LM-factor=(responseb age)/(resource age). 对这样的响应,squid计算响应的年龄和最后修改系数,然后将它作为百分比值进行比较,响应年龄简单地就是从原始服务器产生,或者是最后一次验证响应后,经历的时间数量。源年龄在Last-Modified 和Date头部之间是不同的,LM-factor 是响应年龄与源年龄的比率。这不是一个精确控制过期的参数,如果要精确控制过期,就不要使用该参数

四 squid的refresh_pattern 算法的简单描述

1 如果响应年龄超过refresh_pattern 的max值,该响应过期;

2 如果LM-factor 少于refresh_pattern 的percent的值。该响应存活

3 如果响应年龄少于refresh_pattern 的min值,该响应存活

4 其他情况,响应过期

五 Refresh_pattern percent 计算方法
Resource age=对象进入cache的时间 – 对象的last_modified

Response age= 当前时间 – 对象进入cache的时间

LM-factor =(response age)/(resource age )

例如 refresh_pattern 20%

假如源服务器上www.aaa.com/index.html - --lastmodified 是2007-04-10 02:00:00

Squid 上的proxy.aaa.com/index.html index.html存入cache的时间2007-04-10 03:00:00
1 如果当前时间 2007-04-10 03:00:00

Resource age =3点 – 2点 =60分钟
Response age =0 分钟
Index.html 还可以在cache 中停留的时间(resource age)*20%= 12 分钟,换句话说, index.html 进入cache后,可以停留十二分钟,才被重新载入
2 如果当前时间是 2007-04-10 03:05:00
Resource age =3点 – 2点 =60 分钟
Response age=5 分钟
Index.html 还可以在cache中停留的时间
( resource age)*20%=12 分钟-5=7分钟
LM-factor=5/60 =8.3% <20%
3 所有说2007-04-10 03:12:00 LM-factor=12/60=20% 之后,cache中的页面index.html 终于stale,如果这段时间没有index.html的请求,index.html会一直缓存中,如果有index.html 请求,squid收到请求后,由于已经过期,squid 会像源服务器发一个index.html是否有改变的请求,如果源服务器收到请求后,如果index.html没有更新,squid就不用缓存,直接会把缓存中的内容给客户端;同时,重置对象进入cache的时间为源服务器确认的时间。比如2007-04-10 03:13:00 ,如果正好在这个后重新确认了页面。重置后,resource age 变长,相应在cache中的cache中存活的时间也同样变长

如果有改变则把最新的index.html返还给squid ,而squid 收到会更新缓存,然后把新的index.html 返还给客户端,同时根据新页面中的Last_Modified 和取页面的时间,重新计算resource age,同样也重新计算存活时间
实际上,一个对象进入cache后,同样他的存活时间就确定了,即(resource age)* percent ,直到被重新确认
六 refresh_pattern 指令
1 Override-expire

该项导致squid在检查Ecpires 头部之前,先检查min 值,这样。一个非零的min时间让squid返回一个未确认的cache命中,及时该响应准备过期
2 Override-lastmod

该选项导致squid在检查LM-factor 百分比之前先检查min值,其生效在expire 之后
3 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 \.bmp$ 1440 90% 129600 reload-into-ims

refresh_pattern -i \.js$ 1440 90% 129600 reload-into-ims

4 Ignore-reload

该选项导致squid的忽略请求里的任何no-cache指令
如果希望内容一旦进入cache就不删除,除非是被主动purge掉为止,可以加上ignore-reload 选项,该项常用在mp3,wma,wmv,gif 之类

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

5 Ignore-no-cache

该项导致squid强制忽略从源站而来的“Pragma:no-cache”和“cache-control:no-cache”
6 Ignore-private

该项导致squid强制忽略从源站而来的“cache-control:private”
7 Ignore-auth

该项导致squid 强制将一个请求认为是源站发送的带有“cache-control:public”
8 Ignore-no-store

该指令是忽略来自源站不缓存对象
a no-store directive from the Web server which makes an object non-cacheable is ignored.

9 Refresh-ims

该项是client一个刷新请求转换成一个If-Modified-Since 请求

a refresh request from a client is converted into an If-Modified-Since request.

注:参考blog
http://www.php-oa.com/2008/01/22/zaisquid26zhongrefrerefresh_patterndeyixielijiehejianyi.html

本文出自 “Frank” 博客,请务必保留此出处http://freehat.blog.51cto.com/1239536/448848
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: