You are here

apc.module in APC - Alternative PHP Cache 7

This integrates the drupal APC cache module.

File

apc.module
View source
<?php

/**
 * Implements hook_xmlrpc().
 */
function apc_xmlrpc() {
  $methods[] = array(
    'apc_drush_flush',
    // Method name.
    'apc_drush_flush',
    // Callback.
    array(
      'array',
      // Return variable.
      'array',
    ),
    t('XMLRPC callback to enable cache clear from Drush/CLI.'),
  );
  return $methods;
}

/**
 * XMLRPC callback to enable cache clear from Drush/CLI.
 */
function apc_drush_flush($variables) {
  $cron_key = isset($variables['cron_key']) ? $variables['cron_key'] : NULL;
  $clears = isset($variables['clears']) ? $variables['clears'] : array();
  if (empty($cron_key) || variable_get('cron_key', 'drupal') != $cron_key) {
    watchdog('apc', 'APC could not flush cache(s) because an invalid key was used.', array(), WATCHDOG_ERROR);
    return array(
      'success' => FALSE,
      'message' => t('APC could not flush cache(s) because an invalid key was used.'),
    );
  }
  else {
    foreach ($clears as $bin => $cids) {
      $cache = _cache_get_object($bin);
      foreach ($cids as $serialized_cid => $wildcard) {
        $cache
          ->clear(unserialize($serialized_cid), $wildcard);
      }
    }
    return array(
      'success' => TRUE,
      'message' => t('APC all requested flushes done.'),
    );
  }
}

/**
 * Implementation of hook_init().
 *
 * Used for displaying the APC stats for debug purposes.
 */
function apc_init() {
  global $user;
  if ($user->uid == 0 || !variable_get('apc_show_debug', FALSE) || !user_access('access apc statistics') || strstr($_SERVER['PHP_SELF'], 'update.php') || strstr($_GET['q'], 'autocomplete')) {
    return;
  }
  register_shutdown_function('apc_shutdown');
}

/**
 * @file
 * This integrates the drupal APC cache module.
 */

/**
 * Implementation of hook_permission().
 */
function apc_permission() {
  return array(
    'access apc statistics' => array(
      'title' => t('Access apc statistics'),
      'description' => t('Allows access to the statistics reports of APC.'),
    ),
  );
}

/**
 * See apc_init() which registers this function as a shutdown function.
 * Displays apc stats in the footer.
 */
function apc_shutdown() {
  global $apc_statistics;

  // Don't call theme() during shutdown if the registry has been rebuilt (such
  // as when enabling/disabling modules on admin/build/modules) as things break.
  // Instead, simply exit without displaying admin statistics for this page
  // load.  See http://drupal.org/node/616282 for discussion.
  if (!function_exists('theme_get_registry') || !theme_get_registry()) {
    return;
  }

  // Try not to break non-HTML pages.
  if (function_exists('drupal_get_http_header')) {
    $header = drupal_get_http_header('content-type');
    if ($header) {
      $formats = array(
        'xml',
        'javascript',
        'json',
        'plain',
        'image',
        'application',
        'csv',
        'x-comma-separated-values',
      );
      foreach ($formats as $format) {
        if (strstr($header, $format)) {
          return;
        }
      }
    }
  }
  if (isset($apc_statistics) && is_array($apc_statistics)) {
    print '<div id="apc-devel"><h2>' . t('APC statistics') . '</h2>';
    $rows = array();
    foreach ($apc_statistics as $row) {
      if (is_array($row[2])) {
        $row[2] = implode(',<br />', $row[2]);
      }
      $rows[] = $row;
    }
    print theme('table', array(
      'header' => array(
        'Type',
        t('Bin'),
        t('Cid(s)'),
      ),
      'rows' => $rows,
    ));
    print '</div>';
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function apc_form_system_performance_settings_alter(&$form, $form_state) {
  $form['clear_cache']['clear']['#submit'][] = 'apc_clear_user_cache';
  $form['clear_cache']['clear']['#submit'][] = 'apc_clear_opcode_cache';
  $form['clear_cache']['apc_user'] = array(
    '#type' => 'submit',
    '#value' => t('Clear APC user cache'),
    '#submit' => array(
      'apc_clear_user_cache',
    ),
  );
  if (!extension_loaded('apcu')) {
    $form['clear_cache']['apc_opcode'] = array(
      '#type' => 'submit',
      '#value' => t('Clear APC opcode cache'),
      '#submit' => array(
        'apc_clear_opcode_cache',
      ),
    );
  }
}

/**
 * Helper function to clear user cache.
 */
function apc_clear_user_cache() {
  if (apc_clear_cache('user')) {
    drupal_set_message(t('Cleared APC user cache.'));
  }
  else {
    drupal_set_message(t('Unable to clear APC user cache.'), 'error');
  }
}

/**
 * Helper function to clear opcode cache.
 */
function apc_clear_opcode_cache() {
  if (apc_clear_cache()) {
    drupal_set_message(t('Cleared APC opcode cache.'));
  }
  else {
    drupal_set_message(t('Unable to clear APC opcode cache.'), 'error');
  }
}

Functions

Namesort descending Description
apc_clear_opcode_cache Helper function to clear opcode cache.
apc_clear_user_cache Helper function to clear user cache.
apc_drush_flush XMLRPC callback to enable cache clear from Drush/CLI.
apc_form_system_performance_settings_alter Implements hook_form_FORM_ID_alter().
apc_init Implementation of hook_init().
apc_permission Implementation of hook_permission().
apc_shutdown See apc_init() which registers this function as a shutdown function. Displays apc stats in the footer.
apc_xmlrpc Implements hook_xmlrpc().