Skip to content

Instantly share code, notes, and snippets.

@pbuyle
Last active April 5, 2017 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pbuyle/c2cf59a22e9cd173f4aafa8ae7a0d273 to your computer and use it in GitHub Desktop.
Save pbuyle/c2cf59a22e9cd173f4aafa8ae7a0d273 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);
}
}
@veronica-severyn
Copy link

I am so lucky I found your solution!! Thanks a lot! It seems to work for my needs!

@dalin-
Copy link

dalin- commented Apr 5, 2017

This is a great start. I took this a bit further, and hopefully we can get this turned into a real module:
https://www.drupal.org/node/2867136

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment