Skip to content

Instantly share code, notes, and snippets.

@jasonfroderman
Forked from pbuyle/MODULE.module.php
Created March 12, 2017 16:25
Show Gist options
  • Save jasonfroderman/e0cefdd21ee2a8979442b30dde5dcefe to your computer and use it in GitHub Desktop.
Save jasonfroderman/e0cefdd21ee2a8979442b30dde5dcefe to your computer and use it in GitHub Desktop.
Integration of Pantheon with the Cache Expiration (expire) module on Drupal
<?php
/**
* Implements hook_init().
*/
function MODULE_init() {
// Add Surrogate-Key headers based on path segments.
// E.g. if the current path is product/some-category/product-name
// we should end up with the following Surrogate-Keys:
// product product/some-category product/some-category/product-name
$path_segments = explode('/', drupal_get_path_alias());
$surrogate_keys = array();
$full_url = '';
foreach ($path_segments as $segment) {
if (empty($surrogate_keys)) {
$full_url = $segment;
}
else {
$full_url .= '/' . $segment;
}
$surrogate_keys[] = $full_url;
}
drupal_add_http_header('Surrogate-Key', implode(' ', $surrogate_keys));
}
/**
* Implements hook_expire_cache().
*
* Provides integration of Pantheon with the Cache Expiration (expire) module.
*
* Be sure to uncheck "Include base URL in expires" in Cache Expiration settings.
*/
function MODULE_expire_cache($urls, $wildcards, $object_type, $object) {
if (variable_get('expire_include_base_url', EXPIRE_INCLUDE_BASE_URL)) {
return;
}
if (!empty($urls) && function_exists('pantheon_clear_edge_paths')) {
$urls = array_map(function($url) {return "/$url";}, $urls);
pantheon_clear_edge_paths($urls);
}
// For wildcards, we use the Surrogate-Key purging functionality.
// Surrogate-Key headers are set in the response based on the
// url path segments.
// @See MODULE_init().
$paths = array_keys($wildcards, TRUE);
if (!empty($paths) && function_exists('pantheon_clear_edge_keys')) {
pantheon_clear_edge_keys($paths);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment