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

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

A:首先在页面开始时,
Magento在app\code\core\Mage\Core\Model\App.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后台的几种缓存:

Magento 缓存机制简析的更多相关文章

  1. Linux VFS机制简析(二)

    Linux VFS机制简析(二) 接上一篇Linux VFS机制简析(一),本篇继续介绍有关Address space和address operations.file和file operations. ...

  2. Linux VFS机制简析(一)

    Linux VFS机制简析(一) 本文主要基于Linux内核文档,简单分析Linux VFS机制,以期对编写新的内核文件系统(通常是给分布式文件系统编写内核客户端)的场景有所帮助. 个人渊源 切入正文 ...

  3. Linux内存管理机制简析

    Linux内存管理机制简析 本文对Linux内存管理机制做一个简单的分析,试图让你快速理解Linux一些内存管理的概念并有效的利用一些管理方法. NUMA Linux 2.6开始支持NUMA( Non ...

  4. Binder机制简析(三)

    注册Service Service组件运行在Server进程中,首先要将Service注册到Service Manager中,再启动一个Binder线程池来等待和处理Client的通信请求. 注册过程 ...

  5. Unity - 存读档机制简析

    本文旨在于简要分析Unity中的两种存档机制,即:PlayerPrefs数据持久化方法及Serialization数据序列化方法 较比与源项目,我另加了JSON方法.XML方法等及一些Unity设置, ...

  6. Linux内核poll/select机制简析

    0 I/O多路复用机制 I/O多路复用 (I/O multiplexing),提供了同时监测若干个文件描述符是否可以执行IO操作的能力. select/poll/epoll 函数都提供了这样的机制,能 ...

  7. DPDK多核多线程机制简析

    DPDK通过在多核设备上,创建多个线程,每个线程绑定到单独的核上,减少线程调度的开销,以提高性能. DPDK的线程分为控制线程和数据线程,控制线程一般绑定到MASTER核上,主要是接受用户配置,并传递 ...

  8. linux-2.6.38poll机制简析(以tiny6410按键中断程序为基础)

    一.应用程序 /* struct pollfd { int fd; //文件描述符 short events; //表示请求检测的事件 short revents; //表示检测之后返回的事件 }; ...

  9. 简析 .NET Core 构成体系

    简析 .NET Core 构成体系 Roslyn 编译器 RyuJIT 编译器 CoreCLR & CoreRT CoreFX(.NET Core Libraries) .NET Core 代 ...

随机推荐

  1. Python 学习资料分享

    有同学需要学习 Python,确实,随着人工智能被炒的火热,再加上大数据时代,作为程序员的我们,怎么可能坐得住,必须尝尝鲜,给自己增加一项技能,增加自己的竞争了. 内容定位 这方面的学习资料比较多,本 ...

  2. 正则_action

    http://wiki.ubuntu.org.cn/index.php?title=Python%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%93% ...

  3. 最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)

    \(Greatest Common Increasing Subsequence\) 大致题意:给出两个长度不一定相等的数列,求其中最长的公共的且单调递增的子序列(需要具体方案) \(solution ...

  4. .Net之路(十四)com组件、OLEDB导入EXCEL

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/chenfanglincfl/article/details/30546777 .NET com组件 ...

  5. 百度dureos CMake Error

    CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage CMake Error: CMAKE_CXX_COMPILER not set, ...

  6. WinDbg 查看静态变量

    有如下Class.若想查看静态变量内容.因为静态变量和类绑定,仅需要查看类即可. namespace ConsoleApplication13 { class Program { public sta ...

  7. mongodb c++ driver安装踩坑记

    安装教程:https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/ (1) “initializer_list” fil ...

  8. 探索Oracle11gR2 之 DataGuard 三种保护模式

    Oracle的DataGuard技术有三种实现模式,分别是max performance.max availability.maxprotection这三种模式. 以下是来自Oracle文档的摘要信息 ...

  9. 第一次往github上传文件步骤

    第一次往github上传文件步骤: 1> 从右上角 '+' 位置下拉菜单中,创建一个repository 2>从右上角头像位置下拉菜单 setting中设置 SSH keys 3>打 ...

  10. linux--安装phpcurl扩展

    在UBUNTU中直接用APT包管理工具安装: apt-get install curl libcurl3 libcurl3-dev php5-curl 安装好后重启Apache服务器就行了,如果还是不 ...