zoukankan      html  css  js  c++  java
  • varnish缓存清理

    本篇日志应该较早该去写的,一直脱了好久,直到最近才写。在使用任务cache工具时,都会提到的一个问题。如何只清理想清理的那部分缓存,而其已缓存的部分不受影响 。这里就要用到varnishadm工具,先看下其用法:

    root@cache-40.sangame.com:[/root]/App/varnish/bin/varnishadm help
    help [command]
    ping [timestamp]
    auth response
    quit
    banner
    status
    start
    stop
    vcl.load <configname> <filename>
    vcl.inline <configname> <quoted_VCLstring>
    vcl.use <configname>
    vcl.discard <configname>
    vcl.list
    vcl.show <configname>
    param.show [-l] [<param>]
    param.set <param> <value>
    panic.show
    panic.clear
    storage.list
    backend.list
    backend.set_health matcher state
    ban.url <regexp>
    ban <field> <operator> <arg> [&& <field> <oper> <arg>]...
    ban.list

    一、ban相关的用法

    缓存清理部分主要使用的是ban命令,在一些老的varnish版本里是purge命令。varnishadm ban相关的处理命令非常强大,支持正则和不同的域名进行区分,还支持按文件大小进行处理。下面举一些例子:

    1、最简单的用法ban.url 

    root@cache-40.sangame.com:[/root]/App/varnish/bin/varnishadm -T 127.0.0.1:2000 ban.url /download/

    清理所有域名下download下的缓存。

    2、匹配域名和url正则

    root@cache-40.sangame.com:[/root]/App/varnish/bin/varnishadm -T 127.0.0.1:2000  ban req.http.host == "example.com" && req.url ~ ".png$"

    以上是清理example.com域名下所有png文件的缓存。

    3、根据大小进行处理

    varnishadm -T 127.0.0.1:2000 req.url !~ ".ogg$" && obj.size > 10MB

    以上是清理所有大于10MB的ogg文件。

    4、加cookile参数的清理

    req.http.host ~ "^(?i)(www.)example.com$" && obj.http.set-cookie ~ "USERID=1663"

    这里是处理无论是www.example.com还是example.com下的cookile值USERID=1663的所有缓存 。

    具体的写法可以参看VCL语法,只要符合VCL语法的都可以通过ban使用。所有的正则含义如下:

    A ban expression consists of one or more conditions. A condition consists of a field, an operator, and an argument. Conditions can be ANDed together with "&&".
    A field can be any of the variables from VCL, for instance req.url, req.http.host or obj.http.set-cookie.
    Operators are "==" for direct comparision, "~" for a regular expression match, and ">" or "<" for size comparisons. Prepending an operator with "!" negates the expression.
    The argument could be a quoted string, a regexp, or an integer. Integers can have "KB", "MB", "GB" or "TB" appended for size related fields.

    具体可以参看官网上的相关文档:

    https://www.varnish-cache.org/docs/3.0/reference/varnish-cli.html

    https://www.varnish-cache.org/docs/3.0/tutorial/purging.html

    二、查看ban过的规则列表

    可以使用ban.list查看已经ban过的规则列表:

    root@cache-40:[/root]/App/varnish/bin/varnishadm ban.list
    Present bans:
    1384427961.641222     0         req.url ~ /download/
    1384415727.496078     4G        req.url ~ /download/
    1384412783.261184     1         req.url ~ /android/
    1384412640.295176     0G        req.url ~ /download/

    其中上面提到的G不是代表数据量大小的Gbit ,而是gone的意思,代表已经长时间无效或已经变成过去式的数据。被标记G代表是重复ban ,之所以标记是出于优化的目的。

    三、强制无效

    将req.hash_always_miss的值设为true ,将会将当前的缓存失效(但不会从历史中清除),而直接从后端拿新鲜的数据对象缓存,覆盖当前数据。而旧的缓存对象需要等到TTL过期或其他方法清除。

    四、远程处理

    varnishadm还可以通过telnet的方法处理,不过需要在vcl文件里事先指定允许的IP 。具体操作方法是“telnet varnish的IP varnishadm的端口”,如telnet 192.168.55.100  2000,进入使用的命令和以上说明一直,如:上面到的是varnishadm   ban.list,在这里就直接输入ban.list就行了。另外需要注意的是在vcl规则中指定ban的IP时,重启加载配置原配置或重启varnish经常会报错,如下:

    acl purge {
              "localhost";
              "192.168.55.0"/24;
    }
    增加后,再启动报错
    varnish启动遇到的一个问题
    Message from VCC-compiler:
    Unused acl local, defined:
    ('input' Line 12 Pos 5)
    acl local {
    ----#####--
    Running VCC-compiler failed, exit 1
    VCL compilation failed

    出现该错的原因是因为在sub vcl_recv 、sub vcl_hit、sub vcl_miss 缺少相关的配置。具体可以参考:

    https://www.varnish-cache.org/docs/3.0/tutorial/purging.html#http-purges

    按以上示例增加相关部分后,再加载配置文件就正常了。

    以上是基础,类似通过php或客户端等进行cache处理的都是在此基础上进行的增强,最终调用的一般还是varnishadm或telnet varnishadm的实现。

     
  • 相关阅读:
    [1.2]由UML模型通过XMI生成XML,通过XSLT展示到表现层
    [1.1]XMI 与UML结合开发企业应用中业务模型
    如何创建脱机数据库应用程序思路
    SAML在无线网络传输的应用[对照文]
    SoberGGG对针式PKM的初次测评
    [转]美国知名天使投资人列出愿意投资的30大创意方向
    针式PKM适合哪些用户使用?
    没有个人知识管理就是觉得学了很多,却不得记到底学到了什么。
    [转]人之患在好为人师
    [转]一位中国的黑客的一封信!
  • 原文地址:https://www.cnblogs.com/xzlive/p/12022647.html
Copyright © 2011-2022 走看看