You are here

function media_acquiadam_flush_outdated_cache in Media: Acquia DAM 7

Flush expired asset cache.

Return value

array An array of asset IDs that were cleared out.

1 call to media_acquiadam_flush_outdated_cache()
media_acquiadam_cron in ./media_acquiadam.module
Implements hook_cron().

File

includes/media_acquiadam.cache.inc, line 162
Cache helpers and associated logic.

Code

function media_acquiadam_flush_outdated_cache() {
  $assetIds = [];
  $query = db_select('acquiadam_asset_cache', 'c')
    ->fields('c', [
    'asset_id',
  ]);
  $or = db_or()
    ->condition('c.expiration', REQUEST_TIME, '<=');
  $cache_expiration = variable_get('media_acquiadam_cache_expiration');
  if ($cache_expiration) {

    // Clear out cache entries where the cache is stale.
    $or
      ->where('(c.changed + :cache_expiration) <= :request_time', [
      ':cache_expiration' => $cache_expiration * 60 * 1000,
      ':request_time' => REQUEST_TIME,
    ]);
  }
  $unused_expiration = variable_get('media_acquiadam_unused_expiration');
  if ($unused_expiration) {

    // Clear out cache entries where no file is using the asset.
    $fq = db_select('file_managed', 'm')
      ->fields('m', [
      'acquiadam_id',
    ])
      ->distinct();
    $or
      ->condition(db_and()
      ->condition('c.asset_id', $fq, 'NOT IN')
      ->where('(c.changed + :unused_expiration) <= :request_time', [
      ':unused_expiration' => $unused_expiration * 60 * 1000,
      ':request_time' => REQUEST_TIME,
    ]));
  }
  $query
    ->condition($or);
  $assetIds = $query
    ->execute()
    ->fetchCol();
  if (!empty($assetIds)) {
    media_acquiadam_flush_cache($assetIds);
  }
  return $assetIds;
}