Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

The cache decorator can be used to cache plugin definition discovery, which is especially important for annotations based disovery as that is considerably slower than a info hook based discovery.

To use the cache decorator, use the following code in your plugin manager class:

<?php
class FormatterPluginManager extends PluginMangerBase {
  public function
__construct() {
   
$this->discovery = new AnnotatedClassDiscovery('field', 'formatter');
   
// Add a cache decorator and specify the cache identifier and bin.
   
$this->discovery = new CacheDecorator($this->discovery, 'field_formatter_types', 'field');
  }
}
?>

To clear cache definitions, use

<?php
Drupal
::service('plugin.manager.block')->clearCachedDefinitions();
?>

To allow context-specific caching (typically means caching each language separately, which is required if translated strings are used for labels and other texts), a dynamic cache identifier in combination with a cache tag can be used.

<?php
$this
->discovery = new CacheDecorator($this->discovery, 'mock_block:' . \Drupal::languageManager()->getLanguage()->langcode, 'cache', $expiration, array('plugin_test'));
?>

This causes the cache decorator to add the tag to all created caches (one for each language he is invoked in) and will use the tags to clear the caches instead of just the current cache identifier.

Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done