Cache API

Revision as of 01:26, 8 March 2013 by Patricia Schmidt (Talk | contribs)

Cache Implementation In View or Controller

$cache 	   = CFactory::getCache('Core');
 
// Random is to handle shuffle matter, 
// cache 3 different set of data.
$intRandom = rand(COMMUNITY_CACHE_RANDOM_MIN, 
                  COMMUNITY_CACHE_RANDOM_MAX);
 
 
// Cache does not exist, implement the cache data.		
if (!($data = $cache->load('frontpage_getLatestVideos_' . $intRandom))){  
      $data = // generate data	
      $cache->save($data, NULL, array(COMMUNITY_CACHE_TAG_VIDEOS));
}


Cache Implementation / Removal In Model Class

To implement cache in model class, add the following code in the constructor:

// addMethod(methodName, [ACTION_SAVE, ACTION_REMOVE], [COMMUNITY_CACHE_TAG_ALL, array(TAG)]) 
$oCache = CCache::inject($this);
$oCache->addMethod('getAllPhotos', CCache::ACTION_SAVE, array(COMMUNITY_CACHE_TAG_PHOTOS));


To remove cache in model class, add following code in models's constructor:

$oCache = CCache::inject($this);
$oCache->addMethod('deleteActivity', CCache::ACTION_REMOVE, array(COMMUNITY_CACHE_TAG_ACTIVITIES));
$oCache->addMethod('removeActivity', CCache::ACTION_REMOVE, array(COMMUNITY_CACHE_TAG_ACTIVITIES));


Cache Remove In Table Class

1. Extend CTableCache (table/cache.php) in table class

class CTablePhoto extends CTableCache


2. Add following code in constructor

   // Get cache object.
   $oCache = CCache::inject($this);
   // Remove video cache on every delete & store
   $oCache->addMethod(CCache::METHOD_DEL, CCache::ACTION_REMOVE, array(COMMUNITY_CACHE_TAG_VIDEOS));
   $oCache->addMethod(CCache::METHOD_STORE, CCache::ACTION_REMOVE, array(COMMUNITY_CACHE_TAG_VIDEOS));
 
  // Only CCache::METHOD_DEL, CCache::METHOD_SAVE, CCache::METHOD_STORE will handle by 
  // the cache object,other methods need to be done manually by the class itself.


Cache Remove in User Plugin

Cache will be remove automatically when following functions get trigger.

1. onAfterStoreUser()
2. onAfterDeleteUser()