Congratulations to our top-level developers who have recently obtained their Zend certificates in order to meet the strict requirements of ecommerce development market. Having such qualified professionals on our team, we provide our clients with services of the highest quality.
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:
- How do you add dynamic content to pages served from the full page cache?
- How to operate with cache clearing?
- In which case would you refresh cache/flash cache storage?
- How to clear the cache programmatically?
- 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:
<block class="Magento\Checkout\Block\Onepage\Success" name="checkout.success" template="success.phtml" cacheable="false"/>
<block class="Magento\Checkout\Block\Registration" name="checkout.registration" template="registration.phtml" cacheable="false"/>
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:
<li class="greet welcome" data-bind="scope: 'customer'"> <span data-bind="text: customer().fullname ? $t('Welcome, %1!').replace('%1', customer().fullname) : '<?=$block->escapeHtml($welcomeMessage) ?>'"></span> </li>
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
$this->_eventManager->dispatch('clean_cache_by_tags', ['object' => $cache]);
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:
class Page extends AbstractBlock implements IdentityInterface
public function getIdentities()
return [\Magento\Cms\Model\Page::CACHE_TAG . '_' . $this->getPage()->getId()];
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.