Skip to content

Instantly share code, notes, and snippets.

@jacquesbh
Last active October 10, 2015 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacquesbh/3646203 to your computer and use it in GitHub Desktop.
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)
<?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
<?php
$counter = new \Jbh\Counter;
echo $counter->getCountLessThan(3), ",";
echo $counter->getCountLessThan(10);
// Affiche : 2,9
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
<?php
$tags = ['foo'];
Mage::app()->cleanCache($tags);
<?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
<?php
$block->getCacheKey();
$block->getCacheLifetime();
$block->getCacheTags();
<?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;
}
/* ... */
}
<?php
abstract class Mage_Core_Block_Abstract extends Varien_Object
{
/* ... */
public function getCacheKeyInfo()
{
return array(
$this->getNameInLayout()
);
}
/* ... */
}
<?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()
);
}
<?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;
}
/* ... */
}
<?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;
}
}
<?php
public function getCacheLifetime()
{
return 36000; // 10h
}
<?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;
}
<?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