You are here

function varnish_purge_paths in Varnish 8

Same name and namespace in other branches
  1. 7 varnish.module \varnish_purge_paths()

Helper function that wraps around varnish_purge() and compiles a regular expression of all paths supplied to it. This function takes care to chunk commands into no more than 7500 bytes each, to avoid hitting params.cli_buffer.

Parameters

string $host The host to purge.:

array $paths The paths (no leading slash) to purge for this host.:

File

./varnish.module, line 109
varnish.module

Code

function varnish_purge_paths($host, $paths) {
  $config = \Drupal::config('varnish.settings');

  // Subtract the hostname length from the global length limit.
  // Note we use strlen() because we're counting bytes, not characters.
  $length_limit = $config
    ->get('varnish_cmdlength_limit') - strlen($host);
  $base_path = base_path();
  while (!empty($paths)) {

    // Construct patterns and send them to the server when they're full.
    $purge_pattern = '^';
    while (strlen($purge_pattern) < $length_limit && !empty($paths)) {
      $purge_pattern .= $base_path . array_shift($paths) . '$|^';
    }

    // Chop the final "|^" off the string, leaving "$".
    $purge_pattern = substr($purge_pattern, 0, -2);

    // Remove extra slashes from beginning of URL
    $purge_pattern = preg_replace('#/+#', '/', $purge_pattern);

    // Submit this purge chunk.
    varnish_purge($host, $purge_pattern);
  }
}