tarek fellah
tarek fellah

Reputation: 367

Prestashop clear cache which folders affected

I searched a lot for this question and i didn't found the correct answer for Prestashop 1.6, i have made a script to clear Prestashop cache, smarty cache, I got the code from adminPerformances controller,

Tools::clearSmartyCache();
Tools::clearXMLCache();
Media::clearCache();
Tools::generateIndex();

I read that it cleared the cache from /cache/smarty/cache, but when executing the script or clicking in clear cache in performances page it doesn't remove subfolders from this folder. Any body knows 'clear cache' will affect which folders/files.

Thanks.

Upvotes: 3

Views: 5297

Answers (4)

Mirgen
Mirgen

Reputation: 170

Prestashop 1.7 (tested on 1.7.6.1)

  • widely proposed solutions does not remove smarty template cache (files under _PS_ROOT_DIR_ . '/var/cache/' . $env . '/')

To correctly reproduce the functionality of the "Clear cache" button in Admin > Advanced Parameters > Performance, it should be like this:

include('../../../config/config.inc.php');
include('../../../init.php');


Tools::clearAllCache(); // <---- this is the trick
Tools::clearXMLCache();
Media::clearCache();
Tools::generateIndex();

What I was missing in other answers is to be calling Tools::clearSf2Cache() which is called from inside Tools::clearAllCache(); in my example above.

Tools::clearSf2Cache() removes also files under _PS_ROOT_DIR_ . '/var/cache/' . $env . '/'

So if you want to also remove smarty template FILES, go with my answer

Background/explanation:

"Clear cache" button calls PerformanceController::clearCacheAction() inside it calls CacheClearerChain::clear() which calls these:

  • PrestaShop\PrestaShop\Adapter\Cache\Clearer\SymfonyCacheClearer
  • PrestaShop\PrestaShop\Adapter\Cache\Clearer\SmartyCacheClearer
  • PrestaShop\PrestaShop\Adapter\Cache\Clearer\XmlCacheClearer
  • PrestaShop\PrestaShop\Adapter\Cache\Clearer\MediaCacheClearer
  • PrestaShop\PrestaShop\Adapter\Cache\Clearer\ClassIndexCacheClearer

And this is basically the same as my proposed solution above.

Upvotes: 0

Knowband Plugins
Knowband Plugins

Reputation: 1317

As per our knowledge the only folder that is affected while clearing cache from the admin panel.

/cache/smarty/compile

Please let us know if we are wrong.

Upvotes: 0

sadlyblue
sadlyblue

Reputation: 2987

Even with no cache option on it still creates cache files in the fowllowing folders:

/cache/smarty/cache
/cache/smarty/compile

I think that Smarty_Internal_Utility::clearCompiledTemplate should delete those files. Which is called by Tools::clearSmartyCache()

But anyway, what bugs me the most, is even with no cache option on and force compile, most times I need to manually clear the cache. Normally by deleting the folders I mentioned above (it's faster, specially on a virtual machine locally). This issue is still a "bug" in 1.7.

Speaking of 1.7, the cache is in /app/cache, and there is one for dev and one for prod. where it's even cached translations, class_index.php, and a lot of more stuff than in 1.6 cache.

Upvotes: 0

Florian Lemaitre
Florian Lemaitre

Reputation: 5748

Prestashop uses a system called Lazy Cache.

Here is the clearAllCache and clearCache methods of /classes/SmartyCustom classe:

public function clearAllCache($exp_time = null, $type = null)
{
    Db::getInstance()->execute('REPLACE INTO `'._DB_PREFIX_.'smarty_last_flush` (`type`, `last_flush`) VALUES (\'template\', FROM_UNIXTIME('.time().'))');
    return $this->delete_from_lazy_cache(null, null, null);
}

public function clearCache($template_name, $cache_id = null, $compile_id = null, $exp_time = null, $type = null)
{
    return $this->delete_from_lazy_cache($template_name, $cache_id, $compile_id);
}

public function delete_from_lazy_cache($template, $cache_id, $compile_id)
{
    if (!$template) {
        return Db::getInstance()->execute('TRUNCATE TABLE `'._DB_PREFIX_.'smarty_lazy_cache`', false);
    }

    $template_md5 = md5($template);
    $sql          = 'DELETE FROM `'._DB_PREFIX_.'smarty_lazy_cache`
                        WHERE template_hash=\''.pSQL($template_md5).'\'';

    if ($cache_id != null) {
        $sql .= ' AND cache_id LIKE "'.pSQL((string)$cache_id).'%"';
    }

    if ($compile_id != null) {
        if (strlen($compile_id) > 32) {
            $compile_id = md5($compile_id);
        }
        $sql .= ' AND compile_id="'.pSQL((string)$compile_id).'"';
    }
    Db::getInstance()->execute($sql, false);
    return Db::getInstance()->Affected_Rows();
}

As you can see, cache files are indexed in database under table smarty_lazy_cache. And the cache file is never deleted, only unindexed from the table.

Upvotes: 1

Related Questions