magento緩存系列詳解:clean cache

cache是一個很大的概念,涉及的內容方方面面,magento cache是基於zend的,如果你對zend cache理解很深的話,相信magento cache也不再話下,本篇文章着重介紹Flush Magento Cache 和Flush Cache Storage 兩個按鈕的區別;


爲了理解這兩個選項之間的區別,你要先了解一些東西如緩存如何在 Magento 中工作。特別是要能準確的理解ids 和 tagging。

實質上,"id"就是一個唯一的字符串用來標識高速緩存中的共享存儲的記錄。tagging是另一個字符串,用於對不同類型的應用程序緩存的數據進行分類。
在 Magento中,tagging主要用於區分了以下的幾個緩存類型:
    Configuration (non-layout XML files)
    Layouts (all those XML files under app/design/…)
    Blocks HTML output (Page blocks like headers, footers and callouts)
    Translations
    Collections Data
    EAV types and attributes (reduces some database lookups)
    Web Services Configuration

請看magento中默認的緩存文件列表存儲示例:
$ ls var/cache/mage--0
mage---1ef_DB_PDO_MYSQL_DDL_catalog_product_index_price_idx_1
mage---1ef_DB_PDO_MYSQL_DDL_core_config_data_1
mage---1ef_LAYOUT_0183D2D163E71FE45BB4CE3F4045A71BD
mage---1ef_LAYOUT_0659E64C667F785D2436DB04EBCBEE12E
mage---1ef_LAYOUT_088A9AF9EA75F3D59B57387F8E9C7D7A6
mage---1ef_LAYOUT_0956CDEF59F213D48A2D1218CC2CD1E96
mage---1ef_LAYOUT_1013A059DA3EFFB6F31EB8ABA68D0469E
mage---1ef_LAYOUT_12D7604E9632FF8D14B782A248FCBD2E7
mage---1ef_LAYOUT_14E2F46FB273D9CEA54FDD1B14EB28645
mage---1ef_LAYOUT_16CD0CCB23CB5ABE6844B7E3241F0A751
mage---1ef_LAYOUT_1DC0705D40BBC39A32179EE8A85BEF5D7
mage---1ef_Zend_LocaleC_en_US_day_gregorian_format_wide_wed
mage---1ef_Zend_LocaleC_en_US_month_gregorian_format_wide_5

正如您所看到的 ,根據緩存文件的文件名,可以區分出不同的緩存模式。
因爲magento cache是基於zend cache的,所以magento的緩存文件也會有一個默認前綴mage,然後是一個id前綴(對app/etc/ 這個目錄進行了一次md5 hash計算,然後取的前3個字符),然後是tag標記和其他一些標示符。整個字串就構成了緩存項的唯一一個id。
理解了以上內容,我們就很容易理解當你清除cache的時候,magento具體都做了什麼。

“Flush Magento Cache”
當你點擊“Flush Magento Cache”時,後臺CacheController.php 調用了function flushSystemAction()。在這個function裏,又調用了Mage_Core_Model_App 的cleanCache()這個function,然後又調用了Mage_Core_Model_Cache的clean($tags)這個function,我們先看下這個function的定義,或許能發現些什麼:
/**
 * Clean cached data by specific tag
 *
 * @param   array $tags
 * @return  bool
 */
public function clean($tags=array())
{
    $mode = Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG;
    if (!empty($tags)) {
        if (!is_array($tags)) {
            $tags = array($tags);
        }
        $res = $this->_frontend->clean($mode, $this->_tags($tags));
    } else {
        $res = $this->_frontend->clean($mode, array(Mage_Core_Model_App::CACHE_TAG));
        $res = $res && $this->_frontend->clean($mode, array(Mage_Core_Model_Config::CACHE_TAG));
    }
    return $res;
}

我們看到,在這個方法裏它調用了zend cache對象的一些方法,但是要指出的是在這裏會清除所有和參數$tags相匹配的cache,如果$tags爲空,會執行else代碼段,$tags 定義在 Mage_Core_Model_App 和Mage_Core_Model_Config,分別是MAGE和CONFIG,就是說會清除$tags爲MAGE和CONFIG的所有cache。所以它並沒有清除整個cache。
在看 Zend_Cache “clean()” function 定義之前, 先看下發生了什麼事當點擊另外一個按鈕“Flush Cache Storage”的時候。

“Flush Cache Storage”
當你點擊“Flush Cache Storage”時,後臺CacheController.php 調用了function flushAllAction(),而不是前面提到的flushSystemAction(),然後這個方法同樣會實例化一個Mage_Core_Model_App對象,但是他不在調用clean($tags),而是調用了flush(),看下這個function:
/**
 * Clean cached data by specific tag
 *
 * @return  bool
 */
public function flush()
{
    $res = $this->_frontend->clean();
    return $res;
}

喔喔,發現了沒flush一樣要調用zend cache對象的方法,但是有一個不同的是,這裏的clean()沒有傳入任何參數;接下來去Zend_Cache_Core::clean() 看下到底發生了什麼事,當沒有傳入參數$tags的時候。
/**
 * Clean cache entries
 *
 * Available modes are :
 * 'all' (default)  => remove all cache entries ($tags is not used)
 * 'old'            => remove too old cache entries ($tags is not used)
 * 'matchingTag'    => remove cache entries matching all given tags
 *                     ($tags can be an array of strings or a single string)
 * 'notMatchingTag' => remove cache entries not matching one of the given tags
 *                     ($tags can be an array of strings or a single string)
 * 'matchingAnyTag' => remove cache entries matching any given tags
 *                     ($tags can be an array of strings or a single string)
 *
 * @param  string       $mode
 * @param  array|string $tags
 * @throws Zend_Cache_Exception
 * @return boolean True if ok
 */
public function clean($mode = 'all', $tags = array())
{
    if (!$this->_options['caching']) {
        return true;
    }
    if (!in_array($mode, array(Zend_Cache::CLEANING_MODE_ALL,
                               Zend_Cache::CLEANING_MODE_OLD,
                               Zend_Cache::CLEANING_MODE_MATCHING_TAG,
                               Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG,
                               Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG))) {
        Zend_Cache::throwException('Invalid cleaning mode');
    }
    self::_validateTagsArray($tags);
    return $this->_backend->clean($mode, $tags);

可以看到,clean參數爲空時,這裏的$mode默認是all,就是會清除所有cache;
另外在後臺Cache Storage Management,選擇某一個或幾個Cache Type,再refresh,原理其實就等同於“Flush Magento Cache”,這裏的Cache Type就可以看做是傳入的tag。

總結一下:“Flush Magento Cache” 將清除$tags爲MAGE和CONFIG的cache,並非所有cache;“Flush Cache Storage” 不管有沒有$tags所有的cache都會被清除;
另外需要注意的是,當magento使用默認的file來存儲cache時,通常我們也可以用rm -rf var/cache/*來清除整個cache;但是如果不是用file存的cache,比如是xcache, memcached, apc, db, sqlite等等,“Flush Magento Cache”就可能會失效,因爲memcached不支持tags,諸如此類的原因。此時“Flush Cache Storage”或許會更有效。

如果你使用共享緩存系統,如兩個apps使用一塊memcached,用“Flush Cache Storage”並不是一個明智的辦法,所以要小心使用!

magento緩存系列詳解:如何cache一個block

原創文章,轉載請註明出處,本文鏈接http://blog.csdn.net/shangxiaoxue/article/details/7601355


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章