Magento 2 Cache

Magento 2 Cache tutorial on different usage scenarios and why Cache should be enabled on your store.

How Cache works #

Cache works by Pages (or parts of pages) that are stored, and not generated on each request, to improve the page load time of subsequent requests.

Cache Enable #

Magento 2 enable cache is a action you can do from different application areas.

Cache Enable Backend #

  • Login into the Magento Admin area
  • Go to System > Tools > Cache Management
  • Select the desired Cache Types and click Enable

Cache Enable Command line #

To enable cache you can execute following command:

bin/magento cache:enable

The output of cache disable can be:

There is nothing to change in cache status

if cache is already enabled, or:

Changed cache status:
                        config: 0 -> 1
                        layout: 0 -> 1
                    block_html: 0 -> 1
                   collections: 0 -> 1
                    reflection: 0 -> 1
                        db_ddl: 0 -> 1
               compiled_config: 0 -> 1
                           eav: 0 -> 1
         customer_notification: 0 -> 1
                     full_page: 0 -> 1
            config_integration: 0 -> 1
        config_integration_api: 0 -> 1
                     translate: 0 -> 1
             config_webservice: 0 -> 1
Cleaned cache types:
config
layout
block_html
collections
reflection
db_ddl
compiled_config
eav
customer_notification
full_page
config_integration
config_integration_api
translate
config_webservice

if cache was previously disabled.

Cache Disable #

Disabling cache in Magento 2 has an adverse effect on performance so only disable if your server has enough resources available to handle all the requests. It is highly recommended to have the Cache Enabled in a production environment.

Cache Disable Backend #

  • Login into the Magento Admin area
  • Go to System > Tools > Cache Management
  • Select the desired Cache Types and click Disable

Cache Disable Command Line #

To disabled cache you can execute following command:

bin/magento cache:disable

The output of cache disable can be:

Changed cache status:
                    reflection: 1 -> 0
                        db_ddl: 1 -> 0
               compiled_config: 1 -> 0
                           eav: 1 -> 0
         customer_notification: 1 -> 0
            config_integration: 1 -> 0
        config_integration_api: 1 -> 0
                     translate: 1 -> 0
             config_webservice: 1 -> 0

Cache Clean #

Cache Clean Backend #

You can Clean Cache via admin area by going to:

Go to System > Tools > Cache Management > Select Cache Types and click “Clean”

Cache Clean Command Line #

To clean the Magento cache you can use below command:

php bin/magento cache:clean

The output of the cache clean can be:

Cleaned cache types:
config
layout
block_html
collections
reflection
db_ddl
compiled_config
eav
customer_notification
full_page
config_integration
config_integration_api
translate
config_webservice

Cache Flush #

If you tried Cache Clean and you are still having issues with cached parts of the application. Flushing the cache will result in everything being deleted so it’s a last resort type solution.

To flush the cache you can use command below:

php bin/magento cache:flush

The output of the cache flush can be:

Flushed cache types:
config
layout
block_html
collections
reflection
db_ddl
compiled_config
eav
customer_notification
full_page
config_integration
config_integration_api
translate
config_webservice

Cache Status #

To view the current status of the cache execute following command:

bin/magento cache:status

The output of the cache status can be:

Current status:
                        config: 1
                        layout: 1
                    block_html: 1
                   collections: 1
                    reflection: 1
                        db_ddl: 1
               compiled_config: 1
                           eav: 1
         customer_notification: 1
                     full_page: 1
            config_integration: 1
        config_integration_api: 1
                     translate: 1
             config_webservice: 1

Cache Types #

Cache type nameCache type codeDescription
ConfigurationconfigMagento collects configuration from all modules, merges it, and saves the merged result to the cache. This cache also contains store-specific settings stored in the file system and database. Clean or flush this cache type after modifying configuration files.
LayoutlayoutCompiled page layouts (that is, the layout components from all components). Clean or flush this cache type after modifying layout files.
Block HTML outputblock_htmlHTML page fragments per block. Clean or flush this cache type after modifying the view layer.
Collections datacollectionsResults of database queries. If necessary, Magento cleans up this cache automatically, but third-party developers can put any data in any segment of the cache. Clean or flush this cache type if your custom module uses logic that results in cache entries that Magento cannot clean.
DDLdb_ddlDatabase schema. If necessary, Magento cleans up this cache automatically, but third-party developers can put any data in any segment of the cache. Clean or flush this cache type after you make custom changes to the database schema. (In other words, updates that Magento does not make itself.) One way to update the database schema automatically is using the magento setup:db-schema:upgrade command.
Compiled Configcompiled_configCompilation configuration
Entity attribute value (EAV)eavMetadata related to EAV attributes (for example, store labels, links to related PHP code, attribute rendering, search settings, and so on). You should not typically need to clean or flush this cache type.
Page cachefull_pageGenerated HTML pages. If necessary, Magento cleans up this cache automatically, but third-party developers can put any data in any segment of the cache. Clean or flush this cache type after modifying code level that affects HTML output. It is recommended to keep this cache enabled because caching HTML improves performance significantly.
ReflectionreflectionRemoves a dependency between the Webapi module and the Customer module.
TranslationstranslateAfter merging translations from all modules, the merger cache will be cleaned.
Integration configurationconfig_integrationCompiled integrations. Clean or flush this cache after changing or adding integrations.
Integration API configurationconfig_integration_apiCompiled integration APIs configuration of the Store’s Integrations.
Web services configurationconfig_webserviceCaching the Web API Structure.
Customer Notificationcustomer_notificationTemporary notifications that appear in the user interface.

Need help with Magento 2 Cache ?