By Adonis Karavokyros, alias PrestaRocket
This article is the fifth in PrestaShop Guest Blogger series. Its content was created by PrestaShop expert Adonis Karavokyros, a valued Community Member.
We hope you enjoy this edition as we learn how to utilize Smarty Cache in PrestaShop!
What is compilation and cache?
We are always looking to optimize the response times of our PrestaShop stores, using the cache and compile system is a great way to do this. This article explains the functions and use of Smarty Cache. This is simple to implement and a very effective technique that we apply at PrestaRocket.
Quick clarification before we begin: compilation and caching are different. Compilation converts a template (.Dwt) in PHP while the caching mechanism stores the final result for faster loading times. When your store is live, Smarty cache must be enabled and the option “Never recompile the template files” should be selected (Advanced Settings -> Performance) in your Back Office.
1) How to use Smarty Cache
First, you should know that this function only works if the cache is enabled
This seems obvious, but it is better to clarify.
To enable/disable Smarty Cache, use the following parameters:
Smarty :: CACHING_LIFETIME_CURRENT(or 1)
(Allows you to assign a lifetime variable common to all cached files)
Smarty :: CACHING_LIFETIME_SAVED (or 2)
(Allows you to assign a value at the time the cache was generated. This gives you detailed, granular control over when that particular cache expires)
Set to 0 to disable the cache.
$ Smarty-> setCaching (Smarty CACHING_LIFTIME_CURRENT);
Once the cache is enabled, the <
As you might have guessed by the name of the variables above, the cached files have a lifetime. You can set and edit this lifetime yourself by changing the <
$ Smarty-> setCaching (Smarty CACHING_LIFTIME_SAVED);
$ Smarty-> cache_lifetime = 3600;
In case you were wondering, you can find the cache files via this PrestaShop route
cache / smarty / cache / module_name / file_cache.tpl.php (if version> = 220.127.116.11) or
cache / smarty / cache / file_cache.tpl.php (if Version <18.104.22.168)
2) Example use of the cache with the Editorial Module
The operation of this module is quite simple:
- Creates 2 tables in the database module installation
- Displays a form with different fields that are stored in the tables module
- Displays template on the hook “home”
How does the cache work in this module?
A display on the home page is executed via hook, “displayHome”.
The isCached() function checks if template is already in cache. It takes as the template name as the parameter and the id of the cache file generated in turn by getCacheId() function.
if (! $ this-> isCached ('editorial.tpl', $ this-> getCacheId ()))
If no parameter is passed into the function $ this-> getCacheId (), the id of the cache file is composed of the following elements:
- the id of the store (allows to have two cache files if you have two shops)
- the id of the client group if it is connected
- the id of the language
The cache id will be integrated in the name of the cache file.
To summarize, if you have 2 shops with 5 customer groups and 3 languages, 30 cache files
can be generated (5 x 3 x 2)
a) The cache file does not exist
In our Homepage Editor module, if the template has not been cached, then the following steps are performed:
- A new EditorialClass a object is created (which calls to the database)
- variables are passed to the template ($ this-> smarty-> assign …)
Finally, the cache file is created with the display function through the third not null parameter (id cache) this function.
b) The cache file exists
In this case, the above steps are not processed (new object EditorialClass etc..) and PrestaShop will not generate a new TPL file but will directly display the cache.
Finally, with each change to the configuration of our module, the postProcess() function updates the information in the database and cache files are deleted with the
function _clearCache() that takes as a parameter the name of the template.
3) Comparison with or without cache
Take a simple module, it loads 2 data in the configuration table and a table created for our module. It also changes one of these data. We have 2 loads of data already in memory and 3 queries in data base. It’s not a heavy module but you will notice that the difference is still significant with caching.
We measure the execution time with PrestaShop profiling tool. (If you want to enable the profiling tool in your PrestaShop, edit the file ‘config/defines.inc.php’ and edit the value of profiling to line 44)
define (‘_PS_DEBUG_PROFILING_’, true);
Results obtained before caching: our module is displayed in the left column, we are therefore interested in loading the “displayLeftColumn”. On 100 loads, it gets an average of 114 ms.
Results obtained after caching: on 100 loads, it gets an average of 55 ms. We note also that the number of SQL queries declined by 3: caching has ignored our changes and use data already stored.
Conclusion: It can be seen a reduction of the loading time of pages with only 3 to 4 rows more in your module code!
Receive articles like this in your inbox!
Subscribe to get the latest ecommerce news and tips sent directly to your email. Subscribe