Difference between revisions of "Script to Clear a Page from Varnish Cache"

From Brian Nelson Ramblings
Jump to: navigation, search
(How to use the script)
(How to use the script)
 
(2 intermediate revisions by the same user not shown)
Line 6: Line 6:
 
   
 
   
 
  cmd="varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret"
 
  cmd="varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret"
  site="http://www.briansnelson.com
+
  site="http://www.briansnelson.com"
 
  page="$1"
 
  page="$1"
 
    
 
    
Line 12: Line 12:
 
   echo -e "\nPurging old version from cache:"
 
   echo -e "\nPurging old version from cache:"
 
   echo "-------------------------------"
 
   echo "-------------------------------"
   $cmd ban.url "^$page\$" | sed '/^$/d'
+
   $cmd ban.url "$page$" | sed '/^$/d'
 
   $cmd ban.list | head -n 2
 
   $cmd ban.list | head -n 2
 
   echo -e "\nPopulating cache with new page content:"
 
   echo -e "\nPopulating cache with new page content:"
Line 29: Line 29:
 
  bash recache-varnish.script <page to be purged/ban>
 
  bash recache-varnish.script <page to be purged/ban>
  
  bash recache-varnish.script Main_Page
+
To clear this page from the varnish cache you would run:
 +
 
 +
  bash recache-varnish.script Script_to_Clear_a_Page_from_Varnish_Cache
  
 
Or you can download the  [http://www.briansnelson.com/stuff/recache-varnish.script script here]
 
Or you can download the  [http://www.briansnelson.com/stuff/recache-varnish.script script here]

Latest revision as of 23:21, 22 April 2014

Script to Clear a Page from Varnish Cache

The script below will purge/ban a page from the varnish cache and then re-cache the page by curling it

#!/bin/bash

cmd="varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret"
site="http://www.briansnelson.com"
page="$1"
 
if [ "$page" != "" ]; then
  echo -e "\nPurging old version from cache:"
  echo "-------------------------------"
  $cmd ban.url "$page$" | sed '/^$/d'
  $cmd ban.list | head -n 2
  echo -e "\nPopulating cache with new page content:"
  echo "---------------------------------------"
  curl -sL -w "%{http_code} %{url_effective}\n" $site/$page -o /dev/null
else
  echo Error: please provide a URL to purge..
fi

Once you have it download/setup on your system you will want to give the execute permission

chmod +x recache-varnish.script

How to use the script

bash recache-varnish.script <page to be purged/ban>

To clear this page from the varnish cache you would run:

bash recache-varnish.script Script_to_Clear_a_Page_from_Varnish_Cache

Or you can download the script here

You should just need to change the domain name

Additional Articles