Create a cache type

A cache type enables you to specify what is cached and enables merchants to clear that cache type using the Cache Management page in the Magento Admin.

The tag scope provides a mechanism for a cache type.

To create a new cache type:

class %Namespace%_%Module%_Model_Cache_Type extends \Magento\Cache\Frontend\Decorator\TagScope
{
  public function __construct(\Magento\Framework\App\Cache\Type\FrontendPool $cacheFrontendPool)
  { 
    parent::__construct($cacheFrontendPool->get('%cache_type_id%'), '%cache_type_tag%');
  }
}

You must specify the following parameters:

  • Namespace_Module defines the name of a module that uses a cache type. A module can use several cache types and a cache type can be used in several modules.
  • %cache_type_id% defines unique identifier of a cache type.
  • %cache_type_tag% defines unique tag to be used in the cache type scoping.

More information about caching

You can get more information about caching by looking at the code. We suggest you locate classes that extend Magento\Framework\Cache\Frontend\Decorator\TagScope.

For example, look at Magento\Eav\Model\Cache\Type to understand more about the EAV cache type.

Please help us improve this topic by suggesting details using the Edit this page in GitHub link at the top of the page.