Flush Cache Cron: module for Drupal 7 to flush the cache

Flush Cache Cron is a module for Drupal 7 to manually flush the cache and for set the cache flushing during the cron, this module only flushes the cache without rebuilding the system bootstrap.

The module empties the tables:

  • cache
  • cache_block
  • cache_filter
  • cache_form
  • cache_menu
  • cache_page
  • cache_path
  • cache_views
  • cache_views_data

Here there is the code:

<?php

function flush_cache_cron() {
  try {
    $core = array('cache', 'cache_block', 'cache_filter', 'cache_form', 'cache_menu', 'cache_page', 'cache_path', 'cache_views', 'cache_views_data');
    foreach($core as $table) {
    cache_clear_all('*', $table, TRUE);
    }
	} catch (Exception $e) {
    return $e;
  }
  return NULL;
}

function flush_cache_cron_cron() {
  // Interval defaults to 10800s = 3h.
  $interval = flush_cache_cron_interval();
  // Set interval to 0 to disable.
  if ($interval <= 0) {
    return;
  }
  $lastrun = variable_get('flush_cache_cron_lastrun', 0);
  if (REQUEST_TIME - $lastrun >= $interval) {
    $exception = flush_cache_cron();
    list($message, $placeholders) = flush_cache_cron_message($exception);
    watchdog('flush_cache_cron', $message, $placeholders);
    variable_set('flush_cache_cron_lastrun', REQUEST_TIME);
  }
}

function flush_cache_cron_interval() {
  return variable_get('flush_cache_cron_interval', 10800);
}

function flush_cache_cron_permission() {
  return array(
    'administer flush cache' => array(
      'title' => t('Administer Flush Cache'),
    ),
  );
}

function flush_cache_cron_menu() {
  $items['admin/config/development/flush-cache-cron'] = array(
    'title' => t('Flush Cache'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('flush_cache_cron_form'),
    'access arguments' => array('administer flush cache')
  );
  return $items;
}

function flush_cache_cron_message($exception) {
  if (!$exception) {
    return array('Cache flushed.', array());
  }
  else {
    return array('DB Exception: %e', array('%e' => (string) $exception));
  }
}

function flush_cache_cron_form($form_state) {
  $form = array();
  $form['flush'] = array(
    '#type' => 'submit',
    '#value' => t('Flush Cache now'),
    '#submit' => array('flush_cache_cron_button')
  );
  return variable_group_form($form, $form_state, 'flush_cache_cron');
}

function flush_cache_cron_button(&$form, $form_state) {
  $exception = flush_cache_cron();
  list($message, $placeholders) =  flush_cache_cron_message($exception);
  drupal_set_message(t($message, $placeholders));
}

 

AttachmentSize
Package icon flush_cache_cron-7.x.zip1.85 KB
Section: