Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save code-poel/cce4d6972806eb49d43212c2a9973d3a to your computer and use it in GitHub Desktop.
Save code-poel/cce4d6972806eb49d43212c2a9973d3a to your computer and use it in GitHub Desktop.
Fixes a Magento Enterprise performance bug related to the enterprise_index_refresh cron and Enterprise PageCache.
<?php
/**
* This core over-ride is the result of a BUG in the Enterprise method of
* clearing it's PageCache. During the Mview reindexing process, a changelog
* of IDs is determined. This array contains entity IDs that have been updated
* since the last reindex and contains many duplicates. When passed to
* the PageCache methods to clear the cache, this array of IDs is not de-duped
* which can result in hundreds of thousands of needless queries when bulk
* updates are made.
*
* This code simply removes duplicate entries from this process thus very much
* decreasing pagecache clearing times (which, with bulk updates can take
* upwards of an hour). This drops that to something like 2 minutes, max when
* dealing with large datasets.
*
* Class Veep_Patches_Model_PageCache_Observer_Index
*/
class Veep_Patches_Model_PageCache_Observer_Index
extends Enterprise_PageCache_Model_Observer_Index
{
/**
* {@inheritdoc}
*
* Overridden to account for getCacheIdTags returning false.
*
* @param Mage_Core_Model_Abstract $entity
* @param array $ids
*/
protected function _cleanEntityCache(Mage_Core_Model_Abstract $entity, array $ids)
{
$cacheTags = array();
foreach ($ids as $entityId) {
$entity->setId($entityId);
$cache_id_tags = $entity->getCacheIdTags();
if (false === $cache_id_tags) {
$cache_id_tags = [];
}
$cacheTags = array_unique(array_merge($cacheTags, $cache_id_tags));
}
if ( ! empty($cacheTags)) {
Enterprise_PageCache_Model_Cache::getCacheInstance()->clean($cacheTags);
}
}
/**
* {@inheritdoc}
*
* Overridden to account for getCacheIdTagsWithCategories returning false.
*
* @param Mage_Core_Model_Abstract $entity
* @param array $ids
*/
protected function _cleanProductsCache(Mage_Core_Model_Abstract $entity, array $ids)
{
$cacheTags = array();
foreach ($ids as $entityId) {
$entity->setId($entityId);
$cache_id_tags_with_categories = $entity->getCacheIdTagsWithCategories();
if (false === $cache_id_tags_with_categories) {
$cache_id_tags_with_categories = [];
}
$cacheTags = array_unique(array_merge($cacheTags, $cache_id_tags_with_categories));
}
if ( ! empty($cacheTags)) {
Enterprise_PageCache_Model_Cache::getCacheInstance()->clean($cacheTags);
}
}
/**
* @param Varien_Event_Observer $observer
*/
public function cleanProductsCacheAfterPartialReindex(Varien_Event_Observer $observer)
{
$entityIds = $observer->getEvent()->getData('product_ids');
// Start fix
$newProductIds = array_unique($entityIds);
// End fix
if (is_array($newProductIds) && ! empty($newProductIds)) {
Mage::log(sprintf(
'[ %s ] Found [ %u ] distinct Product IDs out of the original [ %u ]',
date('h:i:s', time()),
count($newProductIds),
count($entityIds)
), null, 'veep_fixes.log');
$this->_cleanProductsCache(Mage::getModel('catalog/product'), $newProductIds);
}
}
/**
* @param Varien_Event_Observer $observer
*/
public function cleanCategoriesCacheAfterPartialReindex(Varien_Event_Observer $observer)
{
$entityIds = $observer->getEvent()->getData('category_ids');
// Start fix
$newEntityIds = array_unique($entityIds);
// End fix
if (is_array($entityIds) && ! empty($entityIds)) {
Mage::log(sprintf(
'[ %s ] Found [ %u ] distinct Category IDs out of the original [ %u ]',
date('h:i:s', time()),
count($newEntityIds),
count($entityIds)
), null, 'veep_fixes.log');
$this->_cleanEntityCache(Mage::getModel('catalog/category'), $entityIds);
Enterprise_PageCache_Model_Cache::getCacheInstance()->clean(Mage_Catalog_Model_Category::CACHE_TAG);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment