Categoría de la lista de productos de mejora bloque de caché
-
16-10-2019 - |
Pregunta
Me reescribir bloque de la lista de productos de catálogo para el almacenamiento en caché tema. Por favor código de revisión y dan sus consejos para mejorar el almacenamiento en caché y problemas de la caché-validación de la lista de productos enorme páginas de categorías. ¿Quién tiene mejor configuración para el almacenamiento en caché bloques como este?
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
}
Solución
It would be better to override the getCacheKeyInfo()
method instead of getCacheKey()
.
public function getCacheKeyInfo()
{
$info = parent::getCacheKeyInfo();
// Add any additional data you want
$info[] = Mage::app()->getRequest()->getOriginalRequest()->getRequestUri();
return $info;
}
The hashing of the value is taken care of automatically.
This will take care important data like the current store view, the theme and the used template are included.
Generally the request URI is not considered ideal for cache key purposes, since there are many factors that could influence the blocks output that are not contained in the URI.
For example, if the block output contains prices, be sure to include the customer group id (linked to the customer's tax_class_id
) in the cache key info array.
There can also be several permutations of the request URI that all refer to the same page, for example with a trailing / and without. It generally leads to better results to build the cache key array manually.
For the cache tags, be sure to include the cache tags of all displayed products, too (already done in 1.8/1.13).
I'd also recommend using a full page cache in addition to tuning block caching.
Otros consejos
huge product listing category pages
Maybe a deterministic algorithm like Monte Carlo
is a good starting point. For example you can cache with a probability of (10% * isThisHardToDoFactor) . After showing the product 10 times with a normal isThisHardToDoFactor, you have a high chance that it will try to cache. If it's only showed once or twice (while testing), it has a low chance to fill up your cache. Also make the TTL a bit random, you don't want a lot of cache entries expires at exactly the same time.