PHP clear varnish cache details (including at the specified site) _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags varnish
PHP clears the varnish cache (including files on the specified site ). You cannot clear the folder content. you can only clear the specified link cache. copy the code as follows :? PhpfunctionclearVarnish ($ ip, $ url, $ hostnull) {$ errstr; $ errno; $ varnist_arrisse cannot clear folder content but can only clear the specified link cache

The code is as follows:


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.

The 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;
}
}


The following code can only be used to clear the specified link cache :? Php function clearVarnish ($ ip, $ url, $ host = null) {$ errstr = ''; $ errno =''; $ varnist_arr = isse...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.