Magento 2 – Full Page Cache Deep Dive – Part1

As we all knew, Magento 2 has native support of full page cache with 2 choices. The first one is, in-built full page cache and next is Varnish. We can go through about how magento decides whether the page needs to be cached or not on which parameters are matters here.

We are covering the in-built full page cache process in this article.

Basically, magento caches all the pages whichever has http request method is either GET / HEAD. Magento will ignore the pages from cache whenever http request is POST / PUT and request is Ajax one.

We can see the overall process from starting to end.

The initial page request triggered from the client browser which will reach index.php file on application folder in the web server. (Assume, we already know how url request will be treated in internet via DNS mapping and all)

Further the request will be progressions in below way.

In the FrontContoller, Magento adds “setNoCacheHeaders()” function to all the page request at dispatch() function.

File Path : vendor\magento\framework\App\FrontController.php

In the above entire course, magento collects all xml layout configuration for specific action to decide whatever all JS, CSS, Blocks and Templates needs to be processed / included while render the relevant page.

Magento will execute “afterGenerateXml()” function which is exist in LayoutPlugin.php. After gathered all layout xml configuration, magento executed the function. The function contains below code snippet.

File Path : vendor\magento\module-page-cache\Model\Layout\LayoutPlugin.php

We can see isCacheable() function is present inside “afterGenerateXml() function which is very important function which is responsible to decide whether the page is qualified for full page cache or not.

File Path : vendor\magento\framework\View\Layout.php

isCacheable() function will checks whether any of the layout elements contains

[cacheable=”false”]

if so magento will return false statement to the original function which triggered it. And “setPublicHeaders()” will not be triggered and page will be treated as non-cacheable page.

In case magento doesn’t find any [cacheable=”false”] in the layout, it will trigger “setPublicHeaders()” function.

We can easily understand whenever any of the elements in layout xml file contains [cacheable=”false”], the entire page will be treated as non-cacheable page. So we need to be surer on when we need to use this portion.

You can understand magento sets “pragma=cache”, “cache-control = public, max-age = configured ttl time”, expires=expiration date”

All above PARAM’s are very important and server will share the page content with proper cache headers. And meanwhile this specific page will be stored on magento in-built cache directory. Next time whenever the same page request comes, magento will load the cache from cache directory and share the response to browser.

We can go through, how magento stores & retrieves the content from cache directory in our part2 article.

I hope, you may got some basic idea on magento page caching process.

Post By

Thamotharan.R