Home >Backend Development >PHP Tutorial >PHP 清空varnish 缓存的详解(包括指定站点下的)_PHP

PHP 清空varnish 缓存的详解(包括指定站点下的)_PHP

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-01 12:06:23927browse

没法清空文件夹内容 只能清空指定链接缓存
复制代码 代码如下:
function clearVarnish($ip,$url,$host=null){

    $errstr = '';
    $errno = '';
    $varnist_arr = isset($host) ? $host : C('VARNISH_LIST');
    foreach ($varnist_arr as $v){
        $fp = fsockopen ($ip, 2000, $errno, $errstr, 2);
        if (!$fp) {
            return false;
        } else {
            $out = "purge.url $url \r\n";
            fputs ($fp, $out);
            $out = fgets($fp , 4096);
            fclose ($fp);
            return $out;
        }
    }
}
?>

需要注意的是:传入进来的 url是不能带参数的  如:www.baidu.com/?tn=sougou
因为 purge.url 后清空的是 正则表达式 可以改成 www.baidu.com/(.?)sougou 这样就可以了。

当一台varnish 缓存多个站点内容需要清除指定站点URL 或者单纯清除 站点首页时 需使用purge 而不是purge.url
复制代码 代码如下:
function varnish_purge($ip, $host='', $url) {
        $errstr = '';
        $errno = '';
        $fp = fsockopen ($ip, 2000, $errno, $errstr, 2);
        if (!$fp) {
                return $errno;
        }else {
              if(!empty($host)){
                 $out = "purge req.http.host == {$host} && req.url ~ ^/$ \r\n";
              }else{
                 $out = " purge.url {$url} \r\n";
              }
              fputs ($fp, $out);
              $out = fgets($fp , 4096);
              fclose ($fp);
              return $out; 
        }
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn