Magento 2 Cache Management
Below, I shed light on cache management in Magento 2. Since the second version of the platform differs from the current one, it is vitally important to understand all the nuances related to Magento 2 Cache Management. Let’s start with cache types available in Magento 2.
Everything about Magento 2 on Firebear
Table of contents
Cache Types of Magento 2
The second version of the platform works with the following types of cache:
-
Configuration (config) cache – this cache appears when Magento gathers configuration from all its modules, merges it, and saves to the cache. Additionally, it includes store-specific settings from the file system and database. You should flush or clean this type of cache after changing configuration files.
-
Layout (layout) cache consists of compiled page layouts . As you might have guessed, this cache type requires cleaning or flushing after you modify layout files.
-
Block HTML output (block_html) cache – this one is about HTML page fragments per each block. Clean or flush block_html cache after changing the view layer.
-
Collections data (collections) cache is related to database queries. Magento offers an automated cleaning for this cache if necessary. At the same time, it is possible to put any data in any segment of collections cache . Please note you should flush it if your custom modules use logic that leads to cache entries that Magento is unable to clean.
-
DDL (db_ddl) cache – it is all about database schema. It can be cleaned up automatically as well, and it is possible to put any data in any segment of db_ddl cache . The cache should be flushed after custom changes to the database schema.
-
Entity attribute value (eav) cache – this cache includes Metadata related to EAV attributes, such as store labels, search settings, attribute rendering, etc. There is no need to flush or clean this cache.
-
Page cache (full_page) cache occurs due to generated HTML pages. It is automatically cleaned by Magento and can be modified by but third-party developers. Clean full_page cache after changing code level that influences HTML output.
-
Translations (translate) cache consist of merged translations from all modules.
-
Integration configuration (config_integration) cache is related to compiled integrations. Clean it after adding new integrations or modifying existing ones.
-
Integration API configuration (config_integration_api) cache is about compiled integration APIs.
-
Web services configuration (config_webservice) cache – the cache of web API structure.
Prerequisites
-
You should be logged in to the Magento server as a user with permissions to write to the Magento file system. Just switch to the web server user.
-
For running Magento commands from any directory, take the following code and add it to the system PATH.
1 |
<your Magento install dir>/bin |
Since shells often have differing syntax, you should check
1 |
export PATH=$PATH://var/www/html/magento2/bin |
These are some other ways for runing the commands:
1.
1 |
cd <your Magento install dir>/bin |
run them as:
1 |
./magento <command name> |
2.
1 |
php <your Magento install dir>/bin/magento <command name> |
<your Magento install dir> is a subdirectory of your web server’s docroot. If you have any troubles while locating the docroot, check
More arguments:
How to Check the Cache Status
By entering the following code, you will display the cache status
1 |
magento cache:status |
If everything is of, it should look like this one:
1 2 3 4 5 6 7 8 9 10 11 |
config: 1 layout: 1 block_html: 1 collections: 1 db_ddl: 1 eav: 1 full_page: 1 translate: 1 config_integration: 1 config_integration_api: 1 config_webservice: 1 |
How to Enable/Disable Cache Types
With the aid of the below command options, you can simultaneously enable or disable all cache types or work only with specified ones. Disabling cache types is necessary while development, since it provides the ability to see the results of your improvements without flushing cache. You should also remember that this procedure significantly decrease performance. So use the following command options to enable/disable cache.
1 |
magento cache:enable [type] |
1 |
magento cache:disable [type] |
where [type] is a space-separated list of cache types. You can enable/disable all cache types simultaneously by omitting this option.
View the list of cache types and status, with
1 |
magento cache:status |
In case when you need to disable the DDL cache and the full page cache, use the following command
1 |
magento cache:disable full_page db_ddl |
This is how the result should look like:
1 2 3 4 |
Changed cache status: full_page: 1 -> 0 db_ddl: 1 -> 0 |
How to Clean and Flush Cache Types
To delete outdated items from cache, you should clean or flush it. Consider the following:
-
If you clean a cache type, you delete all items only from the enabled types. Disabled types remain unaffected.
-
If you flushing a cache type you purge its storage. Please note that all cache types with a specified storage are flushed.
Use the below commands to clean or flush cache respectively:
1 |
magento cache:clean [type] |
1 |
magento cache:flush [type] |
where [type] is a list of your cache types separated with a space. Omit it to clean or flush all cache types simultaneously.
To clean all cache types, enter
1 |
magento cache:clean |
You should get the following result:
1 2 3 4 5 6 7 8 9 10 11 12 |
Flushed cache types: config layout block_html collections db_ddl eav full_page translate config_integration config_integration_api config_webservice |
Congratulations! Now, you know how to work with cache in Magento 2. In case of problems, live your comments below – the Firebear team will always help you.
To check the official documentation, related to cache management of Magento 2, visit this page: