How to effectively use cache in Adobe Commerce(Magento)

Caching is an important aspect of optimizing website performance in Magento. Here are some ways to effectively use cache in Adobe Commerce:

Best Practices for caching

  • Enable Full Page Cache (FPC): Adobe Commerce comes with a built-in Full Page Cache that can dramatically speed up the loading time of pages. Enabling FPC can be done by going to System > Cache Management > Full Page Cache and selecting “Enable” from the drop-down menu. Once enabled, FPC will store the HTML of each page, reducing the amount of processing required to generate each page.
  • Use Varnish Cache: Varnish Cache is a powerful caching solution that can be used in conjunction with Adobe Commerce’s built-in FPC. Varnish Cache sits between the web server and the client and caches the content of each request, serving cached content to subsequent requests. To enable Varnish Cache, you need to configure it in your web server and configure Adobe Commerce to use it as a caching proxy.
  • Use Redis Cache: Redis is an in-memory key-value store that can be used as a cache backend for Adobe Commerce. By default, Adobe Commerce uses file-based caching, which can be slow and inefficient. Redis can be faster and more efficient because it stores data in memory instead of on disk. To use Redis Cache in Adobe Commerce, you need to install and configure the Redis extension, and then configure Adobe Commerce to use Redis as a cache backend.
  • Use Content Delivery Networks (CDN): A CDN is a network of servers that cache static content, such as images, CSS, and JavaScript files, and serves them from the server closest to the user. This can dramatically improve the loading time of static content and reduce the load on your server. Adobe Commerce has built-in support for CDN integration, which can be configured under System > Configuration > Web > CDN.
  • Monitor Cache Performance: It’s important to regularly monitor the performance of your caching solutions to ensure they are working effectively. You can use tools like New Relic or Blackfire to monitor the performance of your website and identify any caching-related issues.

Types Cache:

  • Configuration Cache: Stores configuration settings for the website, including the website’s system configuration and database configuration.
  • Layout Cache: Stores the layout of pages and blocks on the website, including the position of blocks, their content, and other layout settings.
  • Block Cache: Stores the HTML output of blocks on the website, reducing the amount of processing required to generate each page.
  • Collections Data Cache: Stores data related to collections, such as product or category data, to speed up database queries.
  • Reflection Cache: Stores metadata about classes and interfaces, reducing the amount of time required to load and process classes.
  • Page Cache: Stores the entire HTML output of a page, including dynamic content, to reduce the amount of processing required to generate each page.
  • EAV Cache: Stores data related to entities, attributes, and values, speeding up database queries.
  • Translations Cache: Stores translations for the website, reducing the amount of processing required to generate translated content.
  • Integration Configuration Cache: Stores configuration settings for integrations, including third-party integrations and APIs.
  • Web API Cache: Stores data related to the Magento Web API, reducing the amount of processing required to generate API responses.

Create a custom cache list in Magento

  • Create a new file named cache.xml in the directory app/code/<Vendor>/<Module>/etc/.
  • In the cache.xml file, define the custom cache type by adding the following code:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd">
   <type name="custom_cache_type" translate="label description" instance="<Vendor>\<Module>\Cache\Type\CustomCacheType">
       <label>Custom Cache Type</label>
       <description>Custom Cache Type Description</description>
   </type>
</config>

In this code, replace <Vendor> with the name of your module vendor, and <Module> with the name of your module. Also, replace CustomCacheType with the name of your custom cache type.

  • Create a new file named CustomCacheType.php in the directory app/code///Cache/Type/.
  • In the CustomCacheType.php file, define the custom cache type class by adding the following code:
<?php
namespace <Vendor>\<Module>\Cache\Type;
class CustomCacheType extends \Magento\Framework\Cache\Frontend\Decorator\TagScope
{
   const TYPE_IDENTIFIER = 'custom_cache_type';
   const CACHE_TAG = 'CUSTOM_CACHE_TYPE';
   public function __construct(\Magento\Framework\App\Cache\Type\FrontendPool $cacheFrontendPool)
   {
       parent::__construct($cacheFrontendPool->get(self::TYPE_IDENTIFIER), self::CACHE_TAG);
   }
}

In this code, replace <Vendor> with the name of your module vendor, and <Module> with the name of your module. Also, replace CustomCacheType with the name of your custom cache type.

  • Clear the cache by running the following command in the Magento 2 root directory:
php bin/magento cache:clean
  • Verify that your custom cache type is listed in the cache list by going to System > Cache Management in the Magento 2 admin panel.

Once your custom cache type is added, you can use it to cache custom data or functionality in your module.

By effectively using cache in Adobe Commerce, you can significantly improve the performance of your website and provide a better user experience for your customers.

Also read the what is partial caching in Mageto 2

Scroll to Top