Difference between revisions of "Cache API"

(Cache Implement / Remove In Model Class)
(Cache Implementation In View or Controller)
 
(4 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 34: Line 32:
  
 
===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.
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 53: Line 50:
  
 
===Cache Remove in User Plugin ===
 
===Cache Remove in User Plugin ===
Cache will be remove automatically when following functions get trigger.
+
Cache will be removed automatically when the following functions are triggered:
 
+
[[onAfterStoreUse|onAfterStoreUser()]]
1. [[onAfterStoreUse|onAfterStoreUser()]]<br />
+
[[onAfterDeleteUser|onAfterDeleteUser()]]
2. [[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()