最近蚊子我思考了一下,要是想让我的那个web版清除缓存程序支持对指定文件,对指定域名缓存的清除,那就只能使用c/s模式,那s端的脚本就是关键。
这篇文章只是蚊子下一步工作的一个铺垫,只能算是前期的一个准备工作。在s端,蚊子决定使用perl脚本来完成,所以,这里先写了一个使用perl在清除指定文件的脚本。为了方便其他人使用,特把这个脚本share出来。
脚本设计思路:
1,遍历cache目录,拿到所有cache文件的名字
2,把cache文件内容和指定文件进行查找,取得对应的url地址
3,使用Net::HTTP模块模拟http请求purge掉对应的url
由于这个脚本使用了Net::HTTP模块,所以使用前,请先使用cpan安装此模块
脚本文件名:clear_cache.pl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#! /bin/env perl use File::Find; use Net::HTTP; our @src_file_dir = ("/usr/local/squid/cache"); our $grp_file = $ARGV[0]; find(\&wanted, @src_file_dir); sub wanted { !-d search($File::Find::name); } sub search { my $filename = shift; open FH,"strings $filename |"; while() { chomp; \&purge_cache($_) if (/$grp_file/i and /^http/); } } sub purge_cache { my $url = shift; my $conn = Net::HTTP->;new(Host => "127.0.0.1") or die $@; $conn->write_request(PURGE => $url); my($code, $mess, %h) = $conn->read_response_headers; print $url,":",$code,"\n"; } |
设置:
@src_file_dir= 设置squid缓存目录的路径
用法:
1、清除所有Flash缓存(扩展名.swf):
./clear_cache.pl swf
2、清除URL中包含wenzizone.cn的所有缓存:
./clear_cache.pl wenzizone.cn
注意:
此脚本对squid3.0的cache清除完全正常,2.7没环境未测,理论上讲,nginx的cache清除也可以支持,也未测,等测试后把结果反馈上来再。
这个功能很好