Skip to content

Varnish Cache and NextGen Gallery

I won’t go through setting up a varnish cache as others have done much better jobs.  The Varnish plugin for WordPress works really well at its job of purging specific urls from the cache when updated in wordpress and has promised support for NextGen Gallery for ages.  However, where I work, we need that now as updating a thumbnail in Nextgen Gallery does not purge the varnish cache

Our Setup

Varnish cache and wordpress frontend on the same server.

Note:  this is for NExtGen Gallery 1.9.x  Version 2.0+ is not necessarily working with this setup

The (coding) problem

Normally we’d add a filter to an appropriate hook and keep our code nice and free from messing with an author’s plugin.  Unfortunately, an appropriate hook does not exist.  We’re going to have to modify a plugin to get this to work, but let’s keep this as simple as possible

The Solution

Part 1:  [plugins]/nextgen-gallery/lib/ngg-db.php

  1. Find the function ‘update_image_meta’
  2. Make the following modification (highlight in green is what you add)
NextGenGallery_Purge($id);
return $result;
  1. save file, move onto part 2

Part 2:  [your-theme]/functions.php

Add in the below to purge the varnish cache (it’s needed by the above to work)

/* This cannot be hooked into the saving of NextGen Gallery as no hooks exist on saving (pity)  */
class nggGallery_Purge {

    public function __construct($id){
        $this->NextGenGallery_Purge($id);
    }

    /* for PHP 4 */
    public function nggGallery_Purge($id){
        $this->__construct($id);
    }

    private function NextGenGallery_Purge($id){
        // bug out?
        if(!is_numeric($id)){return false;}
        if(!class_exists('nggGallery')){return false;}

        // use standard nextgen calls
        $img_url = nggGallery::get_image_url($id);
        $thumb_url = nggGallery::get_thumbnail_url($id);

        // purge the urls
        $this->varnish_purge($thumb_url);
        $this->varnish_purge($img_url);
    }

    private function varnish_purge($url){
        // bug out if something is wrong
        if(strlen($url)<3){return false;}

        // initialise cUrl
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PURGE');

        // don't send to the screen
         curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);

        // make sure the connection is *never* cached
        curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE);
        curl_setopt($ch,CURLOPT_FORBID_REUSE,TRUE);

        // set the url
        curl_setopt($ch, CURLOPT_URL, $url);
        // make the request
        curl_exec($ch);

        /* make sure we purge with wildcards to account for the query
        strings which are often attached
        in case of VCL's which support this */
        // set the url
        curl_setopt($ch, CURLOPT_URL, $url.'.*');
        // make the request
        curl_exec($ch);

        // we're done here.  Close the setup
        curl_close($c);
    }
}
// keep namespace as clean as possible by using one function and a class
function NextGenGallery_Purge($id){
    $purged = new nggGallery_Purge($id);
}

Part 3: save, test and upload

You do have a development environment, right?  Check it works there and then we’re done!

Notes

We might want to put this into a plugin, but we really hope that the NextGen Gallery Authors include a filter in the next update, which would render the solution we have here complete (we would not need step 1) and then it could be integrated into the varnish purge plugin with one addition to the filters it hooks onto.

Update

  • 25th September – One minor change to the way things were called in curl to make sure we use a non-cached connection and we return absolutely nothing else adding thumbnails will get error 200 listing which will make users think that they have an error despite a success.

Update – Part 2

  • 25th September – $c != $ch.  All variables for Curl use $ch (curl handler)
  • 25th September – purge wildcard urls in case VCLs support it
  • 25th September – added in support for PHP4 diehards
%d bloggers like this: