You are here

boost_expire.module in Boost Expire 7

Expires Boost caches automatically when certain Drupal actions are taken.

File

boost_expire.module
View source
<?php

/**
 * @file
 *
 * Expires Boost caches automatically when certain Drupal actions are taken.
 */

/**
 * Implements hook_menu().
 */
function boost_expire_menu() {
  $items['admin/config/system/boost_expire'] = array(
    'title' => 'Boost Expire',
    'description' => 'Configuration for Boost Expire.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'boost_expire_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_node_insert().
 */
function boost_expire_node_insert($node) {
  boost_expire_flush_boost_cache(array(
    'scope' => 'all',
  ));
}

/**
 * Implements hook_node_update().
 */
function boost_expire_node_update($node) {
  boost_expire_flush_boost_cache(array(
    'scope' => 'all',
  ));
}

/**
 * Implements hook_node_delete().
 */
function boost_expire_node_delete($node) {
  boost_expire_flush_boost_cache(array(
    'scope' => 'all',
  ));
}

/**
 * Implements hook_comment_insert().
 */
function boost_expire_comment_insert($node) {
  boost_expire_flush_boost_cache(array(
    'scope' => 'all',
  ));
}

/**
 * Implements hook_comment_update().
 */
function boost_expire_comment_update($node) {
  boost_expire_flush_boost_cache(array(
    'scope' => 'all',
  ));
}

/**
 * Implements hook_comment_delete().
 */
function boost_expire_comment_delete($node) {
  boost_expire_flush_boost_cache(array(
    'scope' => 'all',
  ));
}

/**
 * Function to clear Boost's cache.
 *
 * At this time, this is a pretty stupid function, but that's because there are
 * no easy ways to expire certain parts of the Boost cache, while Boost is still
 * in development for Drupal 7. But it does it's job.
 *
 * Clearing the entire Boost cache every time a comment is saved/updated, a node
 * is saved/updated, etc. is definitely not ideal, but it's better than having
 * to constantly hit the 'clear all caches' button or wait for cron expiration.
 *
 * @param $options
 *   Associative array of options for this flush. Currently, only supported
 *   option is 'scope' => 'all'.
 */
function boost_expire_flush_boost_cache($options = array()) {
  global $_boost, $base_path, $base_root;

  // If configured to respect the site's minimum cache lifetime, don't flush
  // cache until that much time has passed.
  if (variable_get('boost_expire_respect_minimum_cache_lifetime', 0)) {

    // Check if the minimum amount of time has passed.
    $lifetime = variable_get('cache_lifetime');
    $last_flush = variable_get('boost_expire_last_flush_time', 0);

    // If the last flush happened before the minimum lifetime has passed, exit.
    if ($last_flush > time() - $lifetime) {
      return;
    }
    else {
      variable_set('boost_expire_last_flush_time', time());
    }
  }

  // Borrow some logic from boost_htaccess_cache_dir_put() to build our base
  // directory path.
  if (empty($_boost['base_dir'])) {
    $url = $base_root . request_uri();
    $parts = parse_url($url);
    $_boost['base_dir'] = boost_get_normal_cache_dir() . '/' . $parts['host'] . $base_path;
  }

  // Borrow some logic from boost_flush_caches() to clear all the contents of
  // the boost directory for this site.
  if (!empty($options['scope']) && $options['scope'] == 'all') {
    if (isset($_boost['base_dir'])) {
      $count = _boost_rmdir($_boost['base_dir'], TRUE);
      $count = empty($count) ? 0 : $count;

      // Log the cache flush if configured.
      if (variable_get('boost_expire_log_flushes', 1)) {
        watchdog('boost_expire', 'Flushed all files (%count) from static page cache.', array(
          '%count' => $count,
        ), WATCHDOG_NOTICE);
      }
    }
  }
}

/**
 * Configuration form for Boost Expire.
 */
function boost_expire_admin_settings() {
  $minimum_cache_lifetime = variable_get('cache_lifetime') / 60;

  // Minutes
  $performance_page = l('performance', 'admin/config/development/performance');
  $form['boost_expire_respect_minimum_cache_lifetime'] = array(
    '#type' => 'checkbox',
    '#title' => format_plural($minimum_cache_lifetime, 'Respect Minimum cache lifetime (1 minute)', 'Respect Minimum cache lifetime (@count minutes)'),
    '#default_value' => variable_get('boost_expire_respect_minimum_cache_lifetime', 0),
    '#description' => t('Only flush caches after the minimum cache lifetime has passed. Minimum cache lifetime can be set on the !performance page.', array(
      '!performance' => $performance_page,
    )),
  );
  $form['boost_expire_log_flushes'] = array(
    '#type' => 'checkbox',
    '#title' => t('Log Cache Flushes'),
    '#default_value' => variable_get('boost_expire_log_flushes', 1),
    '#description' => t('Writes an entry to the watchdog log every time Boost Expire flushes the Boost static cache.'),
  );
  return system_settings_form($form);
}