You are here

function apdqc_run_prefetch_array in Asynchronous Prefetch Database Query Cache 7

Prefetch data from a cache table.

Parameters

array $table_keys_cache_prefetch: Array('table' => array(cache_ids)).

bool $reap_on_next_get: Set to false to not block the next cache get call if query is still running.

bool $log: Set to false to disable devel logging for this query.

15 calls to apdqc_run_prefetch_array()
ApdqcPrefetchDrupalDefaultEntityController::__call in ./apdqc.module
Magic method; forward all calls to the original base class.
apdqc_async_data in ./apdqc.mysql.inc
Used to get & parse async cached data.
apdqc_boot in ./apdqc.module
Implements hook_boot().
apdqc_ctools_plugin_pre_alter in ./apdqc.module
Implements hook_ctools_plugin_pre_alter().
apdqc_entity_load in ./apdqc.module
Implements hook_entity_load().

... See full list

17 string references to 'apdqc_run_prefetch_array'
APDQCache::clear in ./apdqc.cache.inc
Implements DrupalCacheInterface::clear().
ApdqcPrefetchDrupalDefaultEntityController::__call in ./apdqc.module
Magic method; forward all calls to the original base class.
apdqc_boot in ./apdqc.module
Implements hook_boot().
apdqc_ctools_plugin_pre_alter in ./apdqc.module
Implements hook_ctools_plugin_pre_alter().
apdqc_entity_load in ./apdqc.module
Implements hook_entity_load().

... See full list

File

./apdqc.cache.inc, line 53
Extends Drupal's default database cache so async queries happen.

Code

function apdqc_run_prefetch_array(array $table_keys_cache_prefetch, $reap_on_next_get = TRUE, $log = TRUE) {
  static $use_async_data;
  if (!isset($use_async_data)) {
    $do_not_run_prefetch_array =& drupal_static('apdqc_run_prefetch_array', array());
  }
  foreach ($table_keys_cache_prefetch as $table => $cids) {
    if (is_array($do_not_run_prefetch_array) && array_key_exists($table, $do_not_run_prefetch_array) && !empty($do_not_run_prefetch_array[$table])) {
      return;
    }
  }
  if (defined('APDQC_PREFETCH')) {

    // Bail if apdqc_prefetch is disabled.
    if (!variable_get('apdqc_prefetch', APDQC_PREFETCH)) {
      return;
    }
  }

  // Process and run query.
  $queries = array();
  foreach ($table_keys_cache_prefetch as $bin => $cids) {
    if (apdqc_get_bin_class_name($bin) !== 'APDQCache') {
      continue;
    }

    // Remove any duplicates or empty.
    $cids = array_filter(array_unique($cids));
    $query = '';
    if (isset($cids['*'])) {
      $query .= "SELECT '{$bin}' AS bin, cid, data, created, expire, serialized FROM " . apdqc_fast_prefix_tables('{' . apdqc_fast_escape_table($bin) . '}') . "";
      if (!empty($cids['*']) && is_array($cids['*'])) {
        $cids_string = "'" . implode("', '", $cids['*']) . "'";
        $query .= " WHERE cid NOT IN ({$cids_string})";
      }
    }
    else {
      $like_ids = array();
      foreach ($cids as $key => $cid) {
        if (strpos($cid, ':%') !== FALSE) {
          unset($cids[$key]);
          $like_ids[] = $cid;
        }
      }

      // Build query string.
      $query .= "SELECT '{$bin}' AS bin, cid, data, created, expire, serialized FROM " . apdqc_fast_prefix_tables('{' . apdqc_fast_escape_table($bin) . '}');
      if (!empty($cids)) {

        // Concat array into strings.
        $cids_string = "'" . implode("', '", $cids) . "'";
        $query .= " WHERE cid IN ({$cids_string}) ";
      }
      if (!empty($like_ids)) {

        // Do the first LIKE.
        $cid = array_shift($like_ids);
        if (empty($cids)) {
          $query .= " WHERE ";
        }
        else {
          $query .= "OR ";
        }
        $query .= "cid LIKE '{$cid}' ";

        // Do the rest of the LIKEs.
        foreach ($like_ids as $cid) {
          $query .= "OR cid LIKE '{$cid}' ";
        }
      }
    }
    $queries[] = array(
      $query,
      $bin,
      $cids,
    );
  }

  // Prefetch cache.
  if (variable_get('apdqc_use_union_query', APDQC_USE_UNION_QUERY)) {
    $query = '';
    $all_cids = array();
    $tables = array();
    foreach ($queries as $values) {
      $tables[] = $values[1];
      $all_cids = array_merge($all_cids, $values[2]);

      // Use a union query if hitting mutiple cache bins.
      if (!empty($query)) {
        $query .= "\nUNION ALL ";
      }
      $query .= $values[0];
    }
    apdqc_query($tables, $all_cids, $query, array(
      'async' => TRUE,
      'log' => $log,
    ));
  }
  else {
    foreach ($queries as $values) {
      apdqc_query(array(
        $values[1],
      ), $values[2], $values[0], array(
        'async' => TRUE,
        'log' => $log,
      ));
    }
  }
}