Skip to content

Instantly share code, notes, and snippets.

@pbuyle
Last active April 5, 2017 18:40
Show Gist options
  • Save pbuyle/c2cf59a22e9cd173f4aafa8ae7a0d273 to your computer and use it in GitHub Desktop.
Save pbuyle/c2cf59a22e9cd173f4aafa8ae7a0d273 to your computer and use it in GitHub Desktop.

Revisions

  1. pbuyle revised this gist Dec 9, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions MODULE.module.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    <?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
  2. pbuyle revised this gist Dec 9, 2016. No changes.
  3. pbuyle revised this gist Dec 9, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions MODULE.module.php
    Original file line number Diff line number Diff line change
    @@ -33,6 +33,7 @@ function MODULE_expire_cache($urls, $wildcards, $object_type, $object) {
    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.
  4. pbuyle revised this gist Dec 9, 2016. 1 changed file with 35 additions and 29 deletions.
    64 changes: 35 additions & 29 deletions MODULE.module.php
    Original file line number Diff line number Diff line change
    @@ -1,40 +1,46 @@
    <?php

    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.
    *
    * Will only works for relative URLs in $urls. Be sure to uncheck "Include base URL in expires" in Cache Expiration
    * settings.
    * Be sure to uncheck "Include base URL in expires" in Cache Expiration settings.
    */
    function MODULE_expire_cache($urls, $wildcards, $object_type, $object) {
    if (defined('PANTHEON_ENVIRONMENT') && defined('PANTHEON_SITE_NAME')) {
    foreach ($urls as $original_url) {
    $options = array(
    'method' => 'PURGE',
    // We just want to execute the queue but not wait.
    'blocking' => FALSE,
    );

    // Build the full purged URL
    $url = url($original_url, array('absolute' => TRUE, 'alias' => TRUE));

    // Extract the host from that URL, and set it in the Host header.
    $parsed_url = @parse_url($url);
    $default_port = $parsed_url['scheme'] == 'https' ? 443 : 80;
    $port = isset($parsed_url['port']) ? $parsed_url['port'] : $default_port;
    $options['headers']['Host'] = $parsed_url['host'] . ($port != $default_port ? ':' . $port : '');

    // Build the URL of the purge endpoint
    $purge_endpoint = $parsed_url['scheme'] . '://' . PANTHEON_ENVIRONMENT . '-' . PANTHEON_SITE_NAME . '.pantheonsite.io:' . $default_port;
    $url = url($original_url, array(
    'absolute' => TRUE,
    'alias' => TRUE,
    'base_url' => $purge_endpoint
    ));
    httprl_request(array($url), $options);
    }
    httprl_send_request();
    if (variable_get('expire_include_base_url', EXPIRE_INCLUDE_BASE_URL)) {
    return;
    }
    if (!empty($urls) && function_exists('pantheon_clear_edge_paths')) {
    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);
    }
    }
  5. pbuyle revised this gist Dec 9, 2016. No changes.
  6. pbuyle created this gist Dec 9, 2016.
    40 changes: 40 additions & 0 deletions MODULE.module.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    <?php

    /**
    * Implements hook_expire_cache().
    *
    * Provides integration of Pantheon with the Cache Expiration (expire) module.
    *
    * Will only works for relative URLs in $urls. Be sure to uncheck "Include base URL in expires" in Cache Expiration
    * settings.
    */
    function MODULE_expire_cache($urls, $wildcards, $object_type, $object) {
    if (defined('PANTHEON_ENVIRONMENT') && defined('PANTHEON_SITE_NAME')) {
    foreach ($urls as $original_url) {
    $options = array(
    'method' => 'PURGE',
    // We just want to execute the queue but not wait.
    'blocking' => FALSE,
    );

    // Build the full purged URL
    $url = url($original_url, array('absolute' => TRUE, 'alias' => TRUE));

    // Extract the host from that URL, and set it in the Host header.
    $parsed_url = @parse_url($url);
    $default_port = $parsed_url['scheme'] == 'https' ? 443 : 80;
    $port = isset($parsed_url['port']) ? $parsed_url['port'] : $default_port;
    $options['headers']['Host'] = $parsed_url['host'] . ($port != $default_port ? ':' . $port : '');

    // Build the URL of the purge endpoint
    $purge_endpoint = $parsed_url['scheme'] . '://' . PANTHEON_ENVIRONMENT . '-' . PANTHEON_SITE_NAME . '.pantheonsite.io:' . $default_port;
    $url = url($original_url, array(
    'absolute' => TRUE,
    'alias' => TRUE,
    'base_url' => $purge_endpoint
    ));
    httprl_request(array($url), $options);
    }
    httprl_send_request();
    }
    }