`
annan211
  • 浏览: 445573 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

nginx + memcached session 同步

 
阅读更多

squid 缓存疑问 问题归纳:
1 squid 缓存的数据,何时过期,如何判断缓存的数据已经过期,如何把最新的数据缓入squid 并且替换掉旧的内容
2 如何判断数据是否应该被缓存
3 校验失败时,是否给出缓存中旧的内容

上面的几个问题  都可以通过 squid中的 refresh_pattern 配置项 找到答案。
答案简单归纳如下
1 何时过期 要根据配置选项
  squid 配置项 refresh_pattern [-i] regex min percent max [options]
  这个配置项中 min 和 max 是针对请求停留在cache中的时间设置的,
  min 参数是分钟数量,它是过时响应的最低时间限制。
如果某个响应驻留在cache里的时间没有超过这个最低限制,
那么它不会过期。同样max 参数是存活响应的最高时间限制。
如果某个响应驻留在cache里的时间高于这个最高限制,那么它必须被刷新

min max 并不是精确控制参数,如果需要精确控制 可以使用 percent
percent 会计算得出一个时间 以首页index.html为例,假设 有效期为 7分钟,
在 3:00缓入squid, 如果在这段有效期内,有改变则把最新的index.html返还给squid ,
而squid 收到会更新缓存,然后把新的index.html 返还给客户端,
同时根据新页面中的Last_Modified 和取页面的时间,重新计算resource age,同样也重新计算存活时间
如果没有改变的话 7分钟过后,首页过期,如果这段时间没有index.html的请求,
index.html会一直缓存中,如果有index.html 请求,squid收到请求后,
由于已经过期,squid 会像源服务器发一个index.html是否有改变的请求,
如果源服务器收到请求后,如果index.html没有更新,squid就不用缓存,
直接会把缓存中的内容给客户端;同时,重置对象进入cache的时间为源服务器确认的时间。

2 一个object的header部分被squid得到之后,是要判断一下,这个object在过期校验层面上,
是否适合被缓存。如果不适合(要校验一个object主要有2种手段,If-Modified-Since和If-None-Match,
一个需要Last-Modified头,一个需要ETag头。这两个头如果都没有的话,object也是不能缓存的。),
将直接不缓存这个object。
如果 过期时间 大于 配置文件中的minimum_expiry_time这个配置项 则会缓存,否则不会缓存。

3 检查当回源校验失败时,是否能将已经过期的object给出去。如果一次回源校验,
原站给出了一个5xx的话,squid为了能让客户端继续拿到内容,可能会将磁盘上的旧内容发给客户端,
但要有一定的条件,即检查object是否已经“严重过期”。
 

squid 配置文件中有 refresh_pattern 配置项

refresh_pattern最主要的作用,过期校验
Squid的过期校验是访问驱动的,如果一个object过期了,却又一直没人访问,
那么squid会一直把这个object扔在那里,而不会主动地回源校验它。
只有当客户端访问到了这个object的时候,squid才会校验。


先介绍一下refresh_pattern的配置方法:
refresh_pattern [-i] regex min percent max [options]
-i表示正则匹配时不区分大小写  - i 选项是忽略大小写,
regex是与url进行匹配的正则表达式
min,percent,max 是3个数字,min, max的单位是分钟,percent就是百分比。
min 参数是分钟数量,它是过时响应的最低时间限制。
如果某个响应驻留在cache里的时间没有超过这个最低限制,
那么它不会过期。同样max 参数是存活响应的最高时间限制。
如果某个响应驻留在cache里的时间高于这个最高限制,那么它必须被刷新
options是refresh_pattern的其他选项,包括以下几种
override-expire
override-lastmod
reload-into-ims
ignore-reload
ignore-no-cache
ignore-private
ignore-auth
stale-while-revalidate=NN
ignore-stale-while-revalidate
max-stale=NN
negative-ttl=NN


refresh_pattern  算法解析如下

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 )


Date一般是Squid从后面取页面的时间,Last-Modified 一般是页面生成时间。
(当前时间定义为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就不删除,直到被主动purge掉为止,可以加上ignore-reload选项
该项常用在mp3,wma,wmv,gif 之类
一般情况可以使用 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 的配置项
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 ,
直到被重新确认











分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics