//Mage_Core_Model_Config
public function saveCache($tags=array())
{
if (!Mage::app()->useCache('config')) {
return $this;
}
//为缓存打标签 保存的时候可以打标签
if (!in_array(self::CACHE_TAG, $tags)) {
$tags[] = self::CACHE_TAG;
}
// Config对象在构造函数中$this->setCacheId('config_global');设置了ID
// 这里获取config_global.lock,如果能load进来,直接就返回了,说明在上锁
$cacheLockId = $this->_getCacheLockId();
if ($this->_loadCache($cacheLockId)) {
return $this;
}
/*
$_cacheSections = array(
'admin' => 0,
'adminhtml' => 0,
'crontab' => 0,
'install' => 0,
'stores' => 1,
'websites' => 0
);
*/
if (!empty($this->_cacheSections)) {
$xml = clone $this->_xml;
//根据_cacheSections数组把全局配置文件拆分
foreach ($this->_cacheSections as $sectionName => $level) {
$this->_saveSectionCache($this->getCacheId(), $sectionName, $xml, $level, $tags);
unset($xml->$sectionName);
}
// config_global 是被剥离后剩余部分
$this->_cachePartsForSave[$this->getCacheId()] = $xml->asNiceXml('', false);
} else {
return parent::saveCache($tags);
}
$this->_saveCache(time(), $cacheLockId, array(), 60); //上锁
//移除打了CONFIG标签的cache(全部配置相关的缓存,全部打了这个标志,默认)
$this->removeCache();
foreach ($this->_cachePartsForSave as $cacheId => $cacheData) {
$this->_saveCache($cacheData, $cacheId, $tags, $this->getCacheLifetime());
/*
protected function _saveCache($data, $id, $tags=array(), $lifetime=false)
{
return Mage::app()->saveCache($data, $id, $tags, $lifetime);
}
*/
}
unset($this->_cachePartsForSave);
$this->_removeCache($cacheLockId); //解锁
return $this;
}
protected function _saveSectionCache($idPrefix, $sectionName, $source, $recursionLevel=0, $tags=array())
{
if ($source && $source->$sectionName) {
// config_global_admin config_global_websites
$cacheId = $idPrefix . '_' . $sectionName;
if ($recursionLevel > 0) {
foreach ($source->$sectionName->children() as $subSectionName => $node) {
$this->_saveSectionCache(
$cacheId, $subSectionName, $source->$sectionName, $recursionLevel-1, $tags
);
}
}
//部分保存的XML
$this->_cachePartsForSave[$cacheId] = $source->$sectionName->asNiceXml('', false);
}
return $this;
}