类别产品列表块缓存改进
-
16-10-2019 - |
题
我重写了用于缓存问题的目录产品列表块。请查看代码并提供建议,以改善大量产品清单类别页面的缓存和缓存验证问题。谁对这样的缓存块有更好的配置?
class Ssd_Fix_Block_Catalog_Product_List extends Mage_Catalog_Block_Product_List
{
const CACHE_TAG = 'CATEGORY_PRODUCTS';
public function getCacheKey()
{
$url = md5(Mage::app()->getRequest()->getOriginalRequest()->getRequestUri());
if (!$this->_cache_key) {
$key = "CATEGORY_PRODUCTS_" . $url;
$this->_cache_key = $key;
}
return $this->_cache_key;
}
public function getCacheTags()
{
$data = array(self::CACHE_TAG);
if ($category = Mage::registry('current_category')) {
$data[] = Mage_Catalog_Model_Category::CACHE_TAG . "_" . $category->getId();
}
if (count($products = $this->getProductList())) {
foreach ($products as $p) {
$data[] = Mage_Catalog_Model_Product::CACHE_TAG . "_" . $p->getId();
}
}
return $data;
}
public function getCacheLifetime()
{
return 60 * 60 * 24;
}
//other methods here
}
解决方案
最好覆盖 getCacheKeyInfo()
方法而不是 getCacheKey()
.
public function getCacheKeyInfo()
{
$info = parent::getCacheKeyInfo();
// Add any additional data you want
$info[] = Mage::app()->getRequest()->getOriginalRequest()->getRequestUri();
return $info;
}
值的哈希自动照顾。
这将包括当前商店视图,主题和使用的模板等重要数据。
通常,请求URI不被认为是缓存关键目的的理想选择,因为有许多因素可能影响URI中未包含的块输出。
例如,如果块输出包含价格,请确保包括客户组ID(链接到客户的 tax_class_id
)在“缓存密钥信息数组”中。
请求URI也可能有几个涉及同一页面的排列,例如带有尾随 /和没有。它通常会导致更好的结果手动构建缓存键阵列。
对于缓存标签,请确保还包括所有显示产品的缓存标签(已在1.8/1.13中完成)。
我还建议您除了调整块缓存外使用完整的缓存。
其他提示
大型产品清单类别页面
也许是确定性的算法 Monte Carlo
是一个很好的起点。例如,您可以使用概率(10% * iShishardTodoFactor)缓存。在使用普通的iShardTodoFactor显示了10次产品后,您很有可能会尝试缓存。如果仅显示一次或两次(测试时),则填充缓存的机会很低。另外,使TTL有点随机,您不希望在同一时间同时出现很多缓存条目。