Difference between revisions of "Cache API"

(Cache Remove in User Plugin)
(Cache Implementation In View or Controller)
 
(2 intermediate revisions by the same user not shown)
Line 7: Line 7:
 
$intRandom = rand(COMMUNITY_CACHE_RANDOM_MIN,  
 
$intRandom = rand(COMMUNITY_CACHE_RANDOM_MIN,  
 
                   COMMUNITY_CACHE_RANDOM_MAX);
 
                   COMMUNITY_CACHE_RANDOM_MAX);
 
 
 
// Cache does not exist, implement the cache data.
 
// Cache does not exist, implement the cache data.
 
if (!($data = $cache->load('frontpage_getLatestVideos_' . $intRandom))){   
 
if (!($data = $cache->load('frontpage_getLatestVideos_' . $intRandom))){   
Line 46: Line 44:
 
   $oCache->addMethod(CCache::METHOD_DEL, CCache::ACTION_REMOVE, array(COMMUNITY_CACHE_TAG_VIDEOS));
 
   $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));
 
   $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  
 
   // 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.
 
   // the cache object,other methods need to be done manually by the class itself.
Line 54: Line 51:
 
===Cache Remove in User Plugin ===
 
===Cache Remove in User Plugin ===
 
Cache will be removed automatically when the following functions are triggered:
 
Cache will be removed automatically when the following functions are triggered:
  [[onAfterStoreUse|onAfterStoreUser()]]<br />
+
  [[onAfterStoreUse|onAfterStoreUser()]]
 
  [[onAfterDeleteUser|onAfterDeleteUser()]]
 
  [[onAfterDeleteUser|onAfterDeleteUser()]]

Latest revision as of 01:29, 8 March 2013

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

Extend CTableCache (table/cache.php) in the table class:

class CTablePhoto extends CTableCache


Add the following code in the 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 removed automatically when the following functions are triggered:

onAfterStoreUser()
onAfterDeleteUser()