Exemplo n.º 1
0
 /**
  * Request processing action
  */
 public function processAction()
 {
     $processor = Mage::getSingleton('ves_optimize/processor');
     $content = Mage::registry('cached_page_content');
     $containers = Mage::registry('cached_page_containers');
     $cacheInstance = Ves_Optimize_Model_Cache::getCacheInstance();
     foreach ($containers as $container) {
         $container->applyInApp($content);
     }
     $this->getResponse()->appendBody($content);
     // save session cookie lifetime info
     $cacheId = $processor->getSessionInfoCacheId();
     $sessionInfo = $cacheInstance->load($cacheId);
     if ($sessionInfo) {
         $sessionInfo = unserialize($sessionInfo);
     } else {
         $sessionInfo = array();
     }
     $session = Mage::getSingleton('core/session');
     $cookieName = $session->getSessionName();
     $cookieInfo = array('lifetime' => $session->getCookie()->getLifetime(), 'path' => $session->getCookie()->getPath(), 'domain' => $session->getCookie()->getDomain(), 'secure' => $session->getCookie()->isSecure(), 'httponly' => $session->getCookie()->getHttponly());
     if (!isset($sessionInfo[$cookieName]) || $sessionInfo[$cookieName] != $cookieInfo) {
         $sessionInfo[$cookieName] = $cookieInfo;
         // customer cookies have to be refreshed as well as the session cookie
         $sessionInfo[Ves_Optimize_Model_Cookie::COOKIE_CUSTOMER] = $cookieInfo;
         $sessionInfo[Ves_Optimize_Model_Cookie::COOKIE_CUSTOMER_GROUP] = $cookieInfo;
         $sessionInfo[Ves_Optimize_Model_Cookie::COOKIE_CUSTOMER_LOGGED_IN] = $cookieInfo;
         $sessionInfo[Ves_Optimize_Model_Cookie::CUSTOMER_SEGMENT_IDS] = $cookieInfo;
         $sessionInfo[Ves_Optimize_Model_Cookie::COOKIE_MESSAGE] = $cookieInfo;
         $sessionInfo = serialize($sessionInfo);
         $cacheInstance->save($sessionInfo, $cacheId, array(Ves_Optimize_Model_Processor::CACHE_TAG));
     }
 }
Exemplo n.º 2
0
 /**
  * Saves informational cache, containing parameters used to show poll.
  *
  * @param array $renderedParams
  * @return Ves_Optimize_Model_Container_Sidebar_Poll
  */
 protected function _saveInfoCache($renderedParams)
 {
     $data = serialize($renderedParams);
     $id = $this->_getInfoCacheId();
     $tags = array(Ves_Optimize_Model_Processor::CACHE_TAG);
     Ves_Optimize_Model_Cache::getCacheInstance()->save($data, $id, $tags);
     return $this;
 }
Exemplo n.º 3
0
 /**
  * Clean cache by specified entity and its ids
  *
  * @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);
         $cacheTags = array_merge($cacheTags, $entity->getCacheIdTags());
     }
     if (!empty($cacheTags)) {
         Ves_Optimize_Model_Cache::getCacheInstance()->clean($cacheTags);
     }
 }
Exemplo n.º 4
0
 /**
  * Retrieve encryption salt
  *
  * @return null|sting
  */
 protected function _getSalt()
 {
     if ($this->_salt === null) {
         $saltCacheId = 'full_page_cache_key';
         $this->_salt = Ves_Optimize_Model_Cache::getCacheInstance()->load($saltCacheId);
         if (!$this->_salt) {
             $this->_salt = md5(microtime() . rand());
             Ves_Optimize_Model_Cache::getCacheInstance()->save($this->_salt, $saltCacheId, array(Ves_Optimize_Model_Processor::CACHE_TAG));
         }
     }
     return $this->_salt;
 }
Exemplo n.º 5
0
 /**
  * Saves informational cache, containing parameters used to show banners.
  * We don't use _saveCache() method internally, because it replaces sid in cache, that can be done only
  * after app is started, while this method can be called without app after rendering serie/shuffle banners.
  *
  * @param array $renderedParams
  * @return Ves_Optimize_Model_Container_Banner
  */
 protected function _saveInfoCache($renderedParams)
 {
     $data = serialize($renderedParams);
     $id = $this->_getInfoCacheId();
     $tags = array(Ves_Optimize_Model_Processor::CACHE_TAG);
     $lifetime = $this->_placeholder->getAttribute('cache_lifetime');
     if (!$lifetime) {
         $lifetime = false;
     }
     Ves_Optimize_Model_Cache::getCacheInstance()->save($data, $id, $tags, $lifetime);
     return $this;
 }
