You are here

function media_filter_invalidate_caches in D7 Media 7

Clears caches that may be affected by the media filter.

The media filter calls file_load(). This means that if a file object is updated, the check_markup() and field caches could return stale content. There are several possible approaches to deal with this:

  • Disable filter caching in media_filter_info(), this was found to cause a 30% performance hit from profiling four node teasers, due to both the media filter itself, and other filters that can't be cached.
  • Clear the filter and field caches whenever any media node is updated, this would ensure cache coherency but would reduce the effectiveness of those caches on high traffic sites with lots of media content updates.
  • The approach taken here: Record the fid of all media objects that are referenced by the media filter. Only clear the filter and field caches when one of these is updated, as opposed to all media objects.
  • @todo: consider an EntityFieldQuery to limit cache clearing to only those entities that use a text format with the media filter, possibly checking the contents of those fields to further limit this to fields referencing the media object being updated. This would need to be implemented carefully to avoid scalability issues with large result sets, and may not be worth the effort.

Parameters

$fid: Optional media fid being updated. If not given, the cache will be cleared as long as any file is referenced.

5 calls to media_filter_invalidate_caches()
media_field_update_instance in includes/media.fields.inc
Implements hook_field_instance_update().
media_file_style_flush in ./media.module
Implements hook_file_style_flush().
media_file_update in ./media.module
Implements hook_file_update().
media_image_style_flush in ./media.module
Implements hook_image_style_flush().
media_styles_style_flush in ./media.module
Implements hook_styles_style_flush().

File

includes/media.filter.inc, line 671
Functions related to the WYSIWYG editor and the media input filter.

Code

function media_filter_invalidate_caches($fid = FALSE) {

  // If fid is passed, confirm that it has previously been referenced by the
  // media filter. If not, clear the cache if the {media_filter_usage} has any
  // valid records.
  if ($fid && db_query('SELECT fid FROM {media_filter_usage} WHERE fid = :fid', array(
    ':fid' => $fid,
  ))
    ->fetchField() || !$fid && media_filter_usage_has_records()) {

    // @todo: support entity cache, either via a hook, or using module_exists().
    cache_clear_all('*', 'cache_filter', TRUE);
    cache_clear_all('*', 'cache_field', TRUE);
  }
}