diff --git a/varnish.admin.inc b/varnish.admin.inc index 515057a..99505d7 100644 --- a/varnish.admin.inc +++ b/varnish.admin.inc @@ -114,6 +114,25 @@ function varnish_admin_settings_form() { ), ); + // Allow users to specify specific domains to use. + $form['varnish_hosts'] = array( + '#type' => 'fieldset', + '#title' => t('Hosts'), + '#collapsible' => TRUE, + ); + $form['varnish_hosts']['varnish_hosts_use_baseurl'] = array( + '#type' => 'checkbox', + '#title' => t('Always include the current host'), + '#description' => t('Allow the Varnish module to always include the current host (parsed from the $base_url of the current request). Uncheck this to strictly control the hosts to be used.'), + '#default_value' => variable_get('varnish_hosts_use_baseurl', TRUE), + ); + $form['varnish_hosts']['varnish_hosts_list'] = array( + '#type' => 'textarea', + '#title' => t('Hosts list'), + '#description' => t('Specify each host (domain) that is associated with this site. One host per line.'), + '#default_value' => variable_get('varnish_hosts_list', ''), + ); + // Check status. $form['varnish_stats'] = array( '#type' => 'item', diff --git a/varnish.cache.inc b/varnish.cache.inc index 3d30bf3..6b5a404 100644 --- a/varnish.cache.inc +++ b/varnish.cache.inc @@ -71,10 +71,12 @@ public function clear($cid = NULL, $wildcard = FALSE) { varnish_purge_all_pages(); } else { - $host = _varnish_get_host(); $base = base_path(); $purge = $cid . '(.*)'; - varnish_purge($host, '^' . $base . $purge . '$'); + // Purge for all selected hosts. + foreach (_varnish_get_hosts() as $host) { + varnish_purge($host, '^' . $base . $purge . '$'); + } } } elseif (is_array($cid)) { diff --git a/varnish.module b/varnish.module index 925ca06..dfa017d 100644 --- a/varnish.module +++ b/varnish.module @@ -126,8 +126,9 @@ function varnish_expire_cache($paths, $wildcards = array()) { } } else { - $host = _varnish_get_host(); - varnish_purge_paths($host, $paths); + foreach (_varnish_get_hosts() as $host) { + varnish_purge_paths($host, $paths); + } } } @@ -136,8 +137,9 @@ function varnish_expire_cache($paths, $wildcards = array()) { */ function varnish_purge_all_pages() { $path = base_path(); - $host = _varnish_get_host(); - varnish_purge($host, $path); + foreach (_varnish_get_hosts() as $host) { + varnish_purge($host, $path); + } } /** @@ -312,6 +314,30 @@ function _varnish_get_host() { return $parts['host']; } +/** + * Helper function to get all hosts associated with this site. Invokes + * hook_varnish_hosts_alter() to fetch the list. + */ +function _varnish_get_hosts() { + $hosts = array(); + + // Include the host from the global $base_url, unless disabled. + if (variable_get('varnish_hosts_use_baseurl', TRUE)) { + $hosts[] = _varnish_get_host(); + } + + // Include additional hosts specified in the settings. + if ($hosts_list = variable_get('varnish_hosts_list', '')) { + foreach (explode("\n", trim($hosts_list)) as $host) { + $hosts[] = trim($host); + } + } + + // Allow the varnish_hosts array to be altered. + drupal_alter('varnish_hosts', $hosts); + + return array_unique($hosts); +} /** * Helper function that sends commands to Varnish.