Skip to content

Commit

Permalink
[impr-OpenMage#104] Improve layout cache efficiency by de-duplicating…
Browse files Browse the repository at this point in the history
… XML strings
  • Loading branch information
colinmollenhour authored and edannenberg committed Sep 19, 2018
1 parent 5abff40 commit 29e2ead
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion app/code/core/Mage/Core/Model/Layout/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class Mage_Core_Model_Layout_Update
*/
const LAYOUT_GENERAL_CACHE_TAG = 'LAYOUT_GENERAL_CACHE_TAG';

/**
* Prefix used for actual XML storage (unprefixed is just the sha1 hash)
*/
const XML_KEY_PREFIX = 'XML_';

/**
* Layout Update Simplexml Element Class Name
*
Expand Down Expand Up @@ -182,6 +187,13 @@ public function loadCache()
return false;
}

// The cache key is just a hash of the real content to de-duplicate the often large XML strings
if (strlen($result) === 40) { // sha1
if (!$result = Mage::app()->loadCache(self::XML_KEY_PREFIX . $result)) {
return false;
}
}

$this->addUpdate($result);

return true;
Expand All @@ -194,8 +206,17 @@ public function saveCache()
}
$str = $this->asString();
$tags = $this->getHandles();

// Cache key is sha1 hash of actual XML string
$hash = sha1($str);
$tags[] = self::LAYOUT_GENERAL_CACHE_TAG;
return Mage::app()->saveCache($str, $this->getCacheId(), $tags, null);
Mage::app()->saveCache($hash, $this->getCacheId(), $tags, null);

// Only save actual XML to cache if it doesn't already exist
if ( ! Mage::app()->getCache()->test(self::XML_KEY_PREFIX . $hash)) {
Mage::app()->saveCache($str, self::XML_KEY_PREFIX . $hash, $tags, null);
}
return TRUE;
}

/**
Expand Down

0 comments on commit 29e2ead

Please sign in to comment.