-
-
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 rn";
- fputs ($fp, $out);
- $out = fgets($fp , 4096);
- fclose ($fp);
- return $out;
- }
- }
- }
- ?>
-
Copy code
Note that the incoming url cannot be Parameters, for example: www.baidu.com/?tn=sougou
Because the regular expression cleared after purge.url can be changed to www.baidu.com/(.?)sougou.
When a Varnish caches the content of multiple sites and needs to clear the specified site URL or simply clear the site homepage, purge must be used instead of purge.url.
2. Code:
-
- 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 ~ ^/$ rn";
- }else{
- $out = " purge.url {$url} rn";
- }
- fputs ($fp, $out);
- $out = fgets($ fp , 4096);
- fclose ($fp);
- return $out;
- }
- }
Copy code
|