Magento cache are pages or parts of pages that are stored to improve the page load time of subsequent requests.
A cache stores data so that future calls for that data can be loaded quicker, and Magento includes many cache types, we will see ahead in this blog.
In Magento Other cache types can be created and defined as per user requirements.
Table of Content:
- 1. Magento 2 Cache Types
- 2. Magento 2 Cache Enable and Disable Using Commands
- 3. Magento 2 Get Product Image Cache Url
- 4. Magento 2 Cache Flush Vs Clean
- 5. Magento 2 Disable Cache for Block
- 6. Magento 2 Clear Cache Programmatically
- 7. Magento 2 Turn Off Cache for Development
- 8. Magento 2 Clear Cache Manually
- 9. Magento 2 Full Page Cache Not Working
- 10. Magento 2 Clear Cache by Tag
- 11. Magento 2 Cache Management
Let’s start with a quick and short description of each point one by one.
-
1. Magento 2 Cache Types
In Magento 2 there is total 14 types of cache available as listed below :
-
config :
In this type Magento collects configuration of all modules that are installed in particular magento version, merges it, and saves the merged result as the cache. -
layout :
In this type Magento collects Compiled page layouts means the layout components from all components. You have to Clean or flush this cache type after modifying layout files. -
block_html :
In this type Magento collects HTML page fragments per block. You have to Clean or flush this cache type after modifying the view layer. -
collections :
In this type Magento collects Results of database queries. Generally Magento cleans this cache automatically But, you have to Clean or flush this cache type if your custom module uses logic that results in cache entries that Magento cannot clean. -
reflection :
In this type Magento Removes a dependency between the Webapi module and the Customer module. -
db_ddl :
In this type magento Collects Database schema. Generally Magento cleans up this cache automatically, But you have to Clean or flush this cache type after you make custom changes to the database schema. -
compiled_config :
In this type magento collects Compilation configuration. -
eav :
In this type Metadata related to EAV attributes stored as cache, in general, You should not need to clean or flush this cache type. -
customer_notification :
In this type magento caches Temporary notifications that appear in the user interface. -
config_integration :
In this type magento Caches the compiled integrations on your store. You have to Clean after adding new or changing existing integrations. -
config_integration_api :
In this type magento caches Compiled integration APIs configuration of the Store’s Integrations. -
full_page :
In this type magento caches links the HTML pages so it is necessary to clean this type of cache regularly. Generally Magento cleans up this cache automatically but if modifying code level that affects HTML output then you have to Clean or flush this cache. -
config_webservice :
In this type magento Caching the Web API Structure. -
translate :
In this type after merging translations from all modules, the merger cache will be cleaned.
If you want to understand about Redis cache and it’s configuration then you can Read our blog by click Here.
-
-
2. Magento 2 Cache Enable and Disable Using Commands
-
Enable Cache Using Command :
- If You want to enable all cache types then Run Below Command :
- php bin/magento cache:enable
- If You want to enable Specific cache type then Run Below Command :
- php bin/magento cache:enable CACHE_TYPE
- For Example :
- php bin/magento cache:enable block
- If You want to enable all cache types then Run Below Command :
-
Disable Cache Using Command :
- If You want to disable all cache types then Run Below Command :
- php bin/magento cache:disable
- If You want to disable Specific cache type then Run Below Command :
- php bin/magento cache:disable CACHE_TYPE
- For Example :
- php bin/magento cache:disable block
- If You want to disable all cache types then Run Below Command :
-
Check Status Using Command :
- If You Want to Check current status of your cache types then run below command :
- bin/magento cache:status
- If You Want to Check current status of your cache types then run below command :
-
-
3. Magento 2 Get Product Image Cache Url
You can get cached images from a product object by using below code .
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$imageHelper = $objectManager->get('\Magento\Catalog\Helper\Image');
foreach($product->getMediaGalleryImages( ) as $k=>$image){
$thumbnailImage = imageHelper->init($product, 'category_page_grid')
->setImageFile($image->getFile( ))
->getUrl( );
$largeImage = imageHelper->init($product, 'product_page_image_large')
->setImageFile($image->getFile( ))
->getUrl( );
}
Here, imageHelper is instance of \Magento\Catalog\Helper\Image class.
And category_page_grid is used to get image which will be equal to the size in product list page and product_page_image_large is used to get large size image.
-
4. Magento 2 Cache Flush Vs Clean :
-
Magento 2 Cache Clean :
- For Cleaning Cache in magento below command is used :
- php bin/magento cache:clean
- By Cleaning a cache type, it will delete all items from enabled Magento cache types only.
- If You Clean Cache then, disabled cache types are not cleaned.
- For Cleaning Cache in magento below command is used :
-
Magento 2 Cache Flush :
- For Flushing Cache in magento below command is used :
- php bin/magento cache:flush
- By Flushing a cache type, it will clean the cache storage, which might affect other processes applications that are using the same storage.
- For Flushing Cache in magento below command is used :
-
-
5. Magento 2 Disable Cache for Block
To disable cache for particular block you just have to add cacheable="false” in your layout file.
Here, Below You can see the example of how you can add cacheable=”false” in block file to disable cache for particular block.
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="product.info.price">
<block class="Mageants\Fee\Block\Product\View\Validity"
name="product.info.validity"
template="Mageants_Fee::product/view/validity.phtml"
cacheable="false” />
</referenceContainer>
</body>
</page>
-
6. Magento 2 Clear Cache Programmatically
To Clear cache programatically you have to define below constuctor in particular file in which you want to clean cache.
public function __construct(
Context $context,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
) {
parent::__construct($context);
$this->_cacheTypeList = $cacheTypeList;
$this->_cacheFrontendPool = $cacheFrontendPool;
}
After defining constuctor you just have to write below code in particular function for cleaning and flushing cache programatically.
$types = array('config','layout','block_html','collections','reflection','db_ddl','eav','config_integration',
'config_integration_api','full_page','translate','config_webservice');
foreach ($types as $type) {
$this->_cacheTypeList->cleanType($type);
}
foreach ($this->_cacheFrontendPool as $cacheFrontend) {
$cacheFrontend->getBackend( )->clean();
}
-
7. Magento 2 Turn Off Cache for Development
In above GIF file we can see how we can enable/disable cache types from magento backend.
If You want to turn off cache for development for particular magento version then you have to disable it’s all types of cache from magento admin panel.
-
8. Magento 2 Clear Cache Manually
In above GIF file we can see how we can clear or flush magento cache from magento backend.
-
9. Magento 2 Full Page Cache Not Working
If the status of full_page cache type is Enable showing in result of bin/magento cache:status and in admin panel then most common issue is a block included in all pages with cacheable = false Or Maybe an extension with cacheable = false declaration in default.xml.
-
10. Magento 2 Clear Cache by Tag
In magento We can flush Magento cache of all types from Admin panel and command line.
But if we want to clean the cache only partially then we have to cleaning cache by tag.
Clean cache partially means if we have made some changes in particular product or category and then we can simply clean cache for that particular Product or category then we can use clean cache by tag method.
To clear magento cache by tag use have to write below code in perticuler file from which you want to clear cache by tag.
private $fullPageCache;
private function getCache( )
{
if (!$this->fullPageCache)
{
$this->fullPageCache =
\Magento\Framework\App\ObjectManager::getInstance( )->get(
\Magento\PageCache\Model\Cache\Type::class
);
}
return $this->fullPageCache;
}
public function cleanByTags()
{
$productId = 45; //Id of the Product whose Cache need to be cleaned
$tags = ['CAT_P_'.$productId];
$this->getCache()->clean(
\Zend_Cache::CLEANING_MODE_MATCHING_TAG, $tags
);
}
-
11. Magento 2 Cache Management
Magento 2 cache management option you will find at magento backend at System > Cache Management .
From this page you can Perform below tasks :
-
Clean / Flush Magento Cache manually :
By this option you can clean or flush all type of magento cache. -
Enable / Disable Magento Cache manually :
By this option you can Enable , Disable or Refresh all type of magento cache.
-
Flush Catalog Image Cache :
By this option you can clear all automatically watermarked and modified catalog images located in the media/catalog/product/cache folder. -
Flush JavaScript / CSS Cache :
By this option you can clear the store cache from all the merged copies of JavaScript and CSS files. -
Flush Static File Cache :
By this option you can removes any preprocessed view and static files.
-
Conclusion :
We hope above guideline helps you to clearly understand How you can use Magento cache.
From this blog you can easily understand about all the magento cache types and how you can check status, enable, disable or refresh them from magento backend and command line.