Folder content cannot be cleared. Only the specified link cache can be cleared.
Copy codeThe Code is as follows:
<? Php
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;
}
}
}
?>
Note that:The entered url does not contain parameters such as www.baidu.com /? Tn = sougou
Because after purge. url is cleared, the regular expression can be changed to www.baidu.com /(.?) Sougou.
When one varnish caches multiple sites, you need to clear the specified site URL or simply clear the site homepage, you need to use purge instead of purge. url.
Copy codeThe Code is as follows:
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;
}
}