Exemplo n.º 6
0
 /**
  * Cache instance static getter
  *
  * @return Mage_Core_Model_Cache
  */
 public static function getCacheInstance()
 {
     if (is_null(self::$_cache)) {
         $options = Mage::app()->getConfig()->getNode('global/full_page_cache');
         if (!$options) {
             self::$_cache = Mage::app()->getCacheInstance();
             return self::$_cache;
         }
         $options = $options->asArray();
         foreach (array('backend_options', 'slow_backend_options') as $tag) {
             if (!empty($options[$tag]['cache_dir'])) {
                 $options[$tag]['cache_dir'] = Mage::getBaseDir('var') . DS . $options[$tag]['cache_dir'];
                 Mage::app()->getConfig()->getOptions()->createDirIfNotExists($options[$tag]['cache_dir']);
             }
         }
         self::$_cache = Mage::getModel('core/cache', $options);
     }
     return self::$_cache;
 }
Exemplo n.º 7
0
 /**
  * Prepare response body before caching
  *
  * @param Zend_Controller_Response_Http $response
  * @return string
  */
 public function prepareContent(Zend_Controller_Response_Http $response)
 {
     $cacheInstance = Ves_Optimize_Model_Cache::getCacheInstance();
     /** @var Ves_Optimize_Model_Processor */
     $processor = Mage::getSingleton('ves_optimize/processor');
     $countLimit = Mage::getStoreConfig(Mage_Reports_Block_Product_Viewed::XML_PATH_RECENTLY_VIEWED_COUNT);
     // save recently viewed product count limit
     $cacheId = $processor->getRecentlyViewedCountCacheId();
     if (!$cacheInstance->getFrontend()->test($cacheId)) {
         $cacheInstance->save($countLimit, $cacheId, array(Ves_Optimize_Model_Processor::CACHE_TAG));
     }
     // save current product id
     $product = Mage::registry('current_product');
     if ($product) {
         $cacheId = $processor->getRequestCacheId() . '_current_product_id';
         $cacheInstance->save($product->getId(), $cacheId, array(Ves_Optimize_Model_Processor::CACHE_TAG));
         $processor->setMetadata(self::METADATA_PRODUCT_ID, $product->getId());
         Ves_Optimize_Model_Cookie::registerViewedProducts($product->getId(), $countLimit);
     }
     return parent::prepareContent($response);
 }
Exemplo n.º 8
0
 /**
  * Save data to cache storage. Store many block instances in one cache record depending on additional cache ids.
  *
  * @param string $data
  * @param string $id
  * @param array $tags
  * @param null|int $lifetime
  * @return Ves_Optimize_Model_Container_Advanced_Abstract
  */
 protected function _saveCache($data, $id, $tags = array(), $lifetime = null)
 {
     $additionalCacheId = $this->_getAdditionalCacheId();
     if (!$additionalCacheId) {
         Mage::throwException(Mage::helper('ves_optimize')->__('Additional id should not be empty'));
     }
     $tags[] = Ves_Optimize_Model_Processor::CACHE_TAG;
     $tags = array_merge($tags, $this->_getPlaceHolderBlock()->getCacheTags());
     if (is_null($lifetime)) {
         $lifetime = $this->_placeholder->getAttribute('cache_lifetime') ? $this->_placeholder->getAttribute('cache_lifetime') : false;
     }
     Ves_Optimize_Helper_Data::prepareContentPlaceholders($data);
     $result = array();
     $cacheRecord = parent::_loadCache($id);
     if ($cacheRecord) {
         $cacheRecord = json_decode($cacheRecord, true);
         if ($cacheRecord) {
             $result = $cacheRecord;
         }
     }
     $result[$additionalCacheId] = $data;
     Ves_Optimize_Model_Cache::getCacheInstance()->save(json_encode($result), $id, $tags, $lifetime);
     return $this;
 }
Exemplo n.º 9
0
 /**
  * Save data to cache storage
  *
  * @param string $data
  * @param string $id
  * @param array $tags
  * @param null|int $lifetime
  * @return Ves_Optimize_Model_Container_Abstract
  */
 protected function _saveCache($data, $id, $tags = array(), $lifetime = null)
 {
     $tags[] = Ves_Optimize_Model_Processor::CACHE_TAG;
     $tags = array_merge($tags, $this->_getPlaceHolderBlock()->getCacheTags());
     if (is_null($lifetime)) {
         $lifetime = $this->_placeholder->getAttribute('cache_lifetime') ? $this->_placeholder->getAttribute('cache_lifetime') : false;
     }
     Ves_Optimize_Helper_Data::prepareContentPlaceholders($data);
     Ves_Optimize_Model_Cache::getCacheInstance()->save($data, $id, $tags, $lifetime);
     return $this;
 }
