You are here

function varnish_purge in Varnish 6

Same name and namespace in other branches
  1. 8 varnish.module \varnish_purge()
  2. 7 varnish.module \varnish_purge()

Helper function to purge items for a host that matches the provided pattern.

Parameters

string $host the host to purge.:

string $pattern the pattern to look for and purge.:

2 calls to varnish_purge()
varnish_expire_cache in ./varnish.module
Implementation of hook_expire_cache
varnish_purge_all_pages in ./varnish.module
Helper function to quickly flush all caches for the current site.

File

./varnish.module, line 208
varnish.module Provide drupal hooks for integration with the Varnish control layer.

Code

function varnish_purge($host, $pattern) {

  // Get the current varnish version, if we are using Varnish 3.x, then we can
  // need to use ban instead of purge.
  $version = floatval(variable_get('varnish_version', 2.1));
  $command = $version >= 3 ? "ban" : "purge";
  $bantype = variable_get('varnish_bantype', VARNISH_DEFAULT_BANTYPE);
  switch ($bantype) {
    case VARNISH_BANTYPE_NORMAL:
      _varnish_terminal_run(array(
        "{$command} req.http.host ~ {$host} && req.url ~ \"{$pattern}\"",
      ));
      break;
    case VARNISH_BANTYPE_BANLURKER:
      _varnish_terminal_run(array(
        "{$command} obj.http.x-host ~ {$host} && obj.http.x-url  ~ \"{$pattern}\"",
      ));
      break;
    default:

      // We really should NEVER get here. Log WATCHDOG_ERROR. I can only see this happening if a user switches between different versions of the module where we remove a ban type.
      watchdog('varnish', 'Varnish ban type is out of range.', array(), WATCHDOG_ERROR);
  }
}