Magento cache example

Here is an example of using Magento’s cache feature. It stores the data in a cache and respond with the cached data if the data is already cached. The cache can be removed if the param clean=yes. Install this example by creating these three files in the appropriate directory. Then go to the url http://localhost/magecacheexample?id=2, to clean the cached version of the data, go to the url http://localhost/magecacheexample?id=2&clean=yes. If the entire caches from var/cache/ are all cleared, this cache will also be cleared.

1. Module install configuration, app/etc/modules/MyExtensions_MageCacheExample.xml

<?xml version="1.0"?>
<config>
    <modules>
        <MyExtensions_MageCacheExample>
            <active>true</active>
            <codePool>local</codePool>
        </MyExtensions_MageCacheExample>
    </modules>
</config>

2. Module configuration, app/code/local/MyExtensions/MageCacheExample/etc/config.xml

<?xml version="1.0"?>
<config>    
    <modules>
        <MyExtensions_MageCacheExample>
            <version>0.1.0</version>
        </MyExtensions_MageCacheExample>
    </modules>
    <frontend>
        <routers>
            <magecacheexample>
                <use>standard</use>
                <args>
                    <module>MyExtensions_MageCacheExample</module>
                    <frontName>magecacheexample</frontName>
                </args>
            </magecacheexample>
        </routers> 
    </frontend>
</config>

3. Index controller, app/code/local/MyExtensions/MageCacheExample/controllers/indexController.php

<?php
class MyExtensions_MageCacheExample_IndexController extends Mage_Core_Controller_Front_Action {        
 
	const CACHE_KEY_PREFIX = 'cacheEx';

	//Main action
	//if the param id is null, respond with {message:"Please provide an id."}
	//if the id is not null, and the data is already stored in the cache, respond with the data from cache.
	//if the id is not null, and the data is not yet cache, create the data, save it to the cache and then respond with the data.
    public function indexAction() {

    	$this->getResponse()->setHeader('Content-type', 'application/json');
    	$id = $this->getRequest()->getParam('id');
    	$clean = $this->getRequest()->getParam('clean');
    	$cache = Mage::app()->getCache();
    	$data;

    	if( !is_null($id) ) 
    	{   

    		if($clean === "yes")
    		{
    			$cache->remove( self::CACHE_KEY_PREFIX . $id );
    		}

    		$data = $cache->load( self::CACHE_KEY_PREFIX . $id );
    		//get the data from cache if it's cached, else create the data and save it to the cache
    		if ( $data !== false )
    		{
    			echo "cached";
    			$this->getResponse()->setBody( $data );
    		} 
    		else
    		{
    			$data = $this->createDummyData();
    			$cache->save( $data, self::CACHE_KEY_PREFIX . $id, array('mage_cache_example'), 60*60*24*30); //set the cache for 30 days            
    			echo "not cached";
	            $this->getResponse()->setBody( $data );

    		}
        } 
        else 
		{
            $this->getResponse()->setBody( '{message:"Please provide an id."}' );
		}        

    }

    private function createDummyData() {
    	$arr = array();
		for($i=0; $i<100; $i++)
		{
			for($j=0; $j<8000; $j++)
			{
				$arr["top".$i][] = "second" . $j; 
			}
		}

		return json_encode($arr);
    }

}

Search within Codexpedia

Custom Search

Search the entire web

Custom Search