How does php clear varnish cached files?

Source: Internet
Author: User
How does php clear varnish cached files?

  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 \ r \ n ";
  11. Fputs ($ fp, $ out );
  12. $ Out = fgets ($ fp, 4096 );
  13. Fclose ($ fp );
  14. Return $ out;
  15. }
  16. }
  17. }
  18. ?>

Note that the entered url cannot contain parameters, for example, 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 must clear the specified site URL or simply clear the site homepage, use purge 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 ~ ^/$ \ R \ n ";
  10. } Else {
  11. $ Out = "purge. url {$ url} \ r \ n ";
  12. }
  13. Fputs ($ fp, $ out );
  14. $ Out = fgets ($ fp, 4096 );
  15. Fclose ($ fp );
  16. Return $ out;
  17. }
  18. }

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.