You are here

function advagg_flush_caches in Advanced CSS/JS Aggregation 6

Same name and namespace in other branches
  1. 7.2 advagg.module \advagg_flush_caches()
  2. 7 advagg.module \advagg_flush_caches()

Implementation of hook_flush_caches().

10 calls to advagg_flush_caches()
advagg_admin_flush_cache_button in ./advagg.admin.inc
Cache clear button.
advagg_bundler_disable in advagg_bundler/advagg_bundler.install
Implements hook_disable().
advagg_bundler_enable in advagg_bundler/advagg_bundler.install
Implements hook_enable().
advagg_css_compress_disable in advagg_css_compress/advagg_css_compress.install
Implementation of hook_disable().
advagg_css_compress_enable in advagg_css_compress/advagg_css_compress.install
Implementation of hook_enable().

... See full list

1 string reference to 'advagg_flush_caches'
advagg_enable in ./advagg.install
Implementation of hook_enable().

File

./advagg.module, line 1597
Advanced CSS/JS aggregation module

Code

function advagg_flush_caches() {

  // Try to allocate enough time to flush the cache
  if (function_exists('set_time_limit')) {
    @set_time_limit(variable_get('advagg_set_time_limit', ADVAGG_SET_TIME_LIMIT));
  }
  global $_advagg;

  // Only one advagg cache flusher can run at a time.
  if (!lock_acquire('advagg_flush_caches')) {
    return;
  }

  // Only run code below if the advagg db tables exist.
  if (!db_table_exists('advagg_files')) {
    return array(
      'cache_advagg_bundle_reuse',
    );
  }

  // Find files that have changed.
  $needs_refreshing = array();
  $results = db_query("SELECT * FROM {advagg_files}");
  while ($row = db_fetch_array($results)) {
    $checksum = advagg_checksum($row['filename']);

    // Let other modules see if the bundles needs to be rebuilt.
    // hook_advagg_files_table
    // Return TRUE in order to increment the counter.
    $hook_results = module_invoke_all('advagg_files_table', $row, $checksum);

    // Check each return value; see if an update is needed.
    $update = FALSE;
    if (!empty($hook_results)) {
      foreach ($hook_results as $update) {
        if ($update === TRUE) {
          break;
        }
      }
    }

    // Increment the counter if needed and mark file for bundle refreshment.
    if ($checksum != $row['checksum'] || $update == TRUE) {
      $needs_refreshing[$row['filename_md5']] = $row['filename'];

      // Update checksum; increment counter.
      db_query("UPDATE {advagg_files} SET checksum = '%s', counter = counter + 1 WHERE filename_md5 = '%s'", $checksum, $row['filename_md5']);
    }
  }

  // Get the bundles.
  $bundles = array();
  foreach ($needs_refreshing as $filename_md5 => $filename) {
    $results = db_query("SELECT bundle_md5 FROM {advagg_bundles} WHERE filename_md5 = '%s'", $filename_md5);
    while ($row = db_fetch_array($results)) {
      $bundles[$row['bundle_md5']] = $row['bundle_md5'];
    }
  }
  foreach ($bundles as $bundle_md5) {

    // Increment Counter
    db_query("UPDATE {advagg_bundles} SET counter = counter + 1, timestamp = %d WHERE bundle_md5 = '%s'", time(), $bundle_md5);
    if (variable_get('advagg_rebuild_on_flush', ADVAGG_REBUILD_ON_FLUSH)) {

      // Rebuild bundles on shutdown in the background. This is needed so that
      // the cache_advagg_bundle_reuse table has been cleared.
      register_shutdown_function('advagg_rebuild_bundle', $bundle_md5, '', TRUE);
    }
  }
  $_advagg['bundles'] = $bundles;
  $_advagg['files'] = $needs_refreshing;

  // Garbage collection
  list($css_path, $js_path) = advagg_get_root_files_dir();
  file_scan_directory($css_path, '.*', array(
    '.',
    '..',
    'CVS',
  ), 'advagg_delete_file_if_stale', TRUE);
  file_scan_directory($js_path, '.*', array(
    '.',
    '..',
    'CVS',
  ), 'advagg_delete_file_if_stale', TRUE);
  lock_release('advagg_flush_caches');
  return array(
    'cache_advagg_bundle_reuse',
  );
}