function varnish_purge in Varnish 8
Same name and namespace in other branches
- 6 varnish.module \varnish_purge()
- 7 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.:
string $operator (optional) the operator used to match the pattern:
2 calls to varnish_purge()
- 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() 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.
File
- ./
varnish.module, line 60 - varnish.module
Code
function varnish_purge($host, $pattern, $operator = '~') {
global $base_path, $base_root;
// Validate operator and fallback to default if not valid
if (!in_array($operator, [
'==',
'!==',
'~',
'!~',
'<',
'!<',
'>',
'!>',
])) {
$operator = '~';
}
$config = \Drupal::config('varnish.settings');
$bantype = $config
->get('varnish_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_path)) == $base_path) {
$single_pattern = substr_replace($single_pattern, '', 1, strlen($base_path));
}
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);
switch ($bantype) {
case VARNISH_BANTYPE_NORMAL:
_varnish_terminal_run([
"ban req.http.host ~ {$host} && req.url {$operator} \"{$pattern}\"",
]);
break;
case VARNISH_BANTYPE_BANLURKER:
_varnish_terminal_run([
"ban obj.http.x-host ~ {$host} && obj.http.x-url {$operator} \"{$pattern}\"",
]);
break;
default:
// We really should NEVER get here. Log error. I can only see this
// happening if a user switches between different versions of the
// module where we remove a ban type.
\Drupal::logger('varnish')
->error('Varnish ban type is out of range.');
}
}