Smartyはキャッシュの寿命を処理できますが、Prestashopはキャッシュライフを使用する方法を提案していません。私たちは、この機能を追加するModule.php
とTools.php
クラスをオーバーライドする必要があります
:
Tools.php
:デフォルトでは
<?php
class Tools extends ToolsCore
{
public static function enableCache($level = 1, Context $context = null, $lifetime = null)
{
if (!$context) {
$context = Context::getContext();
}
$smarty = $context->smarty;
if (!Configuration::get('PS_SMARTY_CACHE')) {
return;
}
if ($smarty->force_compile == 0 && $smarty->caching == $level) {
return;
}
self::$_forceCompile = (int)$smarty->force_compile;
self::$_caching = (int)$smarty->caching;
$smarty->force_compile = 0;
$smarty->caching = (int)$level;
// If there is a lifetime provided then set the cache_lifetime to this value
$smarty->cache_lifetime = is_null($lifetime) ? 31536000 : (int) $lifetime; // 1 Year
}
}
を、PrestaShopのは1年にキャッシュライフタイムを設定します。ここでは、私たち自身のライフタイムを定義するための第3のパラメータを追加しました。
Module.php
:
ここ
<?php
class Module extends ModuleCore {
/**
* @param string $template
* @param null|string $cache_id
* @param null|string $compile_id
* @param int|null $lifetime cache life time
* @return bool
*/
public function isCached($template, $cache_id = null, $compile_id = null, $lifetime = null)
{
if (Tools::getIsset('live_edit') || Tools::getIsset('live_configurator_token')) {
return false;
}
Tools::enableCache(1, null, $lifetime);
$new_tpl = $this->getTemplatePath($template);
$is_cached = $this->getCurrentSubTemplate($template, $cache_id, $compile_id)->isCached($new_tpl, $cache_id, $compile_id);
Tools::restoreCacheSettings();
return $is_cached;
}
}
我々は、上記のTools.php
クラスに変更されたenableCache
方法で使用されるisCached()
メソッドに新しいパラメータを追加します。
ファイルcache/class_index.php
を削除して、Prestashopが新しく作成されたオーバーライドを読み込むようにする必要があります。
現在、当社のフックにこの新しいパラメータを使用することができます。
protected function _prepareHook()
{
if (!$this->isCached('homeslider.tpl', $this->getCacheId(), null, 3600))
{
$slides = $this->getSlides(true);
if (is_array($slides))
foreach ($slides as &$slide)
{
$slide['sizes'] = @getimagesize((dirname(__FILE__).DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.$slide['image']));
if (isset($slide['sizes'][3]) && $slide['sizes'][3])
$slide['size'] = $slide['sizes'][3];
}
if (!$slides)
return false;
$this->smarty->assign(array('homeslider_slides' => $slides));
}
return true;
}