Skip to content

Instantly share code, notes, and snippets.

@thewilkybarkid
Last active December 23, 2015 06:39
Show Gist options
  • Save thewilkybarkid/6595681 to your computer and use it in GitHub Desktop.
Save thewilkybarkid/6595681 to your computer and use it in GitHub Desktop.
Patch for the Drupal Varnish module (7.x-1.0-beta3 + https://www.drupal.org/files/issues/varnish_purge_limit-1481136-11_1.patch) to make it site alias aware.
diff --git a/varnish.cache.inc b/varnish.cache.inc
index 7628579..a18a846 100644
--- a/varnish.cache.inc
+++ b/varnish.cache.inc
@@ -71,10 +71,11 @@ class VarnishCache implements DrupalCacheInterface {
varnish_purge_all_pages();
}
else {
- $host = _varnish_get_host();
$base = base_path();
$purge = $cid . '(.*)';
- varnish_purge($host, '^' . $base . $purge . '$');
+ foreach(_varnish_get_hosts() as $host) {
+ varnish_purge($host, '^'.$base.$purge.'$');
+ }
}
}
elseif (is_array($cid)) {
diff --git a/varnish.module b/varnish.module
index f205629..cdc8638 100644
--- a/varnish.module
+++ b/varnish.module
@@ -141,8 +141,9 @@ function varnish_expire_cache($paths) {
}
}
else {
- $host = _varnish_get_host();
- varnish_purge_paths($host, $paths);
+ foreach(_varnish_get_hosts() as $host) {
+ varnish_purge_paths($host, $paths);
+ }
}
}
@@ -151,8 +152,9 @@ function varnish_expire_cache($paths) {
*/
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);
+ }
}
/**
@@ -277,12 +279,42 @@ function theme_varnish_status($status) {
}
/**
- * Helper function to parse the host from the global $base_url
+ * Helper function to get the hosts (pseudonymous subdomains) we need to purge
+ * @return array of hosts
*/
-function _varnish_get_host() {
+function _varnish_get_hosts() {
global $base_url;
+
+ // We need access to Drupal's site aliases. The file /sites/sites.php is included
+ // in drupal bootstrap, but unfortunately its contents are not stored anywhere.
+ if (file_exists(DRUPAL_ROOT . '/sites/sites.php')) {
+ include(DRUPAL_ROOT . '/sites/sites.php');
+ }
+
$parts = parse_url($base_url);
- return $parts['host'];
+ $currentHost = $parts['host'];
+ $hosts = array();
+
+ if (isset($sites) && count($sites)>0) {
+ // Find the other pseudonymous domains. We might be using an alias
+ // or the primary host, so find the primary host.
+ if (array_key_exists($currentHost, $sites)) {
+ // Request used an alias of the primary host
+ $primaryHost = $sites[$currentHost];
+ } else {
+ // we must be using the primary host.
+ $primaryHost = $currentHost;
+ }
+ // add pseudonyms
+ $hosts = array_merge($hosts, array_keys($sites, $primaryHost));
+ // add primary name
+ $hosts[] = $primaryHost;
+ }
+ if (count($hosts)==0) {
+ // ensure current host is added
+ $hosts[] = $currentHost;
+ }
+ return $hosts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment