Last active
October 10, 2015 06:08
-
-
Save jacquesbh/3646203 to your computer and use it in GitHub Desktop.
[blog] Le cache des blocks sur Magento (http://jacques.sh/le-cache-des-blocks-sur-magento.html)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Jbh; | |
class Counter | |
{ | |
use \Jbh\Db; | |
protected $_cache = []; | |
protected $_cacheKey = 'jbh_counter_sql_result'; | |
public function getCountLessThan($id) | |
{ | |
$sql = "SELECT COUNT(`id`) AS n FROM `table` WHERE `id` < %d;"; | |
$key = sprintf($this->_cacheKey, $id); | |
if (!isset($this->_cache[$key])) { | |
$this->_cache[$key] = $this->query(sprintf($sql, $id))->fetchOne('n'); | |
} | |
return $this->_cache[$key]; | |
} | |
} | |
$counter = new \Jbh\Counter; | |
echo $counter->getCountLessThan(3), ","; | |
echo $counter->getCountLessThan(10); | |
// Affiche : 2,2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$counter = new \Jbh\Counter; | |
echo $counter->getCountLessThan(3), ","; | |
echo $counter->getCountLessThan(10); | |
// Affiche : 2,9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Cache A: | |
key => cache_a | |
tags => foo, bar, baz | |
lifetime => false | |
Cache B: | |
key => cache_b | |
tags => foo, bar | |
lifetime => 10 | |
Cache C: | |
key => cache_c | |
tags => baz, qux | |
lifetime => 62400 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$tags = ['foo']; | |
Mage::app()->cleanCache($tags); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$tags = ['baz', 'qux']; | |
Mage::app() | |
->getCache() | |
->getFrontend() | |
->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, $tags); | |
// Constantes à utiliser pour nettoyer le cache de manière conditionnelle : | |
// Zend_Cache::CLEANING_MODE_ALL = 'all'; | |
// Zend_Cache::CLEANING_MODE_OLD = 'old'; | |
// Zend_Cache::CLEANING_MODE_MATCHING_TAG = 'matchingTag'; | |
// Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG = 'notMatchingTag'; | |
// Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG = 'matchingAnyTag'; <=== Par défaut sur Magento |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$block->getCacheKey(); | |
$block->getCacheLifetime(); | |
$block->getCacheTags(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class Mage_Core_Block_Abstract extends Varien_Object | |
{ | |
/* ... */ | |
public function getCacheKey() | |
{ | |
if ($this->hasData('cache_key')) { | |
return $this->getData('cache_key'); | |
} | |
/** | |
* don't prevent recalculation by saving generated cache key | |
* because of ability to render single block instance with different data | |
*/ | |
$key = $this->getCacheKeyInfo(); | |
//ksort($key); // ignore order | |
$key = array_values($key); // ignore array keys | |
$key = implode('|', $key); | |
$key = sha1($key); | |
return $key; | |
} | |
/* ... */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class Mage_Core_Block_Abstract extends Varien_Object | |
{ | |
/* ... */ | |
public function getCacheKeyInfo() | |
{ | |
return array( | |
$this->getNameInLayout() | |
); | |
} | |
/* ... */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function getCacheKeyInfo() | |
{ | |
// Utilisation d'index pour pouvoir faire un array_merge au besoin ;) | |
// Par exemple si ce tableau est en partie dans un Helper pour faciliter les développement ? | |
return array( | |
'name_in_layout' => $this->getNameInLayout(), | |
'store' => Mage::app()->getStore()->getId(), | |
'design_package' => Mage::getDesign()->getPackageName(), | |
'design_theme' => Mage::getDesign()->getTheme('template'), | |
// 'object_id' => $this->getObjectId() | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class Mage_Core_Block_Abstract extends Varien_Object | |
{ | |
/* ... */ | |
public function getCacheTags() | |
{ | |
if (!$this->hasData('cache_tags')) { | |
$tags = array(); | |
} else { | |
$tags = $this->getData('cache_tags'); | |
} | |
$tags[] = self::CACHE_GROUP; | |
return $tags; | |
} | |
/* ... */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Jbh_Demo_Model_Foo extends Mage_Core_Model_Abstract | |
{ | |
const CACHE_TAG = 'FOO'; | |
public function getCacheTags() | |
{ | |
$tags = parent::getCacheTags(); | |
array_push( | |
$tags, | |
Jbh_Demo_Model_Foo::CACHE_TAG, | |
Jbh_Demo_Model_Foo::CACHE_TAG . '_' . $this->getObjectId() | |
); | |
return $tags; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function getCacheLifetime() | |
{ | |
return 36000; // 10h | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function saveCache($data, $id, $tags=array(), $lifeTime=false) | |
{ | |
$this->_cache->save($data, $id, $tags, $lifeTime); | |
Mage::log($tags, null, 'tags.log', true); | |
return $this; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function cleanCache($tags=array()) | |
{ | |
$this->_cache->clean($tags); | |
Mage::log($tags, null, 'tags.log', true); | |
Mage::dispatchEvent('application_clean_cache', array('tags' => $tags)); | |
return $this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment