10Apr/110

Purge squid cache using curl (without squidclient)

Squid Logo Purging cached document from squid can easily be achieved using squidclient. But in some cases where squidclient is not available you can use curl to purge squid cache.

Purging squid cache is relatively simple, as you only need to specify PURGE http method instead of GET or PUT.

Your need to allow your web server   to PURGE data from squid. This is achievable  by specifying  ACL on squid server to allow web server , ie :

acl purgehosts src 192.168.1.10
http_access allow PURGE purgehosts

Bash

curl -X PURGE -x [squid-server:port] [URL to purge] -Is | head -n1

Example :

curl -X PURGE -x http://squid.example.com:80 http://www.example.com/contact -Is | head -n1

PHP

<!--?<span class="hiddenSpellError" pre=""-->php
$proxy="http://squid.example.com:80";
echo clearcache("http://www.example.com/contact",$proxy);

function clearcache($url,$proxy)
{
    if (!function_exists('curl_init')) { die('cURL is not installed!'); }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST , "PURGE");
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);

    if (!curl_exec($ch)) { return false; }
    $output = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return ($output == '200') ? "success": "not cached";
}

?>

Posted by Rianto Wahyudi

Filed under: Linux Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


+ two = 7

No trackbacks yet.