You are here

function apdqc_cron in Asynchronous Prefetch Database Query Cache 7

Implements hook_cron().

Empties out the __truncated_tables on cron.

File

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

Code

function apdqc_cron() {

  // Execute once a day (24 hours).
  if (variable_get('apdqc_cron_timestamp', APDQC_CRON_TIMESTAMP) > REQUEST_TIME - variable_get('apdqc_cron_frequency', APDQC_CRON_FREQUENCY)) {
    return;
  }
  variable_set('apdqc_cron_timestamp', REQUEST_TIME);

  // Get all cache tables.
  module_load_include('admin.inc', 'apdqc');
  if (!function_exists('apdqc_get_db_object')) {
    module_load_include('mysql.inc', 'apdqc');
  }
  $tables = apdqc_get_cache_tables();
  foreach ($tables as $table) {
    $mysqli = apdqc_get_db_object(array(
      $table,
    ), array(
      '*',
    ));

    // Skip tables if it doesn't end in __truncated_table.
    if (strpos(strrev($table), strrev('__truncated_table')) === FALSE) {
      continue;
    }

    // Get the real table name.
    $real_table_name_truncated = Database::getConnection()
      ->prefixTables("{" . db_escape_table($table) . "}");

    // Remove any values from the *__truncated_table if needed.
    $results = $mysqli
      ->query("SELECT 1 FROM {$real_table_name_truncated} LIMIT 1");

    // mysqli_result::fetch_row returns NULL when there are no more rows.
    if ($results !== FALSE && !is_null($results
      ->fetch_row())) {

      // Empty the truncated_table since it is not empty.
      apdqc_query(array(
        $real_table_name_truncated,
      ), array(
        '*',
      ), "TRUNCATE {$real_table_name_truncated}");
    }
  }
}