Caching your views to improve the performance of your PrestaShop Modules

In this post I will talk about a very important topic in the development of PrestaShop modules: how to cache your template files to improve the performance of the modules. The relevance of this topic is even greater when we talk about Front-Office modules, because a bad written module may have serious impact in the loading time of the shop. The good news is that is very easy to use the PrestaShop own caching system.

As you may already know, all the HTML of the shop is (or should be) generated through some template language (usually smarty). As they are template languages, they must be compiled for the HTML to be generated. If no cache is used, every time a page is accessed, several template files have to be compiled, even if they didn’t change since the last compilation. This is a waste of server resources, and also makes the page to take more time to be load than it would be necessary. Thins get even worse if several database queries are performed to get the data used in the template.

As has already been said, the caching system of PrestaShop is very easy to be used, and you just have to include a few lines of code to your module. Let’s suppose that your module insert some content in the  Front-Office through the displayShoppingCartFooter hook. So, your code will be something like

When this function is executed, if there is a cached version of the template ‘mytemplate.tpl’, PrestaShop will automatically use the cached version, instead of recompile it. If your template didn’t depends of the $data variable, we wouldn’t need do to nothing more. However, as the value of the shopping cart changes, the HTML generated by this template may change as well. This makes necessary the use of some way to distinguish between different cached versions of the same template file.

These different versions may be produced using the third argument of the method Module::display(), named $cache_id, as the following code:

Now, we have different cached versions of the template file, and each version is indexed by the shopping cart value. It is a good practice to use the method Module::getCacheId(), which returns a string with the following information:

  • Whether the shop was accessed using secure connection or not;
  • ID of the shop (if using multishop);
  • ID of client group which the user who accessed the shop belongs to;
  • Language in use by the client;
  • Currency in use by the client;
  • Country in use by the client.

Then, a very good cache_id may be obtained like the following code.

Note that the variable $data is not used when the template is already in cache, because the template will not be compiled. So, if the template is in cache, the method MyModel::getSomeDbData() shouldn’t be called. Then, we can save some CPU resources with the following code.

Finally, the cache invalidation may be done with the method Module::_clearCache(). If we want to clear every cached version of our template file whenever any object of the class MyModel is updated, we may use the hook actionObjectMyModelUpdateAfter. This hook is called by PrestaShop whenever the method update() is executed with success in a object of the class MyModel (which must extends the PrestaShop class ObjectModel).

The method Module::_clearCache() also accepts the argument $cache_id, allowing us to clear the cache of a single version of our template file.

Leave a Reply

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