Magento Cache Series Detailed: Clean cache

Source: Internet
Author: User
Tags function definition memcached vars zend

The cache is a big concept, covering all aspects of the content, Magento cache is based on Zend, if you have a deep understanding of the Zend cache, I believe Magento cache is no longer talking, this article focuses on the flush Magento cache and flush Cache Storage The difference of two buttons;

To understand the difference between these two options, you should first understand something like how caching works in Magento. In particular, to be able to accurately understand IDs and tagging.

In essence, "id" is a unique string used to identify a record of shared storage in the cache. Tagging is another string used to classify data cached by different types of applications.
In Magento, tagging is primarily used to differentiate between the following cache types:

[Plain]View Plaincopy
    1. Configuration (non-layout XML files)
    2. Layouts (all those XML files under app/design/...)
    3. Blocks HTML output (Page Blocks like headers, footers and callouts)
    4. Translations
    5. Collections Data
    6. EAV types and attributes (reduces some database lookups)
    7. Web Services Configuration


Take a look at the default cache file list Storage example in Magento:

[Plain]View Plaincopy
  1. $ ls var/cache/mage--0
  2. Mage---1ef_db_pdo_mysql_ddl_catalog_product_index_price_idx_1
  3. Mage---1ef_db_pdo_mysql_ddl_core_config_data_1
  4. Mage---1ef_layout_0183d2d163e71fe45bb4ce3f4045a71bd
  5. Mage---1ef_layout_0659e64c667f785d2436db04ebcbee12e
  6. Mage---1ef_layout_088a9af9ea75f3d59b57387f8e9c7d7a6
  7. Mage---1ef_layout_0956cdef59f213d48a2d1218cc2cd1e96
  8. Mage---1ef_layout_1013a059da3effb6f31eb8aba68d0469e
  9. Mage---1ef_layout_12d7604e9632ff8d14b782a248fcbd2e7
  10. Mage---1ef_layout_14e2f46fb273d9cea54fdd1b14eb28645
  11. Mage---1ef_layout_16cd0ccb23cb5abe6844b7e3241f0a751
  12. Mage---1ef_layout_1dc0705d40bbc39a32179ee8a85bef5d7
  13. Mage---1ef_zend_localec_en_us_day_gregorian_format_wide_wed
  14. Mage---1ef_zend_localec_en_us_month_gregorian_format_wide_5


As you can see, depending on the file name of the cache file, different cache modes may be divided.
Because the Magento cache is based on Zend cache, the Magento cache file will also have a default prefix mage, followed by an ID prefix (app/etc/hash is calculated for MD5, and then the first 3 characters are taken), Then there are tag tags and some other identifiers. The entire string constitutes the only ID of the cache entry.
Having understood the above, it is easy to understand what Magento did when you cleared the cache.

"Flush Magento Cache"
When you click "Flush Magento Cache", the background cachecontroller.php calls function Flushsystemaction (). In this function, the function of Mage_core_model_app's Cleancache () is called, and then the Mage_core_model_cache's Clean ($tags) function is called. , let's look at the definition of this function, and maybe we can find something:

[PHP]View Plaincopy
  1. /**
  2. * Clean cached data by specific tag
  3. *
  4. * @param array $tags
  5. * @return BOOL
  6. */
  7. Public function Clean ($tags =Array ())
  8. {
  9. $mode = Zend_cache::cleaning_mode_matching_any_tag;
  10. if (! Empty ($tags)) {
  11. if (! Is_array ($tags)) {
  12. $tags = Array ($tags);
  13. }
  14. $res = $this->_frontend->clean ($mode, $this->_tags ($tags));
  15. } Else {
  16. $res = $this->_frontend->clean ($mode, Array (mage_core_model_app::cache_tag));
  17. $res = $res && $this->_frontend->clean ($mode, Array (mage_core_model_config::cache  _tag));
  18. }
  19. return $res;
  20. }


We see that in this method it invokes some of the methods of the Zend cache object, but to point out that all caches matching the parameter $tags are cleared here, and if the $tags is empty, the else code snippet is executed, $tags defined in the Mage_core_model_ The APP and Mage_core_model_config, respectively, are Mage and Config, which means that all caches $tags to Mage and Config are cleared. So it didn't erase the entire cache.
Before looking at the Zend_cache "clean ()" function definition, look at what happened when you click on the other button "Flush Cache Storage".

"Flush Cache Storage"
When you click "Flush Cache Storage", the background cachecontroller.php calls function flushallaction () instead of the Flushsystemaction () mentioned earlier, The method will then instantiate a Mage_core_model_app object, but instead of calling clean ($tags), it calls Flush () and looks at the function:

[PHP]View Plaincopy
    1. /** 
    2.  * clean cached data by specific tag 
    3.  * 
    4.  *  @return   bool 
    5.  */  
    6. Public function flush ()   
    7. {  
    8.      $res  =  $this->_frontend->clean ();   
    9.      return  $res;   
    10. }   


Oh, I found a way to invoke the Zend Cache object without flush, but there is a difference where clean () does not pass any parameters; next go to Zend_cache_core::clean () to see what happened, when there is no incoming parameter $ tags in time.

[PHP]View Plaincopy
  1. /**
  2. * Clean Cache entries
  3. *
  4. * Available Modes is:
  5. * ' All ' (default) and remove all cache entries ($tags are not used)
  6. * ' old ' = remove too old cache entries ($tags are not used)
  7. * ' Matchingtag ' = Remove cache entries matching all given tags
  8. * ($tags can be an array of strings or a single string)
  9. * ' Notmatchingtag ' = Remove cache entries not matching one of the given tags
  10. * ($tags can be an array of strings or a single string)
  11. * ' Matchinganytag ' = Remove cache entries matching any given tags
  12. * ($tags can be an array of strings or a single string)
  13. *
  14. * @param string $mode
  15. * @param array|string $tags
  16. * @throws zend_cache_exception
  17. * @return Boolean True if ok
  18. */
  19. Public function Clean ($mode = "All", $tags = Array ())
  20. {
  21. if (! $this->_options[' caching ']) {
  22. return true;
  23. }
  24. if (!in_array ($mode, Array (zend_cache::cleaning_mode_all,
  25. Zend_cache::cleaning_mode_old,
  26. Zend_cache::cleaning_mode_matching_tag,
  27. Zend_cache::cleaning_mode_not_matching_tag,
  28. Zend_cache::cleaning_mode_matching_any_tag)) {
  29. Zend_cache::throwexception (' Invalid cleaning mode ');
  30. }
  31. Self::_validatetagsarray ($tags);
  32. return $this->_backend->clean ($mode, $tags);


As you can see, when the clean parameter is empty, the $mode default is all, which clears all caches;
In addition, in the background cache Storage Management, select one or several cache type, then refresh, the principle is actually equivalent to "Flush Magento cache", here the cache type can be regarded as the incoming tag.

To summarize: "Flush Magento Cache" will clear the cache $tags as Mage and Config, not all caches, "flush Cache Storage" regardless of whether or not $tags all caches will be erased;
It is also important to note that when Magento uses the default file to store the cache, usually we can also use the RM-RF var/cache/* to clear the entire cache, but if not with the file cache, such as XCache, memcached, APC, DB, SQLite and so on, "Flush Magento Cache" may become invalid because memcached does not support tags, and so on. "Flush Cache Storage" may be more effective at this point.

If you use a shared cache system, such as two apps using a piece of memcached, using "Flush cache Storage" is not a sensible approach, so be careful!

Magento Cache Series Detailed: Clean cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.