You are here

function varnish_purge in Varnish 7

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

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

Take care to limit the length of $pattern to params.cli_buffer on your Varnish server, otherwise Varnish will truncate the command. Use varnish_purge_paths() to protect you from this, if applicable.

Parameters

string $host: The host to purge.

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

3 calls to varnish_purge()
VarnishCache::clear in ./varnish.cache.inc
Expires data from the cache.
varnish_purge_all_pages in ./varnish.module
Helper function to quickly flush all caches for the current site.
varnish_purge_paths in ./varnish.module
Helper function that wraps around varnish_purge().

File

./varnish.module, line 157
Common functions used for the module.

Code

function varnish_purge($host, $pattern) {
  global $base_path, $base_root;

  // 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);

  // Modify the patterns to remove base url and base path.
  $patterns = explode('|', $pattern);
  foreach ($patterns as $num => $single_pattern) {
    if (substr($single_pattern, 1, strlen($base_root)) == $base_root) {
      $single_pattern = substr_replace($single_pattern, '', 1, strlen($base_root));
    }
    $patterns[$num] = $single_pattern;
  }
  $pattern = implode('|', $patterns);

  // Get list of domains to ban
  $front_domains = variable_get('varnish_front_domains', '');

  // Trigger hook_varnish_front_domains_alter().
  drupal_alter('varnish_front_domains', $front_domains);
  $fronts = explode(' ', $front_domains);
  switch ($bantype) {
    case VARNISH_BANTYPE_NORMAL:
      _varnish_terminal_run(array(
        "{$command} req.http.host ~ {$host} && req.url ~ \"{$pattern}\"",
      ));
      if ($front_domains) {
        foreach ($fronts as $value) {
          _varnish_terminal_run(array(
            "{$command} req.http.host ~ {$value} && req.url ~ \"{$pattern}\"",
          ));
        }
      }
      break;
    case VARNISH_BANTYPE_BANLURKER:
      _varnish_terminal_run(array(
        "{$command} obj.http.x-host ~ {$host} && obj.http.x-url  ~ \"{$pattern}\"",
      ));
      if ($front_domains) {
        foreach ($fronts as $value) {
          _varnish_terminal_run(array(
            "{$command} obj.http.x-host ~ {$value} && 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);
  }
}