Managing Magento 2 cache via the command line

Cache management is one of the new optimized key features in Magento 2. We will be using the following cache types:

Cache types

Cache type code name

Description

Configuration

config

Magento 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 filesystem and database.

Clean or flush this cache type after modifying configuration files.

Layout

layout

This is the compiled page layout (that is, the layout components from all components).

Clean or flush this cache type after modifying layout files.

Block HTML output

block_html

This is the HTML page fragments per block.

Clean or flush this cache type after modifying the view layer.

Collections data

collections

This is the result 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.

DDL

db_ddl

This is the database 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.

EAV

eav

This is metadata 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 cache

full_page

This is the generated 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's recommended to keep this cache enabled because caching HTML improves performance significantly.

Reflection

reflection

This removes a dependency between the web API module and the Customer module.

Translations

translate

This is the merged translations from all modules.

Integration configuration

config_integration

This is the compiled integrations.

Clean or flush this cache after changing or adding integrations.

Integration API configuration

config_integration_api

This is the compiled integration APIs.

Web services configuration

config_webservice

This is the web API structure.

By default, we will be using Full Page Cache now in the community version, which is a great improvement next to the web services (API) caches.

Depending on the current development, default, and production state, caches will be different.

In the next recipes of this chapter, we will dive deeper into the use of different states.

Getting ready

When cleaning or flushing your cache, Magento will flush its content from either the var/cache or var/full_page directory. In this recipe, we will refer to the bin/magento cache:enable, bin/magento cache:disable, bin/magento cache:clean, or bin/magento cache:flush options.

How to do it...

For the purpose of this recipe, let's assume that we need to manage the Magento 2 cache setup. The following steps will guide you through this:

  1. Let's first check the current status using the following command:
    php bin/magento cache:status
    

    The output looks like this:

    How to do it...
  2. Now we will check how to enable and disable caches individually or all at once. Use the following command to disable the caches individually:
    php bin/magento cache:disable config
    

    This will disable the cache for only config. You may pick any cache type code name. To enable the config cache back, use the following command:

    php bin/magento cache:enable config
    

    When skipping the cache type code name behind the command, we will be able to enable or disable the caches all at once. It will look like this when we disable the cache:

    How to do it...
  3. When we want to re-enable all caches, we use php bin/magento cache:enable. As you can see now, after enabling the caches, they are cleaned as well:
    How to do it...
  4. Now let's clean the caches individually using php bin/magento cache:clean config. By removing the cache type code name, we will be able to clean all of them at once.
  5. Now let's flush the caches individually using php bin/magentocache:flush config. By removing the cache type code name, we will be able to clean all of them at once.

    Tip

    Cleaning a cache type deletes all items from enabled Magento cache types only. In other words, this option does not affect other processes or applications because it cleans only the cache that Magento uses.

    Disabled cache types are not cleaned.

    Flushing a cache type purges the cache storage (such as Redis, Memcache, and so on), which might affect other process' applications that are using the same storage.

How it works…

Let's recap and find out what we did throughout this recipe. In steps 1 through 5, you learned how to manage the cache in Magento 2.

In step 1, you learned how to use the status option to check what the current cache status is. In step 2, we were able to enable or disable the caches individually.

In steps 4 and 5, you learned how to flush and clean the caches individually.

There's more…

You can also flush all cached items running the following command from the shell:

php bin/magento cache:flush -all

Keep in mind that cleaning or flushing your Full Page Cache can resolve in a cold (no-cache) page, so warming up all pages is advised.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.99.152