Exemplo n.º 10
0
 /**
  * Clean cached tags for product if tags for product are saved
  *
  * @param Varien_Event_Observer $observer
  */
 public function cleanCachedProductTagsForTags(Varien_Event_Observer $observer)
 {
     if (!$this->isCacheEnabled()) {
         return;
     }
     /** @var $tagModel Mage_Tag_Model_Tag */
     $tagModel = $observer->getEvent()->getDataObject();
     $productCollection = $tagModel->getEntityCollection()->addTagFilter($tagModel->getId());
     /** @var $product Mage_Catalog_Model_Product */
     foreach ($productCollection as $product) {
         Ves_Optimize_Model_Cache::getCacheInstance()->clean($product->getCacheTags());
     }
 }
Exemplo n.º 11
0
 /**
  * Save rendered block content to cache storage
  *
  * @param string $blockContent
  * @param array $tags
  * @return Ves_Optimize_Model_Container_Abstract
  */
 public function saveCache($blockContent, $tags = array())
 {
     $blockCacheId = $this->_getBlockCacheId();
     if ($blockCacheId) {
         $categoryCacheId = $this->_getCategoryCacheId();
         if ($categoryCacheId) {
             $categoryUniqueClasses = '';
             $classes = array();
             $classesCount = preg_match_all('/< *li[^>]*class *= *["\']?([^"\']*)/i', $blockContent, $classes);
             for ($i = 0; $i < $classesCount; $i++) {
                 $classAttribute = $classes[0][$i];
                 $classValue = $classes[1][$i];
                 if (false === strpos($classAttribute, 'active')) {
                     continue;
                 }
                 $classInactive = preg_replace('/\\s+active|active\\s+|active/', '', $classAttribute);
                 $blockContent = str_replace($classAttribute, $classInactive, $blockContent);
                 $matches = array();
                 if (preg_match('/(?<=\\s|^)nav-.+?(?=\\s|$)/', $classValue, $matches)) {
                     $categoryUniqueClasses .= ($categoryUniqueClasses ? ' ' : '') . $matches[0];
                 }
             }
             $this->_saveCache($categoryUniqueClasses, $categoryCacheId);
         }
         if (!Ves_Optimize_Model_Cache::getCacheInstance()->getFrontend()->test($blockCacheId)) {
             $this->_saveCache($blockContent, $blockCacheId, $tags);
         }
     }
     return $this;
 }
Exemplo n.º 12
0
 /**
  * Get shared info param
  *
  * @param string|null $key
  * @return mixed
  */
 protected function _getSharedParam($key = null)
 {
     $placeholderName = $this->_placeholder->getName();
     $info = self::$_sharedInfoData[$placeholderName]['info'];
     if (is_null($info)) {
         $info = array();
         $cacheRecord = Ves_Optimize_Model_Cache::getCacheInstance()->load($this->_getCacheId());
         if ($cacheRecord) {
             $cacheRecord = json_decode($cacheRecord, true);
             if ($cacheRecord && array_key_exists($this->_getInfoCacheId(), $cacheRecord)) {
                 $info = $cacheRecord[$this->_getInfoCacheId()];
             }
         }
         self::$_sharedInfoData[$placeholderName]['info'] = $info;
     }
     return isset($key) ? isset($info[$key]) ? $info[$key] : null : $info;
 }
Exemplo n.º 13
0
 /**
  * Load cache metadata from storage
  */
 protected function _loadMetadata()
 {
     if ($this->_metaData === null) {
         $cacheMetadata = Ves_Optimize_Model_Cache::getCacheInstance()->load($this->getRequestCacheId() . self::METADATA_CACHE_SUFFIX);
         if ($cacheMetadata) {
             $cacheMetadata = unserialize($cacheMetadata);
         }
         $this->_metaData = empty($cacheMetadata) || !is_array($cacheMetadata) ? array() : $cacheMetadata;
     }
 }
Exemplo n.º 14
0
 /**
  * Retrieves cache instance
  *
  * @return Mage_Core_Model_Cache
  */
 protected function _getCacheInstance()
 {
     return Ves_Optimize_Model_Cache::getCacheInstance();
 }