You are here

function imagecache_external_flush_cache in Imagecache External 7.2

Same name and namespace in other branches
  1. 8 imagecache_external.module \imagecache_external_flush_cache()

Helper function to flush caches.

Parameters

string $mode: The mode to call the cache flush in. Two options:

  • 'all' will flush all items in the cache (default).
  • 'cron' will flush only items older than the cron threshold.

Return value

boolean A Boolean value to indicate that the operation succeeded.

2 calls to imagecache_external_flush_cache()
imagecache_external_cron in ./imagecache_external.module
Implements hook_cron().
imagecache_external_flush_form_submit in ./imagecache_external.admin.inc
Submit handler.

File

./imagecache_external.module, line 481
Allows the usage of Image Styles on external images.

Code

function imagecache_external_flush_cache($mode = 'all') {
  $success = FALSE;
  $path = file_build_uri(variable_get('imagecache_directory', 'externals'));
  switch ($mode) {
    case 'cron':
      $threshold = (int) variable_get('imagecache_external_cron_flush_threshold', '');

      // If a threshold is not set, don't flush the cache.
      if ($threshold <= 0) {
        break;
      }
      $now = time();
      $removed_count = 0;
      $external_cache_files = file_scan_directory($path, '/.+\\.(jpe?g|gif|png)$/i');
      foreach ($external_cache_files as $external_cache_file) {
        $uri = $external_cache_file->uri;
        $stat = stat($uri);
        if ($now - $stat['mtime'] > $threshold * 3600 * 24) {
          if (drupal_unlink($uri)) {
            $removed_count++;
          }
          else {
            watchdog('imagecache_external', 'Could not remove cached external file @file', array(
              '@file' => $uri,
            ), WATCHDOG_WARNING);
          }
        }
      }
      if ($removed_count > 0) {
        watchdog('imagecache_external', '@count files removed from the external image cache', array(
          '@count' => $removed_count,
        ));
      }

      // This is always considered successful.
      $success = TRUE;
      break;
    case 'all':
    default:
      if (is_dir($path)) {
        if (file_unmanaged_delete_recursive($path)) {
          watchdog('imagecache_external', 'Imagecache caches have been flushed');
          $success = TRUE;
        }
      }
      break;
  }
  return $success;
}