How to Manage the Cache Types in Magento 2

Mar 29, 20185971
How to Manage the Cache Types in Magento 2

Managing cache is another sophisticated issue in Magento 2. Today we are going to demonstrate you some cache management techniques. You are about to learn various cache types and the tools used to manage them. As usual, we are going to provide technical extracts along with plain text for your better understanding.

Here is a list of questions covered in this article:

  1. How do you add dynamic content to pages served from the full page cache?
  2. How to operate with cache clearing?
  3. In which case would you refresh cache/flash cache storage?
  4. How to clear the cache programmatically?
  5. What mechanisms are available for clearing all or part of the cache?

Adding dynamic content to pages served from the full page cache

There are two methods to add dynamic contents for full page cache (FPC).

Method 1. Use the following in layout:

Attribute cacheable=false says that the page with this block is not cached by FPC. But be careful with this parameter, because if this block is located on all pages, your FPC will not work properly.

Method 2. You can use ajax for this purpose. As usual, Magento has two types of content:

  • Public content. Public content is stored on server side in your reverse proxy cache storage (file system or database) and is available to multiple customers. Examples: header, footer, and category listing;
  • Private content. Private content is specific to individual users, therefore it is stored on client side (browser). Private content includes but is not limited to wishlist, customer name, address, etc. Magento UI component can be used to keep stored private content limited to a small portion of the page’s total content.

Magento has different FPC cache for each group of customers.

The strategy of deferring private caching and cache invalidation to the front end is best demonstrated at the example of the welcome greeting in the header. Starting in Magento_Theme:view/frontend/templates/html/header.phtml, we see the following:

The cache data is stored in sections (in the example of the customer component, the section is also called ‘customer’). You can either pull the cache sections from local storage using getFromStorage or request them via AJAX using getFromServer.

Operating with cache clearing

There are three ways to clean the cache:

  • to do it from backend Admin;
  • to use command cli bin/magento cache:clean;
  • to do it manually (be careful: if you use reds, this feature will not work) var/cache/*.

Refreshing cache/flash cache storage

When you clean cache you do it by tags. If cache tags have wrong names, they will not be cleaned from storage.

When you flush cache — you clean all cache in storage. Sometimes it’s more preferable than simply cleaning cache.

Clearing the cache programmatically

Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey

The cache tags are created at block level. Each block class implements the Identity Interface. It means they must implement a getIdentities method, which must return a unique identifier. For example:

Mechanisms for clearing all or part of the cache

You can clean cache from the admin panel. System → Cache Management

On this page you can clean/enable/disable cache by cache types or full cache. You can also clean cache from the console:

  • php bin/magento cache:clean — clean cache;
  • php bin/magento cache:clean <cache_type> — clean cache only for <cache_type>;
  • php bin/magento cache:status — see statuses and types of cache;
  • php bin/magento cache:enable  <cache_type> — enable cache;
  • php bin/magento cache:flush — flush cache.

We hope the above was helpful and led you to a deeper understanding of ways to manage cache in Magento 2. If you still have any questions, please leave them in the comment section below and our developers will make sure each of them is paid attention to.

We have another article devoted to cache management, specifically how to use default cache in Magento 2, be sure to check it out.

full_cache_store

Full Cache

Take your online store to the next level with BelVG module

Download here

Check out BelVG’s quality Magento extensions at our official store.


Partner With Us Looking for a partner that will help you to grow your business? We are the right company to develop your webstore. Feel free to get in touch with us. We will be happy to discuss your business opportunities and provide you with a Free Quote. Talk to Andrey

2 Comments

  1. We are programming a multi-site Magento 2 website in different countries, we are having continual caching issues with the client in the USA viewing the site and seeing changes to sign the site off, they keep clearing there cache, viewing incognito and private viewing but getting the same issues they are redirected to the INT site not the US site or can only see old versions of the site – any ideas

  2. Hi, Dawn! Thanks for sharing your situation with us, we’ll try to help you.
    Could you please tell if you tried to use Cdn in this case?

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *