本篇日志应该较早该去写的,一直脱了好久,直到最近才写。在使用任务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的实现。

 

varnish缓存清理的更多相关文章

  1. Varnish缓存服务

    Varnish缓存服务详解及应用实现   1.varnish的基本介绍   Varnish 的作者Poul-Henning Kamp是FreeBSD的内核开发者之一,他认为现在的计算机比起1975年已 ...

  2. 关于varnish缓存

    目录 缓存的概念 一.varnish缓存 1. 简介 2. 总体结构 2.1 两个主进程 2.1.1 Management进程 2.1.2 Child/Cacher进程 2.2 Varnish的日志收 ...

  3. Android Glide数据更新及内存缓存、硬盘缓存清理

    [转] 原文                                         Android Glide数据更新及内存缓存.硬盘缓存清理 Android的Glide在加载图片时候内部默 ...

  4. Hibernate中的脏检查和缓存清理机制

    脏检查 Session到底是如何进行脏检查的呢?当一个Customer对象被加入到Session缓存中时,Session会为Customer对象的值类型的属性复制一份快照.当Session清理缓存时, ...

  5. ecshop缓存清理-限制或禁用ECShop缓存

    ecshop缓存清理-限制或禁用ECShop缓存   ECSHOP的缓存存放在templates/caches/文章夹下,时间长了这个文件夹就会非常庞大,拖慢网站速度.还有很多情况我们不需要他的缓存. ...

  6. 分布式Nginx缓存清理(PHP的socket编程)

    最近,公司要使用康乐的几台自建CDN换成Nginx,在缓存配置上不会有很多的问题,纠结的问题是:Nginx的如何批量进行缓存清理 我们都知道Nginx提供了一个第三方的模块"nginx ng ...

  7. Varnish缓存服务器的搭建配置手册

    Varnish缓存服务器的搭建配置手册 1.Varnish官方环境依赖提示 Installing Varnish Cache is as simple as enabling our package ...

  8. Hibernate——脏检查和缓存清理机制

    Session到底是如何进行脏检查的呢? 当一个Customer对象被加入到Session缓存中时,Session会为Customer对象的值类型的属性复制一份快照.当Session清理缓存时,会先进 ...

  9. Angularjs中的缓存以及缓存清理

    写在最前面:这篇博文是2篇文章组成,详细介绍了Angularjs中的缓存以及缓存清理,文章由上海尚学堂转载过来,欢迎大家阅读和评论.转载请注明出处,谢谢! 一个缓存就是一个组件,它可以透明地储存数据, ...

随机推荐

  1. spark的一些基本概念和模型

    Application application和Hadoop MapReduce类似,都是指用户编写的spark应用程序,其中包含了一个driver功能的代码和分布在集群中多个节点运行的executo ...

  2. iframe中用到的例子

    <view:qrytr>         <view:qrytd colspan="4">     <iframe id="ccbl&quo ...

  3. 震惊,hexo个人博客居然有这么方便的评论系统

    论文搞得一头火,瞎倒腾了一下,没想到几分钟给自己的博客换了个评论系统. 之前用的gitalk,需要依赖github,死活没有成功,而且评论者还需要登录github才可以评论,不好用,刚才偶然间发现va ...

  4. Linux tar: Cannot change ownership to [..]: Permission denied

    tar xzf $INPUT_FOLDER/archive.tar.gz --no-same-owner -C /mnt/test-nas/

  5. springboot前端传参date类型后台处理方式

    转载:https://blog.csdn.net/xiangluer/article/details/81913137?utm_source=copy 参考如下博客: https://www.jian ...

  6. 如何在shell脚本中获取当前用户名?

    答:使用环境变量USER即可 如在脚本中打印当前用户名; #!/bin/sh echo "user name = ${USER}"

  7. Leetcode: Minimum Domino Rotations For Equal Row

    In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino. (A domin ...

  8. jenkins用户和权限管理

    添加用户 进入管理界面,选择manage users,添加用户 将人和角色绑定 用户和项目绑定 角色管理 可以针对不同的站点对不同的人赋权 添加角色并赋权 将角色赋权给对应的用户 使用新添加的这个用户 ...

  9. Python3基础 bool True为1 False为0

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  10. 搭建iscsi存储系统(一)

    (1).DAS.SAN.NAS三种存储方式 参考:https://blog.csdn.net/qq_23348071/article/details/73963407 DAS全称Direct-Atta ...