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.
Let’s start with a quick and short description of each point one by one.
In Magento 2 there is total 14 types of cache available as listed below :
If you want to understand about Redis cache and it’s configuration then you can Read our blog by click Here.
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.
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> |
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(); } |
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.
In above GIF file we can see how we can clear or flush magento cache from magento backend.
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.
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 ); } |
Magento 2 cache management option you will find at magento backend at System > Cache Management .
From this page you can Perform below tasks :
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.
Sign In
Create New Account