You are here

ape.module in Advanced Page Expiration 8

Same filename and directory in other branches
  1. 7 ape.module

Allows finer control of the Cache Control header.

File

ape.module
View source
<?php

use Drupal\Core\Form\FormStateInterface;

/**
 * @file
 * Allows finer control of the Cache Control header.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function ape_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'ape.admin':
      return '<p>' . t('Configure the max age header based on path and other criteria.') . '</p>';
    case 'help.page.ape':
      $output = '';
      $output .= '<p>' . t('The advanced page expiration module is a lightweight solution to allow the max age header (used by external caching mechanisms such as Varnish) to be set based on different criteria, such as path or redirect code. This allows for more advanced scenarios such as having the homepage expire every five minutes while the rest of the site expires in one hour.') . '</p>';
      $output .= '<p>' . t('The <a href="@settings">settings page</a> explains the different options available. There is also Rules integration and an alter hook available for more complex integrations.', array(
        '@settings' => Url::fromRoute('ape.admin')
          ->toString(),
      )) . '</p>';
      return $output;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * * Remove page_cache_maximum_age selection from main performance page and
 * instead direct users to this module's config page. Also do a little cleanup
 * to clarify external caching.
 */
function ape_form_system_performance_settings_alter(&$form, FormStateInterface $form_state) {
  $form['caching']['page_cache_maximum_age']['#access'] = FALSE;
  $form['caching']['#description'] = Link::fromTextAndUrl(t('Configure page caching strategy with APE.'), Url::fromRoute('ape.admin'))
    ->toString();
}

/**
 * Statically cache an expiration length.
 *
 * @param int $new_age
 *   The new value for age.
 *
 * @return int
 *   The resulting max age.
 */
function ape_cache_set($new_age = NULL) {
  $max_age =& drupal_static(__FUNCTION__);
  if (!isset($max_age)) {
    $max_age = NULL;
  }
  if (is_null($max_age) && !is_null($new_age)) {
    $max_age = $new_age;
  }
  return $max_age;
}

Functions

Namesort descending Description
ape_cache_set Statically cache an expiration length.
ape_form_system_performance_settings_alter Implements hook_form_FORM_ID_alter().
ape_help Implements hook_help().