How to Setup Database Caching in Magento 2

The faster and efficient your site, the better it is for your customers. Suppose your store is not fast enough. That will annoy the customers and the next time they will think a hundred times before buying anything from your store. In order to avoid this situation and to make your eStore faster, one can use caches.

Caching in Magento 2 will help your store to perform better and much faster even when you add more content and data into it. The cache system of Magento allows you to store the recently accessed data so that the system performance will be faster and more efficient. If we use caches in our Magento store – as Magento is a database-driven platform – the speed can be increased.

In Magento 2 database tables, the cached objects are stored in Cache and cache_tag. The Full Page caching files are stored in var/ cache or var/page_cache. If you want to know how to setup database caching in Magento 2 and how to verify if the database cache is working or not in Magento 2, then this article is really helpful for you. We are going to discuss the following options of caching the database:

  • By modifying the xml (using default front end cache)
  • By modifying the env.php (using custom front end cache)

Both of them will help you setup the database caching and make your store much speedier. Nothing is stored var/cache or var/page_cache.

Prior Conditions Before Database Caching

Before doing anything else, you must note two things that are listed below:

  1. If you are using default frontend cache, that is if you choose to modify xml then you don’t really need to associate cache frontends with the cache types.
  2. If you are using custom frontend cache, that is if you are modifying php then you don’t need to do such association.

PS: cache types can be the one the Magento provided or you can create your own. If you want to see how cache types are made, do let me know in comments!

Below are the two in-detail methods of database caching:

  • Using the Default Front End Cache:

To use this option you must modify the <your Magento 2 2 install dir>/ app / etc / di.xml, which is global deployment injection configuration for the Magento 2 application.

To alter di.xml  these are the steps that you need to follow:

  1. First, you need to login to the Magento server or simply switch to the Magento file system owner.
  2. Make copy of xml by entering the following commands shown below.

  1. Now, open up this file in the text editor and find out the block as follows.

These two types have different workings. The <type name=”Magento\Framework\App\Cache\Frontend\Pool”> configures all the options for the in memory pool of all the frontend cache instances. And <type name=”Magento\Framework\App\Cache\Type\FrontendPool”>  configures the cache frontend options that are specific to each cache type.

  1. Now just simply replace the above code with the following

And the name=”typeFrontendMap” is the cache code that is unique.

  1. Now press ctrl+s or select the save option from the menu of your editor and exit the editor.
  2. Lastly, to check if you have done the steps right or not, you have to verify that. Yes, of course we have given the verification steps in this tutorial too which are at the end.
  • Using the Custom Cache Frontend

In this method you must alter the <your Magento 2 install dir>/ app / etc / env.php. This type of cache still results in some of the objects to be cached in the file system, but as compared to file system caching very few assets are cached. To alter the env.php just perform the steps below and you will surely learn how to do so.

  1. First, you need to login to the Magento server or simply switch to the Magento file system owner.
  2. Make copy of php by entering the following commands shown below:

  1. Now open up the file in text editor and copy paste the following anywhere.

  1. Now press ctrl+s or select the save option from the menu of your editor and exit the editor.
  2. Lastly, to check if you have done the steps write or not you have to verify that. Yes, of course we have given the verification steps in this tutorial too which is at the end.

Testing Database Cache

By testing the database cache, you will make sure that the data is being written on the database and not to the file system. However, before doing that, first clear the current cache directories and then go to any cacheable page on the web browser. Perform the steps below to verify.

  1. First, you need to login to the Magento server or simply switch to the Magento file system owner.
  2. Clear the current cache directories. The command is:

rm -rf <your Magento 2 install dir>/var/cache/* <your Magento 2 install dir>/var/page_cache/* <your Magento 2 install dir>/var/di/* <your Magento 2 install dir>/var/generation/*

  1. In the web browser, go to the storefront front door page. If you will see the exceptions then verify xml syntax and then try again. But to see the exceptions you need to enable the developer mode.
  2. Now enter the following commands:

  1. Check if both the directories are empty, if they are not then try editing xml again and correct any issues.
  2. Now check your phpMyAdmin panel to verify if all the data is in the cache and cache_tag If you perform the query to show all of the data from Cache and cache_tag table and the data is shown, then you have a positive result.

Final Thoughts

This tutorial gave a walk through of two methods of setting up database caching. What you are waiting for? Start caching you store’s database and give your customers relief from being annoyed ;)

About Author

Fayyaz is a Magento Community Manager at Cloudways - A Managed Magento Hosting Platform. He writes about Magento Tutorials at Magenticians as well as share his knowledge with the Magento Community. Fayyaz is a food lover and enjoys driving. You can email him at m.fayyaz@cloudways.com