How does PHP empty the varnish cache file?

Source: Internet
Author: User
Tags varnish
    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, $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. ?>

Copy Code

Note that the incoming URL is not parameters, for example: Www.baidu.com/?tn=sougou because the purge.url after emptying the regular expression can be changed to www.baidu.com/(.?) Sougou can be.

When a varnish cache multiple site content needs to clear the specified site URL or simply clear the site home page, you need to use purge instead of purge.url.

2. Code:

    1. function Varnish_purge ($ip, $host = ", $url) {
    2. $errstr = ";
    3. $errno = ";
    4. $fp = Fsockopen ($ip, $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. }
Copy Code
  • Related Article

    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.