Skip to content

Instantly share code, notes, and snippets.

@mklooss
Last active December 11, 2015 00:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mklooss/4517079 to your computer and use it in GitHub Desktop.
Save mklooss/4517079 to your computer and use it in GitHub Desktop.
Magento Product Cache Loading by Johann Reinke, added "isAdmin" http://www.johannreinke.com/en/2012/04/13/magento-how-to-cache-product-loading/
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Catalog>
<version>0.1.0</version>
</Namespace_Catalog>
</modules>
<global>
<models>
<catalog>
<rewrite>
<product>Namespace_Catalog_Model_Product</product>
</rewrite>
</catalog>
</models>
<cache>
<types>
<product_loading>
<label>Product Loading</label>
<description>Cache of product attributes loading.</description>
<tags>catalog_product</tags>
</product_loading>
</types>
</cache>
</global>
<default>
<core>
<cache>
<lifetime>3600</lifetime>
</cache>
</core>
</default>
</config>
<?php
/**
* @see http://www.johannreinke.com/en/2012/04/13/magento-how-to-cache-product-loading/
*/
class Namespace_Catalog_Model_Product
extends Mage_Catalog_Model_Product
{
public function load($id, $field = null)
{
if(Mage::app()->getStore()->isAdmin())
{
return parent::load($id, $field = null);
}
if (null !== $field || !Mage::app()->useCache('product_loading')) {
return parent::load($id, $field);
}
// Caching product data
Varien_Profiler::start(__METHOD__);
$storeId = (int) $this->getStoreId();
$cacheId = "product-$id-$storeId";
if ($cacheContent = Mage::app()->loadCache($cacheId)) {
$data = unserialize($cacheContent);
if (!empty($data)) {
foreach ($data as $key => &$value){
$this->$key = $value;
}
unset($value);
}
} else {
parent::load($id);
// You can call some heavy methods here
try {
$cacheContent = serialize(get_object_vars($this));
$tags = array(
Mage_Core_Model_Store::CACHE_TAG,
Mage_Core_Model_Store::CACHE_TAG.'_'.Mage::app()->getStore()->getStoreId(),
Mage_Core_Model_Website::CACHE_TAG,
Mage_Core_Model_Website::CACHE_TAG.'_'.Mage::app()->getStore()->getWebsiteId(),
Mage_Catalog_Model_Product::CACHE_TAG,
Mage_Catalog_Model_Product::CACHE_TAG.'_'.$id,
);
$lifetime = Mage::getStoreConfig('core/cache/lifetime');
Mage::app()->saveCache($cacheContent, $cacheId, $tags, $lifetime);
} catch (Exception $e) {
// Exception = no caching
Mage::logException($e);
}
}
Varien_Profiler::stop(__METHOD__);
return $this;
}
}
public function load($id, $field = null)
{
if(Mage::app()->getStore()->isAdmin())
{
return parent::load($id, $field = null);
}
if (null !== $field || !Mage::app()->useCache('product_loading')) {
return parent::load($id, $field);
}
// Caching product data
Varien_Profiler::start(__METHOD__);
$storeId = (int) $this->getStoreId();
$cacheId = "product-$id-$storeId";
if ($cacheContent = Mage::app()->loadCache($cacheId)) {
$data = unserialize($cacheContent);
if (!empty($data)) {
foreach ($data as $key => &$value){
$this->$key = $value;
}
unset($value);
}
} else {
parent::load($id);
// You can call some heavy methods here
try {
$cacheContent = serialize(get_object_vars($this));
$tags = array(
Mage_Catalog_Model_Product::CACHE_TAG,
Mage_Catalog_Model_Product::CACHE_TAG . '_' . $id
);
$lifetime = Mage::getStoreConfig('core/cache/lifetime');
Mage::app()->saveCache($cacheContent, $cacheId, $tags, $lifetime);
} catch (Exception $e) {
// Exception = no caching
Mage::logException($e);
}
}
Varien_Profiler::stop(__METHOD__);
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment