Skip to content

Commit

Permalink
Refactor to a single cache item request
Browse files Browse the repository at this point in the history
  • Loading branch information
John Knowles committed May 30, 2014
1 parent 8774fd6 commit c930e68
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 109 deletions.
42 changes: 13 additions & 29 deletions src/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,33 @@ class Jh_DesignExceptionsFix_Model_Observer extends Enterprise_PageCache_Model_O
/**
* Checks whether exists design exception value in cache.
* If not, gets it from config and puts into cache
* @todo Condense the following into a single cache file
* @return Enterprise_PageCache_Model_Observer
*/
protected function _saveDesignException()
{
if (!$this->isCacheEnabled()) {
return $this;
}
$cacheId = Enterprise_PageCache_Model_Processor::DESIGN_EXCEPTION_KEY;

$exception = Enterprise_PageCache_Model_Cache::getCacheInstance()->load($cacheId);
if (!$exception) {
$exception = Mage::getStoreConfig(self::XML_PATH_DESIGN_EXCEPTION);
Enterprise_PageCache_Model_Cache::getCacheInstance()->save($exception, $cacheId);
$this->_processor->refreshRequestIds();
}
Varien_Profiler::start('FPC DESIGN EXCEPTIONS FIX:'.__METHOD__);

$cacheId = Jh_DesignExceptionsFix_Model_PageCache_Processor::DESIGN_SKIN_EXCEPTION_KEY;

$exception = Enterprise_PageCache_Model_Cache::getCacheInstance()->load($cacheId);
if (!$exception) {
$exception = Mage::getStoreConfig(self::XML_PATH_DESIGN_SKIN_EXCEPTION);
Enterprise_PageCache_Model_Cache::getCacheInstance()->save($exception, $cacheId);
$cacheId = Enterprise_PageCache_Model_Processor::DESIGN_EXCEPTION_KEY;
if (!Enterprise_PageCache_Model_Cache::getCacheInstance()->getFrontend()->test($cacheId)) {
$exception = array(
'package' => @unserialize(Mage::getStoreConfig(parent::XML_PATH_DESIGN_EXCEPTION)),
'skin' => @unserialize(Mage::getStoreConfig(self::XML_PATH_DESIGN_SKIN_EXCEPTION)),
'template' => @unserialize(Mage::getStoreConfig(self::XML_PATH_DESIGN_TEMPLATE_EXCEPTION)),
'layout' => @unserialize(Mage::getStoreConfig(self::XML_PATH_DESIGN_LAYOUT_EXCEPTION))
);
$exception = serialize($exception);
Enterprise_PageCache_Model_Cache::getCacheInstance()
->save($exception, $cacheId, array(Enterprise_PageCache_Model_Processor::CACHE_TAG));
$this->_processor->refreshRequestIds();
}

$cacheId = Jh_DesignExceptionsFix_Model_PageCache_Processor::DESIGN_TEMPLATE_EXCEPTION_KEY;

$exception = Enterprise_PageCache_Model_Cache::getCacheInstance()->load($cacheId);
if (!$exception) {
$exception = Mage::getStoreConfig(self::XML_PATH_DESIGN_TEMPLATE_EXCEPTION);
Enterprise_PageCache_Model_Cache::getCacheInstance()->save($exception, $cacheId);
$this->_processor->refreshRequestIds();
}

$cacheId = Jh_DesignExceptionsFix_Model_PageCache_Processor::DESIGN_LAYOUT_EXCEPTION_KEY;
Varien_Profiler::stop('FPC DESIGN EXCEPTIONS FIX:'.__METHOD__);

$exception = Enterprise_PageCache_Model_Cache::getCacheInstance()->load($cacheId);
if (!$exception) {
$exception = Mage::getStoreConfig(self::XML_PATH_DESIGN_LAYOUT_EXCEPTION);
Enterprise_PageCache_Model_Cache::getCacheInstance()->save($exception, $cacheId);
$this->_processor->refreshRequestIds();
}
return $this;
}
}
90 changes: 10 additions & 80 deletions src/Model/PageCache/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,103 +5,33 @@
*/
class Jh_DesignExceptionsFix_Model_PageCache_Processor extends Enterprise_PageCache_Model_Processor
{

/**
* Skin design exception cache key
*/
const DESIGN_SKIN_EXCEPTION_KEY = 'FPC_DESIGN_SKIN_EXCEPTION_CACHE';

/**
* Template design exception cache key
*/
const DESIGN_TEMPLATE_EXCEPTION_KEY = 'FPC_DESIGN_TEMPLATE_EXCEPTION_CACHE';

/**
* Layout design exception cache key
*/
const DESIGN_LAYOUT_EXCEPTION_KEY = 'FPC_DESIGN_LAYOUT_EXCEPTION_CACHE';

/**
* FPC_THEME_COOKIE
*/
const FPC_THEME_COOKIE = 'FPC_THEME';

/**
* Override of the _getDesignPackage() method to accommodate for design exceptions
* @todo Condense the cache request to a single file
* @return string
*/
protected function _getDesignPackage()
{
Varien_Profiler::start('FPC DESIGN EXCEPTIONS FIX:'.__METHOD__);
/** @todo implement a cookie to reduce the amount of load required currently the getPath within
* the cookie model is preventing the cookie from being saved without Magento init */
/*if ($theme = $this->_getCookie()->get(self::FPC_THEME_COOKIE)) {
return $theme;
}*/

$packageArray = array();

$cacheInstance = Enterprise_PageCache_Model_Cache::getCacheInstance();

$exceptionKeys = array(
$this::DESIGN_EXCEPTION_KEY,
self::DESIGN_SKIN_EXCEPTION_KEY,
self::DESIGN_TEMPLATE_EXCEPTION_KEY,
self::DESIGN_LAYOUT_EXCEPTION_KEY
);

foreach ($exceptionKeys as $key) {
$test = $cacheInstance->getFrontend()->test($key);
if ($test!=false) {
$this->_designExceptionExistsInCache = $test;
break;
}
}

//Package
$exceptions = $cacheInstance->load($this::DESIGN_EXCEPTION_KEY);
$exceptions = $cacheInstance->load(Enterprise_PageCache_Model_Processor::DESIGN_EXCEPTION_KEY);
$this->_designExceptionExistsInCache = $cacheInstance->getFrontend()->test(Enterprise_PageCache_Model_Processor::DESIGN_EXCEPTION_KEY);
if ($exceptions) {
$rules = @unserialize($exceptions);
if (!empty($rules)) {
$packageArray['package'] = Mage_Core_Model_Design_Package::getPackageByUserAgent($rules);
foreach ($rules as $key=>$value) {
if (!empty($value)) {
$packageArray[$key] = Mage_Core_Model_Design_Package::getPackageByUserAgent($value);
}
}
}

//Skin
$exceptions = $cacheInstance->load($this::DESIGN_SKIN_EXCEPTION_KEY);
if ($exceptions) {
$rules = @unserialize($exceptions);
if (!empty($rules)) {
$packageArray['skin'] = Mage_Core_Model_Design_Package::getPackageByUserAgent($rules);
}
}

//Templates
$exceptions = $cacheInstance->load($this::DESIGN_TEMPLATE_EXCEPTION_KEY);
if ($exceptions) {
$rules = @unserialize($exceptions);
if (!empty($rules)) {
$packageArray['template'] = Mage_Core_Model_Design_Package::getPackageByUserAgent($rules);
}
}

//Layout
$exceptions = $cacheInstance->load($this::DESIGN_LAYOUT_EXCEPTION_KEY);
if ($exceptions) {
$rules = @unserialize($exceptions);
if (!empty($rules)) {
$packageArray['layout'] = Mage_Core_Model_Design_Package::getPackageByUserAgent($rules);
}
}


$packageExceptions = implode('', $packageArray);
if ($packageExceptions) {
/** @todo implement a cookie to reduce the amount of load required currently the getPath within
* the cookie model is preventing the cookie from being saved without Magento init */
//$this->_getCookie()->set(self::FPC_THEME_COOKIE, $packageExceptions, time()+604800);
return $packageExceptions;
}
return '';

Varien_Profiler::stop('FPC DESIGN EXCEPTIONS FIX:'.__METHOD__);
return $packageExceptions;
}
}

0 comments on commit c930e68

Please sign in to comment.