Magento Expert Forum - Improve your Magento experience

Results 1 to 3 of 3

A Developer's Guide to using the Magento Cache

  1. #1
    Moderator speed2x's Avatar
    Join Date
    Mar 2013
    Location
    Hollywood, Florida, United States
    Posts
    88
    Thanks
    8
    Thanked 7 Times in 6 Posts

    Thumbs up A Developer's Guide to using the Magento Cache

    Magento employs caching at many levels within the system, most of which are transparent to the unwitting developer. As the complexity of your Magento tasks increase, it's inevitable that you'll eventually begin to require caching of your own.

    I wrote this post for the very first time in July 2010 when Magento had released version 1.4. This article has since been updated for version 1.7.

    Caching Concepts



    The cache in Magento resembles many other systems in that it is a key-value store. If we wish to save $data in the cache we must address it with a name, the $key. When we later need to retrieve $data, we will do so with that same $key.

    The $data stored in the cache can also be tagged with metadata. This allows for logical groupings of cache entries for mass deletion.

    Controlling the Cache



    For most of us, our first contact with Magento's cache comes through frustration, not understanding why one earth our perfectly formed blocks and models aren't working only to discover that 'the cache was on'.

    Magento provides an interface for enabling, disabling and flushing the cache in the administration area. Each row in the "Cache Management" table represents a cache tag. When refreshing one of these rows we are deleting all cache entries with a certain tag.

    The Cache API



    The model controlling our interface with the caching subsystem is Mage_Core_Model_Cache. We typically access the cache through an already instantiated object accessible at Mage::app()->getCache().

    The implementation of the cache is hidden from us, the interface we are given consists (pretty much entirely) of the following methods:

    PHP Code:
    save($value$key$tags = array(), $lifeTime=null)
    load($key)
    remove($key)
    clean($tags = array() 
    The actual backend used for caching can be a number of mechanisms from the built-in flat file approach (here's a more efficient algorithm) tomemcached, apc, or even redis for the hardcore.

    Storing Values

    We'll start with storing a value.

    PHP Code:
    $cache Mage::app()->getCache();
    $cache->save(date("r"), "mage2x_date", array("mage2x_cache"), 10);
    $cache->save("hello world - " time(), "mage2x_helloworld", array("mage2x_cache"), 60 60); 
    With the above example I have instructed the cache to store the value of the current date with the $key mage2x_date. We'll be able to refer to this date using the key at a later date. I've also stored the phrase hello world - 601992000 (for the given timestamp) with the $key mage2x_helloworld.

    We'll ignore the tag mage2x_cache for the time being, but you will notice an integer lifeTime value associated with both cache entries. This is the amount of time, in seconds, that the value will exist in cache before being deleted. By setting this value to null, or omitting the argument, the value will remain in cache until removed.

    Retrieving Values


    To fetch the value of a key from cache, we simply use:

    PHP Code:
    $cache->load("mage2x_date"); 
    If the value does not exist in cache then false will be returned.

    Removing Values



    To remove individual values from the cache, we use:

    PHP Code:
    $cache->remove("mage2x_date"); 
    You'll notice that I've also applied the mage2x_cache $tag to the cache entries I initially stored. By applying a tag to a set of values I am able to refer to them all at a later for mass removal. We do this by:

    PHP Code:
    $cache->clean(array("mage2x_cache")); 
    This will delete all cache entries with the $tag mage2x_cache.

    View more threads in the same category:


  2. #2
    Junior Member avery's Avatar
    Join Date
    Jun 2013
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    I like the caching concept what can apply to magento programming, it 's very effective. Thanks for the good share article

  3. #3
    Junior Member funkywizard's Avatar
    Join Date
    Mar 2015
    Location
    Phoenix, AZ
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by avery View Post
    I like the caching concept what can apply to magento programming, it 's very effective. Thanks for the good share article
    I agree, thanks for sharing the article. As your site becomes more complex, caching is definitely going to help you keep the performance up.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •