Home  >  Article  >  Backend Development  >  How to clear varnish cache files in php?

How to clear varnish cache files in php?

WBOY
WBOYOriginal
2016-07-25 08:52:11964browse
  1. function clearVarnish($ip,$url,$host=null){

  2. $errstr = '';

  3. $errno = '';
  4. $varnist_arr = isset($host) ? $host : C('VARNISH_LIST');
  5. foreach ($varnist_arr as $v){
  6. $fp = fsockopen ($ip, 2000, $errno, $errstr, 2);
  7. if (!$fp) {
  8. return false;
  9. } else {
  10. $out = "purge.url $url rn";
  11. fputs ($fp, $out);
  12. $out = fgets($fp , 4096);
  13. fclose ($fp);
  14. return $out;
  15. }
  16. }
  17. }
  18. ?>

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:

  1. function varnish_purge($ip, $host='', $url) {
  2. $errstr = '';
  3. $errno = '';
  4. $fp = fsockopen ($ip, 2000, $errno, $errstr, 2);
  5. if (!$fp) {
  6. return $errno;
  7. }else {
  8. if(!empty($host)){
  9. $out = "purge req.http.host == {$host } && req.url ~ ^/$ rn";
  10. }else{
  11. $out = " purge.url {$url} rn";
  12. }
  13. fputs ($fp, $out);
  14. $out = fgets($ fp , 4096);
  15. fclose ($fp);
  16. return $out;
  17. }
  18. }
Copy code


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