【问题标题】:Prestashop clear cache which folders affectedPrestashop 清除缓存受影响的文件夹
【发布时间】:2017-02-08 10:55:18
【问题描述】:

我为这个问题搜索了很多,但我没有找到 Prestashop 1.6 的正确答案,我制作了一个脚本来清除 Prestashop 缓存,smarty 缓存, 我从 adminPerformances 控制器获得了代码,

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

我读到它从 /cache/smarty/cache 中清除了缓存,但是在执行脚本或单击性能页面中的清除缓存时,它不会从该文件夹中删除子文件夹。 任何人都知道“清除缓存”会影响哪些文件夹/文件。

谢谢。

【问题讨论】:

  • 在我的情况下(PS 1.6.1.6),它只删除了一些文件夹而不是所有文件夹。在cache/smarty/compile,你看last_flush在你调用Tools::clearSmartyCache();时是否更新了

标签: php caching smarty prestashop prestashop-1.6


【解决方案1】:

Prestashop 使用一个称为 Lazy Cache 的系统。

这是/classes/SmartyCustom类的clearAllCacheclearCache方法:

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

如您所见,缓存文件在数据库中的表smarty_lazy_cache 下被索引。并且缓存文件永远不会被删除,只会从表中取消索引。

【讨论】:

  • 这是给mysql还是文件系统缓存?
  • 用于 Smarty 文件系统缓存。正如您在表 smaty_lazy_cache 中看到的,filepath 列包含缓存文件的路径。
  • 我看到了表,它是空的,我不小心删除了 cache/smarty/ 文件夹并重新创建了它,但它总是空的你有解决办法吗?谢谢
【解决方案2】:

即使没有缓存选项,它仍然会在以下文件夹中创建缓存文件:

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

我认为Smarty_Internal_Utility::clearCompiledTemplate 应该删除这些文件。由Tools::clearSmartyCache()调用

但无论如何,最让我烦恼的是,即使没有打开缓存选项并强制编译,大多数时候我都需要手动清除缓存。通常通过删除我上面提到的文件夹(它更快,特别是在本地虚拟机上)。 这个问题在 1.7 中仍然是一个“错误”。

说到 1.7,缓存在/app/cache,其中一个用于开发,一个用于生产。它甚至缓存了翻译、class_index.php 以及比 1.6 缓存更多的东西。

【讨论】:

  • 正如我在问题中所写,我尝试了 Tools::clearSmartyCache();但它没有清除 /cache/smarty/cache 和 /cache/smarty/compile 子文件夹
【解决方案3】:

据我们所知,从管理面板清除缓存时唯一受影响的文件夹。

/cache/smarty/compile

如果我们错了,请告诉我们。

【讨论】:

    【解决方案4】:

    Prestashop 1.7(在 1.7.6.1 上测试)

    • 广泛提出的解决方案不删除 smarty 模板缓存_PS_ROOT_DIR_ . '/var/cache/' . $env . '/' 下的文件)

    要正确重现管理>高级参数>性能中“清除缓存”按钮的功能,应该是这样的:

    include('../../../config/config.inc.php');
    include('../../../init.php');
    
    
    Tools::clearAllCache(); // <---- this is the trick
    Tools::clearXMLCache();
    Media::clearCache();
    Tools::generateIndex();
    

    我在其他答案中缺少的是调用Tools::clearSf2Cache(),在上面的示例中,它是从Tools::clearAllCache(); 内部调用的。

    Tools::clearSf2Cache() 也删除_PS_ROOT_DIR_ . '/var/cache/' . $env . '/' 下的文件

    因此,如果您还想删除 smarty 模板文件,请按照我的答案进行操作

    背景/说明:

    “清除缓存”按钮调用PerformanceController::clearCacheAction(),其中调用CacheClearerChain::clear(),它调用这些:

    • 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

    这与我上面提出的解决方案基本相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-29
      • 2020-07-07
      • 2023-03-31
      • 2017-07-07
      • 2012-04-25
      • 2016-06-12
      相关资源
      最近更新 更多