Ecommerce Blog - The #1 Source for Ecommerce News, Tips and Trends!

Get ecommerce updates, news, and insider tips from the experts at PrestaShop!

Ecommerce Blog - The #1 Source for Ecommerce News, Tips and Trends!
 

< Back to the Blog home

Guest Blogger Series: How to Utilize Smarty Cache

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 <> function (responsible for displaying a TPL file) will make a copy of the file and place it in the cache for storage. If you enable the cache feature, PrestaShop will not generate a new TPL file (longer loading time) but will instead display the cache that is already stored! (shorter loading time)

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 property. This value represents a lifetime in seconds; when set to -1 will force the cache to never expire.

$ 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> = 1.5.4.1) or
cache / smarty / cache / file_cache.tpl.php (if Version <1.5.4.1)

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:

  • filename
  • 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.

SC Module

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);

SC Profiling

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.

SC No cache

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.

SC No cache

Conclusion: It can be seen a reduction of the loading time of pages with only 3 to 4 rows more in your module code!

  1. Author: gcomandini

    Date: June 14, 2013 at 12:45 pm

    hello,

    I have prestashop 1.4.6.2, but I cannot locate the file “cache / smarty / cache / file_cache.tpl.php”

    thanks,

    Gianluca

    • Author: benjamin utterback

      Date: June 17, 2013 at 4:05 pm

      Hi Gianluca,

      You won’t actually find the “file_cache.tpl.php” file inside those folders. The “file_cache” is simply a placeholder name for whichever file is included in those folders. It may be a module or theme name. The reason why it is added as “file_cache” is to show you where the smarty files are. For example, instead of “file_cache” it may be blockuserinfo.tpl.php

      This article was written for PrestaShop v1.5. The Smarty Cache is found differently in v1.4

      In v1.4 you can view the Smarty Cache in
      /tools/smarty/cache

      /tools/smarty/compile

      If you are using Smarty v2 you will find it under
      /tools/smarty_v2/cache

      /tools/smarty_v2/compile

      I hope this helps answer your question. Thanks for reading!

      -Benjamin

  2. Author: coworker

    Date: June 17, 2013 at 12:04 am

    Hi,

    I don’t know how to report bugs, so here I am.

    When 1.5.4.1 is run in Catalog mode, the Cart block (top right hand side) disappears but then everything else moves up so the Logo overlaps the Top horizontal menu and buttons disappears.

    Please help
    Coworker

  3. Author: coworker

    Date: June 18, 2013 at 12:06 am

    Hi Benjamin,

    Any idea to when the next release?
    Also are there any plans to include the choice to show the price when software is run in catalog mode?

    Coworker

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>