You are here

function apdqc_boot in Asynchronous Prefetch Database Query Cache 7

Implements hook_boot().

File

./apdqc.module, line 1132
Asynchronous Prefetch Database Query Cache module.

Code

function apdqc_boot() {
  if (!function_exists('apdqc_run_prefetch_array') || !function_exists('apdqc_escape_string')) {
    require_once 'apdqc.cache.inc';
    $db_type = apdqc_fast_get_db_type();
    if ($db_type === 'mysql') {
      require_once 'apdqc.mysql.inc';
    }
  }

  // Bail if this is a cached page hit.
  $header_list = headers_list();
  if (in_array('X-Drupal-Cache: HIT', $header_list)) {
    return;
  }
  $table_keys_cache_prefetch = array();
  if (apdqc_get_bin_class_name('cache_bootstrap') === 'APDQCache') {

    // Prefetch the rest of cache_bootstrap on a cache_page miss.
    // List the cids we already have.
    $table_keys_cache_prefetch['cache_bootstrap']['*'] = array(
      'variables',
      'bootstrap_modules',
      'lookup_cache',
      'system_list',
      'module_implements',
    );
  }

  // Get defaults for language & theme.
  $lang_shorthand = apdqc_get_lang();
  $default_theme = apdqc_escape_string(variable_get('theme_default', 'bartik'));
  if (apdqc_get_bin_class_name('cache') === 'APDQCache') {
    $table_keys_cache_prefetch['cache'][] = 'theme_registry:runtime:' . $default_theme;

    // Get cache bin keys.
    $table_keys_cache_prefetch['cache'][] = 'entity_info:' . $lang_shorthand;
    $table_keys_cache_prefetch['cache'][] = 'node_types:' . $lang_shorthand;
  }
  if (apdqc_get_bin_class_name('cache_field') === 'APDQCache') {
    $uid = apdqc_escape_string($GLOBALS['user']->uid);
    $table_keys_cache_prefetch['cache_field'][] = 'field:user:' . $uid;
    $table_keys_cache_prefetch['cache_field'][] = 'field_info:fields';
    $table_keys_cache_prefetch['cache_field'][] = 'field_info:bundle:user:user';
    $table_keys_cache_prefetch['cache_field'][] = 'field_info:instances';
    $table_keys_cache_prefetch['cache_field'][] = 'field_info:field_map';
    $table_keys_cache_prefetch['cache_field'][] = 'field_groups';

    // Prefetch node field info.
    $page = apdqc_escape_string($_GET['q']);
    $bin = 'cache_field';
    $query = " SELECT '{$bin}' AS bin, {$bin}.cid, {$bin}.data, {$bin}.created, {$bin}.expire, {$bin}.serialized FROM " . apdqc_fast_prefix_tables('{' . apdqc_fast_escape_table($bin) . '}') . " AS {$bin} ";

    // Get node type.
    $matches = array();
    if (preg_match("/node\\/(\\d+)/i", $page, $matches)) {

      // If page starts with node/NID.
      $nid = apdqc_escape_string($matches[1]);
      $query .= " INNER JOIN " . apdqc_fast_prefix_tables('{' . apdqc_fast_escape_table('node') . '}') . " AS node ON node.nid = {$nid}";
    }
    else {

      // Page might be an alias to a node.
      $query .= " INNER JOIN " . apdqc_fast_prefix_tables('{' . apdqc_fast_escape_table('url_alias') . '}') . " AS url_alias ON (url_alias.alias = '{$page}' AND SUBSTRING(url_alias.source, 1, 5) = 'node/')";
      $query .= " INNER JOIN " . apdqc_fast_prefix_tables('{' . apdqc_fast_escape_table('node') . '}') . " AS node ON node.nid = SUBSTRING(url_alias.source, 6)";
    }
    $query .= " WHERE cid LIKE 'field_info:bundle:node:%' AND cid = CONCAT('field_info:bundle:node:', node.type)";
    apdqc_query(array(
      'cache_field',
    ), array(
      'field_info:bundle:node:%',
    ), $query, array(
      'async' => TRUE,
    ));
  }

  // Prefetch cache.
  apdqc_run_prefetch_array($table_keys_cache_prefetch);
}