Read More
Menu Close

    Improve Module Performance With PrestaShop Cache

    Nowadays speed and performance is integral part of software development. When we talk about speed and performance, the first thing to talk about is cache. PrestaShop utilises various type of cache to manage its speed performance. Here we will explain how to use cache to improve module performance.

    PrestaShop Cache Settings

    In Performance tab in PrestaShop Backoffice, you can see two types of cache:

    • Smarty Cache
    Improve performance
    • Caching (File-system/DB Query cache)
    Improve performance

    These cache settings are available to use in module also. We will discuss one by one with example on how to use each type in module.

    Improve module performance using Smarty Cache

    This setting creates cache for templates available in PrestaShop and modules. When this smarty cache is available, you do not need to perform all operations to find values for your smarty template. In other words, If you do not utilise smarty cache, your module code always executes to assign smarty variables. This happens even when the smarty cache is available.

    So, we need to check if the smarty cache is available for the template then return the cache without calculating and assigning smarty variables. The “Module” class has functions to check and get smarty cache for module templates. Lets understand this with an example:

    Start your headless eCommerce
    now.
    Read More
    • First, check if cache is available. If yes, then return from cache directly. If not available, only then calculate the smarty variables and assign.
    /* 
    * $templateFile is Path to template to return
    * Must start with "module:" followed by template path in 1.7.x.x 
    * And just template name in 1.6.x.x like "$templateFile = 'yourtemplate.tpl';"
    */
    
    $templateFile = 'module:'.$this->name. '/views/templates/hook/yourtemplate.tpl';
    if (!$this->isCached($templateFile, $this->getCacheId())) {
        $this->smarty->assign($this->getWidgetVariables($hookName, $configuration));
    }
    
    return $this->fetch($templateFile, $this->getCacheId());

    Improve module performance using PrestaShop Caching

    Just like the smarty cache, you can improve module code performance by using PrestaShop Cache. You can create cache of the results from most frequent Db requests so that these request are not called from Db again and again. Instead, the result is fetched from Db only once, then cached and next time fetched from cache only.

    To use this cache, we will use Cache class, it automatically uses the configured cache option (memcache/xcache/APC etc) in the PrestaShop cache settings. We also provide Redis Cache module which adds another option “Redis” in this list.

    However, we need to create cache key ourselves, it means we have to be very careful so that the cache key does not match with any other key in the PrestaShop system or module. We will understand with an example:

    • First of all check if the cache key is already stored.
    • If not stored, then execute DB request and store the result in cache with this cache key.
    • Otherwise, fetch from cache itself.
    /*
    * Best way to create unique cache key is to add ClassName+methodName followed by method arguments
    * Append module name after cache key to reliably make it unique
    * This is just example, you can use any other pattern to create your own unique Cache Key
    */
    class MyClass {
        public function myMethodName($arg1, $arg2)
        {
            $cacheKey = 'MyClass::myMethodName_'.$arg1.'_'.$arg2.'_mymodule';
            if (!Cache::isStored($cacheKey) {
                $sql = 'sql query to fetch result from database';
                $result = Db::getInstance()->executeS($sql);
                Cache::store($cacheKey, $result);
            }
            return Cache::retrieve($cacheKey);
        }
    }

    It is important to keep in mind that the cache is used to reduce time of execution of very frequent processes. So we must create best cache structure skilfully, so that there is no over-caching in the module. The best way to avoid over-caching is to only apply Cache in those method which are frequently used in a single request.

    That’s all about using cache to improve module performance. If any issue or doubt in the above process, please feel free to let us know in the comment section.

    I would be happy to help.

    Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.

    For any doubt contact us at [email protected].

    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    Be the first to comment.

    Back to Top