Understanding the Magento Cache – Part 1

The cache is probably one of the least understood parts of Magento.  The purpose of this post is to help developers and store owners alike to better understand the limitations of the Magento Cache and how to overcome them.

The two most important concepts to wrap our heads around are IDs and Tagging.

By default the cache is stored in individual records under the var/cache/ folder.  Most stores have a low amount of products and so the cache files will normally not go over a few thousand. It is when the number of products on your catalog is more than 20,000 or 30,000 that you can expect at least an equal amount of cache records.

These record file names are composed of an Id that is basically a unique string that identifies that particular record, combined with a tag that is used to identify the type of cache record.

In Magento we have the following cache types:

  • Configuration (non-layout XML files)
  • Layouts
  • Blocks HTML output (Page blocks like headers, footers and callouts)
  • Translations
  • Collections Data
  • EAV types and attributes
  • Web Services Configuration

The cache records would look like :

mage—internal-metadatas—05a_REQEST_FF2FBE8C30D0D7A5BA253B88193067C9_CURRENT_PRODUCT_ID
mage—05a_LAYOUT_2C025FB2A82FEBF0632C2E65748B1DF83
mage—internal-metadatas—05a_LAYOUT_1B9C754D9E258E9D88E137641E80AD86B
mage—internal-metadatas—05a_REQEST_FF6659E9AAED4DCC2EB9E72038A87E75_CURRENT_PRODUCT_ID
mage—05a_LAYOUT_2C0A8A7F9C64D3D0524FE46587932515B
mage—internal-metadatas—05a_LAYOUT_1BA494CA17CC54A4FDB850E8EDE9A002A
mage—internal-metadatas—05a_REQEST_FFC948994745C87BA46E4C6C4E7FAFB6_CURRENT_PRODUCT_ID
mage—05a_LAYOUT_2C0E7A26B02CC4483BEDFCF0270F0D6F1
mage—internal-metadatas—05a_LAYOUT_1BA6DBC192208B6193542572BCFD04DC4

Magento uses Zend Cache as base for it’s own cache implementation and we can that the records use the prefix mage—— followed the the id, the tags and some other unique identifiers like the first 2 characters of the md5 hash of the path to the file. This pattern makes each record unique.

The slowest component on modern servers is usually the hard drive, the more I/O operations we add to our application in this case Magento the more will it take for it to respond and it will impact our application performance.

Knowing all of this, we can look to improve our store performance by reducing the number of read operations by keeping trimmed and clean cache. We have put together a module that will allow you to easily schedule several cache cleaning tasks.

The Module is called CacheSweep and can be download from Magento Connect for free.

CacheSweep allows you to schedule the following 5 tasks:

  1. Sweep: This is the base method and it will delete all outdated cache records ideally you want to run this once per day.
  2. SweepSystem: This method will flush the cache and its equivalent to running Flush Magento Cache, it will only flush the system cache records and ignore the images and css/js records
  3. SweepAll: Is equivalente to the Flush Cache storate and will delete all cache types.
  4. SweepStyle: Will refresh the css/js cache records
  5. SweepMedia: Will refresh the image cache.

Each of these methods can be configured in the Magento backend as they take a simple cron expression as a parameter.  In the next articles of this series we will dive into improving the store performance by setting up the cache and session folders on a tmpfs (Temporary Filesystem).