Magento 缓存机制简析

在知道缓存机制前,首先需要知道,Magento的路由机制,这边就不做赘述了,百度一大堆。

下面一个简单的缓存生效流程:

A:首先在页面开始时,
Magento在appcodecoreMageCoreModelApp.php的run函数里

//可以看到一个判断条件,根据请求地址,判断是否有缓存命中,若中,则直接返回缓存
if ($this->_cache->processRequest()) {
            $this->getResponse()->sendResponse();
        } else {
//生成modules,config的缓存
$this->_initModules();
//生成translate的缓存
       $this->loadAreaPart(Mage_Core_Model_App_Area::AREA_GLOBAL, Mage_Core_Model_App_Area::PART_EVENTS); if ($this->_config->isLocalConfigLoaded()) { $scopeCode = isset($params['scope_code']) ? $params['scope_code'] : ''; $scopeType = isset($params['scope_type']) ? $params['scope_type'] : 'store'; $this->_initCurrentStore($scopeCode, $scopeType); $this->_initRequest(); Mage_Core_Model_Resource_Setup::applyAllDataUpdates(); } $this->getFrontController()->dispatch(); }

之后经由路由匹配分发,进入控制器,在控制器里,会加载layout层

C:Layout层的缓存在Mage_Core_Controller_Varien_Action类的loadLayoutUpdates里

//调用Mage_Core_Model_Layout_Update类的load函数
public function load($handles=array())
    {
        if (is_string($handles)) {
            $handles = array($handles);
        } elseif (!is_array($handles)) {
            throw Mage::exception('Mage_Core', Mage::helper('core')->__('Invalid layout update handle'));
        }

        foreach ($handles as $handle) {
            $this->addHandle($handle);
        }

        if ($this->loadCache()) {
            return $this;
        }

        foreach ($this->getHandles() as $handle) {
            $this->merge($handle);
        }

        $this->saveCache();
        return $this;
    }

经由Layout加载block,同时输出其对应的template。


D:View层的缓存是在Block抽象类:Mage_Core_Block_Abstract的toHtml()里

Mage::dispatchEvent('core_block_abstract_to_html_before', array('block' => $this));
        if (Mage::getStoreConfig('advanced/modules_disable_output/' . $this->getModuleName())) {
            return '';
        }
//简单明了,页面渲染出来前,加载是否有缓存
        $html = $this->_loadCache();
        if ($html === false) {
            $translate = Mage::getSingleton('core/translate');
            /** @var $translate Mage_Core_Model_Translate */
            if ($this->hasData('translate_inline')) {
                $translate->setTranslateInline($this->getData('translate_inline'));
            }

            $this->_beforeToHtml();
            $html = $this->_toHtml();
//在页面生成后,同时保存缓存
            $this->_saveCache($html);

            if ($this->hasData('translate_inline')) {
                $translate->setTranslateInline(true);
            }
        }

E:最后Model层的缓存则比较有意思,

首先是Model的抽象类Mage_Core_Model_Abstract里的

//可以看到,每当model类执行保存和删除操作时,都会执行清楚缓存操作
    protected function _afterSave()
    {
        $this->cleanModelCache();
        Mage::dispatchEvent('model_save_after', array('object'=>$this));
        Mage::dispatchEvent($this->_eventPrefix.'_save_after', $this->_getEventData());
        return $this;
    }

    protected function _beforeDelete()
    {
        Mage::dispatchEvent('model_delete_before', array('object'=>$this));
        Mage::dispatchEvent($this->_eventPrefix.'_delete_before', $this->_getEventData());
        $this->cleanModelCache();
        return $this;
    }

上面是清除缓存操作(购物车更新,下订单之类,都会触发这个),那么Model层的缓存是在哪里加入的?

Mage_Core_Model_Resource_Db_Collection_Abstract抽象类的getData()会触发数据缓存保存:

还有一个就是Mage_Eav_Model_Entity_Collection_Abstract类的load()操作,

这两个类都继承自Varien_Data_Collection_Db,会调用其_fetchAll()

 //被以上两个函数调用
    protected function _fetchAll($select)
    {
        if ($this->_canUseCache()) {
            $data = $this->_loadCache($select);
            if ($data) {
                $data = unserialize($data);
            } else {
                $data = $this->getConnection()->fetchAll($select, $this->_bindParams);
                $this->_saveCache($data, $select);
            }
        } else {
            $data = $this->getConnection()->fetchAll($select, $this->_bindParams);
        }
        return $data;
    }

 比对下magento后台的几种缓存:

原文地址:https://www.cnblogs.com/cangzhou/p/6344410.html