Magento 2, Magento

Caching Explain in Magento 2

Caching in Magento 2

Hello Everyone, Welcome to my blog. First of all for those people who don’t know anything about me please check out this link for more information. 

If you want to connect with me or want to discuss any specific topic, want to share your view or anything, please feel free to write a comment below or write me an email on my email id i.e. admin@asolutions.co.in  I will definitely try to address all your queries.

Today, I am going to discuss the caching in Magento 2. Previously, I shed some light on how to create a custom cache type in Magento 2. If you haven’t checked yet, please check out this link for more information.

We all know the importance of caching in web development and how it affects the customer journey on your website. Caching helps to improve the server response and helps you to improve conversion rates. Caching is one of the most effective ways to improve website performance. There are two types of cache in general i.e.

  1. Server Side
  2. Client Side.

Server Side Caching

Server-side caching works as an intermediate between the server and the client request. If the cache is already available then instead of calling the application the requested data is served from the cache. The Magento page cache library is using the PHP reverse proxy. 

The reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or more servers. These resources are then returned to the client, appearing as if they originated from the proxy server itself.

Client Side Caching

The client-side caching is used to serve the data from the web browser instead of requesting a web server if the previous request is already made and cached.

Cache Content

Magento is using the reverse proxy server to serve cached data and there are few challenges that Magento needs to address. There are two types of content.

  1. Private 
  2. Public

Private Content

Private contents are stored on the client-side(browser), data related to the wishlist, addresses, cart. This data can not be shared with other users and will be served to only one customer.  

Public Content

Public content is stored on the server-side and will be served to the browser when requested. Public data is shared between all the users as public content, for example, headers, footer, category listing, etc. Magento uses different methods to save public content/cache on the server-side.

  1. File System
  2. Database
  3. Varnish
  4. Redis

File System

We don’t need to do any configuration for file-based caching in Magento. We can check the configuration in etc/env.php file and all the cache (public content) are stores in var/cache and var/page_cache.

Database

Database cache works the same as file-based caching. The difference is the cache is stored in the database instead of the file system. There are two tables in which Magento saves its cache data i.e. cache and cache_tab table. Magento strongly recommends using a varnish cache in production environments.

To enable the database caching we need to modify the di.xml file from <magento_root>/app/etc/di.xml. Find the following content

<type name="Magento\Framework\App\Cache\Frontend\Pool">
    <arguments>
        <argument name="frontendSettings" xsi:type="array">
            <item name="page_cache" xsi:type="array">
                <item name="backend_options" xsi:type="array">
                    <item name="cache_dir" xsi:type="string">page_cache</item>
                </item>
            </item>
        </argument>
    </arguments>
</type>
<type name="Magento\Framework\App\Cache\Type\FrontendPool">
    <arguments>
        <argument name="typeFrontendMap" xsi:type="array">
            <item name="full_page" xsi:type="string">page_cache</item>
        </argument>
    </arguments>
</type>

Replace with :

<type name="Magento\Framework\App\Cache\Frontend\Pool">
    <arguments>
        <argument name="frontendSettings" xsi:type="array">
            <item name="page_cache" xsi:type="array">
                <item name="backend" xsi:type="string">database</item>
            </item>
            <item name="<your cache id>" xsi:type="array">
                <item name="backend" xsi:type="string">database</item>
            </item>
        </argument>
    </arguments>
</type>
<type name="Magento\Framework\App\Cache\Type\FrontendPool">
    <arguments>
        <argument name="typeFrontendMap" xsi:type="array">
            <item name="backend" xsi:type="string">database</item>
        </argument>
    </arguments>
</type>

The Magento\Framework\App\Cache\Frontend\Pool configures options for the in-memory pool of all frontend cache instances. The Magento\Framework\App\Cache\Type\FrontendPool configures cache frontend options specific to each cache type.

Note: Don’t forget to take the backup of your di.xml file

Remove the var/cache and var/page_cache directory and check the database table cache and cache_tag for cached data. 

Cache
Caching Explain in Magento 2 5
Cache Tag
Caching Explain in Magento 2 6

If you want to know more about varnish and Redis cache configuration, please let me know through my email id i.e. admin@asolutions.co.in, or write down in the comments below.

 Let me know what you think about this blog. Goodbye for now and happy coding 🙂

Tagged

Leave a Reply

Your email address will not be published. Required fields are marked *

*

code