A shell script to refresh a single page in varnish cache

Hot on the heels of the shell script to refresh a predefined set of regularly changing pages in varnish cache, comes the updated version of single page refresh script. The previous version didn't automatically refresh the cache with curl after purging.

Script in action:

ak@loon:~$ v-purge /alain/
Purging old version from cache:
-------------------------------
0x2e112400 1321656227.056981     0 	req.url ~ ^/alain/$

Populating cache with new page content:
---------------------------------------
200 http://giantdorks.org/alain/

Download the script:

#!/bin/bash

cmd="sudo varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret"
site="http://giantdorks.org"
page="$1"

if [ "$page" != "" ]; then
  echo -e "\nPurging old version from cache:"
  echo "-------------------------------"
  $cmd purge.url "^$page\$" | sed '/^$/d'
  $cmd purge.list | head -n 1
  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

2 Comments

  • 1. Alain Kelder is a Giant D&hellip replies at 18th July 2012, 11:43 am :

    […] UPDATE 2011-11-18: Here’s updated code, that immediately refreshes the cache with curl after purging. […]

  • 2. mugnier102 replies at 25th September 2013, 10:06 am :

    You may need to add :
    site=$(echo $1 | sed -r ‘s|https?://||;s|/(.*)||;’)
    page=$(echo $1 | awk -F’/’ -v OFS=’/’ ‘{sub(/https?:\/\//, “”); $1=””; print $0}’)
    To get the ban working with complete url.
    (Code from 2010 page: http://giantdorks.org/alain/exploring-methods-to-purge-varnish-cache/)

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).