五、管理varnish缓存内容
Varnish的一个显著优点是可以灵活的管理缓存内容,而管理缓存主要的工作是如何迅速有效的控制和清除指定的缓存内容,varnish清除缓存相对比较复杂,不过幸运的是,可以通过varnish的管理端口发送PURGE指令来清除不需要的缓存。
下面列出了清除缓存内容的命令格式:
/usr/local/varnish/bin/varnishadm -T 192.168.12.246:3500 purge.url <regexp>
下面的命令可以列出最近清除的详细URL列表:
/usr/local/varnish/bin/varnishadm -T 192.168.12.246:3500 purge.list
举例如下:
(1)如果要清除http://www.example.com/a/2010.html的URL地址,可以执行如下命令:
/usr/local/varnish/bin/varnishadm -T 192.168.12.246:3500 purge.url /a/2010.html
(2)批量清除类似http://www.example.com/a/b/s*.html的URL地址,可执行如下命令:
/usr/local/varnish/bin/varnishadm -T 192.168.12.246:3500 purge.url ^/a/b/s.*$
(3)批量清除类似http://www.example.com/a/b/*.html的URL地址,可执行如下命令:
/usr/local/varnish/bin/varnishadm -T 192.168.12.246:3500 purge.url ^/a/b.*$
(4)如果要清除所有缓存,可执行如下命令:
/usr/local/varnish/bin/varnishadm -T 192.168.12.246:3500 purge.url ^.*$
(5)查看最近清除的详细URL列表
[root@varnish-server ~]# /usr/local/varnish/bin/varnishadm -T 192.168.12.246:3500 purge.list
0x2dc310c0 1278674980.497631 0 req.url ~ /zm/a/web/2010/0423/64.html
0x2dc31100 1278674964.851327 1 req.url ~ ^/zm/a/d.*$
除了通过linux命令行方式清理varnish缓存外,还可以通过telnet到管理端口的方式来清理缓存页面,例如:
- [root@varnish-server ~]#telnet 192.168.12.246 3500
- Trying 192.168.12.246...
- Connected to localhost.localdomain (192.168.12.246).
- Escape character is '^]'.
- 200 154
- -----------------------------
- Varnish HTTP accelerator CLI.
- -----------------------------
- Type 'help' for command list.
- Type 'quit' to close CLI session.
- purge.url /a/mz/2010/0421/11.html #这里是清除这个页面缓存
- 200 0
- purge.url ^/zm/a/d.*$ #这里是清除/zm/a/d/目录下所有以字母d开头的缓存页面
- 200 0
对于系统管理人员或者运维人员来说,时刻了解varnish命中率是至关重要的,虽然varnish给出了很详细的统计数据,但是统计的数据不是很直观,并且必须登录到varnish服务器才能查看,下面给出一个php程序,可以随时随地,并且非常清晰的了解varnish系统的命中率相关情况,程序如下:
- <?php
- // This is just a code snippet written by Jason "Foxdie" Gaunt, its not meant to be executed, it may work as-is, it may not.
- // I freely acknowledge this code is unoptimised but it has worked in practice for 6 months :)
- // Lets define our connection details
- $adminHost = "127.0.0.1"; // varnish服务器的IP地址
- $adminPort = "3500"; // varnish服务器管理端口
- // pollServer(str) - this function connects to the management port, sends the command and returns the results, or an error on failure
- function pollServer($command) {
- global $adminHost, $adminPort;
- $socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname("tcp"));
- if ((!socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, Array("sec" => "5", "usec" => "0"))) OR (!socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, Array("sec" => "5", "usec" => "0")))) {
- die("Unable to set socket timeout");
- }
- if (@socket_connect($socket, $adminHost, $adminPort)) {
- $data = "";
- if (!$socket) {
- die("Unable to open socket to " . $server . ":" . $port . "\n");
- }
- socket_write($socket, $command . "\n");
- socket_recv($socket, $buffer, 65536, 0);
- $data .= $buffer;
- socket_close($socket);
- return $data;
- }
- else {
- return "Unable to connect: " . socket_strerror(socket_last_error()) . "\n";
- }
- }
- // byteReduce(str) - this function converts a numeric value of bytes to a human readable format and returns the result
- function byteReduce($bytes) {
- // Terabytes
- if ($bytes > 1099511627776) {
- return round($bytes / 1099511627776) . "TB";
- }
- else if ($bytes > 1073741824) {
- return round($bytes / 1073741824) . "GB";
- }
- else if ($bytes > 1048576) {
- return round($bytes / 1048576) . "MB";
- }
- else if ($bytes > 1024) {
- return round($bytes / 1024) . "KB";
- }
- else {
- return $bytes . "B";
- }
- }
- // This is where our main code starts
- echo "<div class=\"inner\"><br />Statistics since last reset:<ul>";
- $stats = pollServer("stats");
- if (substr($stats, 0, 3) == "200") { // If request was legitimate
- // Clear all excessive white space and split by lines
- $stats = preg_replace("/ {2,}/", "|", $stats);
- $stats = preg_replace("/\n\|/", "\n", $stats);
- $statsArray = explode("\n", $stats);
- // Removes the first call return value and splits by pipe
- array_shift($statsArray);
- $statistics = array();
- foreach ($statsArray as $stat) {
- @$statVal = explode("|", $stat);
- @$statistics[$statVal[1]] = $statVal[0];
- }
- unset($stats, $statsArray, $stat, $statVal);
- // Start outputting statistics
- echo "<li>" . $statistics["Client connections accepted"] . " clients served over " . $statistics["Client requests received"] . " requests";
- echo "<li>" . round(($statistics["Cache hits"] / $statistics["Client requests received"]) * 100) . "% of requests served from cache";
- echo "<li>" . byteReduce($statistics["Total header bytes"] + $statistics["Total body bytes"]) . " served (" . byteReduce($statistics["Total header bytes"]) . " headers, " . byteReduce($statistics["Total body bytes"]) . " content)";
- // The following line is commented out because it only works when using file storage, I switched to malloc and this broke
- // echo "<li>" . byteReduce($statistics["bytes allocated"]) . " out of " . byteReduce($statistics["bytes allocated"] + $statistics["bytes free"]) . " used (" . round(($statistics["bytes allocated"] / ($statistics["bytes allocated"] + $statistics["bytes free"])) * 100) . "% usage)";
- }
- else {
- echo "Unable to get stats, error was: \"" . $stats;
- }
- echo "</ul></div>";
- ?>
将此php程序放到varnish服务器上,即可统计处当前varnish的命中率以及缓存状态,统计结果类似于下面的一个输出:
- Statistics since last reset:
- 63543 clients served over 584435 requests
- 98% of requests served from cache
- 4GB served (246MB headers, 4GB content)
在这个输出中,清晰的列出了浏览器请求总数、缓存命中率、缓存区中所有缓存内容的HTTP头信息长度和缓存内容的正文长度。根据这个结果判断,varnish的缓存效果还是很不错的,命中率很高。
本文出自 “dsafsa_技术博客” 博客,转载请与作者联系!