Associate cache frontends with cache types

Overview of Magento caching

Magento enables you to configure alternatives to the default file system caching. This guide discusses some of those alternatives; namely,

  • Set up the following cache mechanisms in the Magento configuration:

    • Database
    • Redis
    • File system (default): No configuration is necessary to use file system caching.
  • Set up the Varnish without modifying the Magento configuration.

We'll periodically add more cache alternatives so watch this space.

Magento uses the following caching terminology:

  • Frontend: Similar to an interface or gateway to cache storage, implemented by Magento\Framework\Cache\Frontend.
  • Cache types: Can be one of the types provided with Magento or you can create your own.
  • Backend: Specifies details about cache storage, implemented by Magento\Framework\Cache\Backend
  • Two-level backend: Stores cache records in two backends—a faster one and a slower one.

    Two-level backend cache configuration is beyond the scope of this guide at this time.

This topic discusses the following options for configuring caching:

  • Modifying the provided default cache frontend, which means you modify only <your Magento install dir>/app/etc/di.xml (the Magento application’s global dependency injection configuration)
  • Configuring your own custom cache frontend, which means you modify only <your Magento install dir>/app/etc/env.php because it overrides the equivalent configuration in di.xml

Varnish requires no changes to the Magento configuration. For more information, see Configure and use Varnish.

Step 1: Define a cache frontend

The Magento application has a default cache frontend you can use for any cache type. This section discusses how to optionally define a cache frontend with a different name, which is preferable if you expect to customize your frontend.

To use the default cache type, you don't need to modify env.php at all; you modify Magento's global di.xml. See the topics referenced in Low-level cache options.

You must specify a custom cache frontend either app/etc/env.php or Magento’s global app/etc/di.xml.

The following example shows how to define it in env.php (which overrides di.xml):

'cache' => [
    'frontend' => [
        '<unique frontend id>' => [
             <cache options>
        ],
    ],
    'type' => [
         <cache type 1> => [
             'frontend' => '<unique frontend id>'
        ],
    ],
    'type' => [
         <cache type 2> => [
             'frontend' => '<unique frontend id>'
        ],
    ],
],

where <unique frontend id> is a unique name to identify your frontend and <cache options> are options discussed in the topics specific to each type of caching (database, Redis, and so on).

Step 2: Configure the cache

You can specify frontend and backend cache configuration options in env.php or di.xml. This task is optional.

env.php example:

'frontend' => <frontend_type>,
'frontend_options' => [
    <frontend_option> => <frontend_option_value>,
    ...
],
'backend' => <backend_type>,
'backend_options' => [
    <backend_option> => <backend_option_value>,
    ...
],

where

  • <frontend_type> is the low-level frontend cache type. Specify the name of a class that is compatible with Zend_Cache_Core.

    If you omit <frontend_type>, Magento\Framework\Cache\Core is used.

  • <frontend_option>, <frontend_option_value> are the name and value of options the Magento framework passes as an associative array to the frontend cache upon its creation.
  • <backend_type> is the low-level backend cache type. Specify the name of a class that is compatible with Zend_Cache_Backend and that implements Zend_Cache_Backend_Interface.
  • <backend_option>, <backend_option_value> are the name and value of options the Magento framework passes as an associative array to backend cache upon its creation.

Next step

Low-level cache options