Difference between revisions of "Cache API"

(Cache Implement / Remove In Model Class)
(Cache Remove In Table Class)
Line 34: Line 34:
  
 
===Cache Remove In Table Class ===
 
===Cache Remove In Table Class ===
1. Extend CTableCache (table/cache.php) in table class
+
Extend CTableCache (table/cache.php) in the table class:
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
 
class CTablePhoto extends CTableCache
 
class CTablePhoto extends CTableCache
 
</syntaxhighlight>
 
</syntaxhighlight>
 
<br />
 
<br />
2. Add following code in constructor
+
Add the following code in the constructor:
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
 
   // Get cache object.
 
   // Get cache object.

Revision as of 01:27, 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 remove automatically when following functions get trigger.

1. onAfterStoreUser()
2. onAfterDeleteUser()