Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve layout cache efficiency by de-duplicating XML strings. #104

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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