Jump to content

How to clear Smarty cache just for specific template (module). 1.6


Recommended Posts

My site runs automatic daily cron tasks that activate/deactivate some product values, as "On sale", directly on the database tables. I've noticed that some modules, for example homefeatured or new products in home page don't reflect those changes if I don't clear the cache.

 

I know I can clear the cache manually from Backoffice (not a practical solution), or as some topics show, I can resolve it with a cron task using:

 

Tools::clearSmartyCache();

 

That works fine when you need to clear the whole site cache, but as I only need to clear the cache for those 2 specific templates: Home featured & Home new products, clearing all the cache seems unnecesary.

 

How can I use the Tools::clearSmartyCache(); to clear just some specific templates? I can't find any documentation regarding this.

 

Based on an older topic I've tried:

 

// clear only cache for index.tpl
$smarty->clear_cache('homefeatured.tpl');  --> Does nothing on my Prestashop 1.6.0.9

 

&

 

Tools::clearSmartyCache('homefeatured.tpl'); ---> Clears all the cache not just the template.

 

Any help will be appreciated.

 

Thanks

Link to comment
Share on other sites

Hi,

 

I've checked the  _clearCache method on Module.php

 

/*************/

protected function _clearCache($template, $cache_id = null, $compile_id = null)
    {
        Tools::enableCache();
        if ($cache_id === null)
            $cache_id = $this->name;
        $number_of_template_cleared = Tools::clearCache(Context::getContext()->smarty, $this->getTemplatePath($template), $cache_id, $compile_id);
        Tools::restoreCacheSettings();

        return $number_of_template_cleared;
    }

/*************/

 

But I can no imagine how to translate this to a php cron task. I was expecting something more simple like the way with: Tools::clearSmartyCache();

 

Thanks for your recommendation.

Link to comment
Share on other sites

Based on Krystian's suggestion, finally I've managed to create this script as a cron task in my root folder:

 

////////////////////////////////

require(dirname(__FILE__).'/config/config.inc.php');
    
    if (!Tools::enableCache())
    echo "enable Cache failed"; // To control if ends ok

    if (!Tools::clearCache(Context::getContext()->smarty, getTemplatePath('blocknewproducts_home.tpl'), null, null))
    echo "clearCache failed"; // To control if ends ok

    if (!Tools::restoreCacheSettings())   
    echo "restoreCache failed"; // To control if ends ok
//////////////////////

 

When executed no changes are made to the blocknewproducts_home results and I receive the: "enableCache failed" custom control warning message.

 

Any ideas what's going wrong?

 

Thanks

Link to comment
Share on other sites

  • 3 years later...
  • 5 years later...

have you found a working solution for your CRON ? I have the exact same issue. Clearing the whole cache makes my website returning 500 error for 20 seconds when I only need to flush those 2 home modules.. 😕

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...