Sinisakrisan (Talk | contribs) (Created page with "===Cache Implement In View Or Controller === <syntaxhighlight lang="php"> $cache = CFactory::getCache('Core'); // Random is to handle shuffle matter, // cache 3 differe...") |
(→Cache Implementation In View or Controller) |
||
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | ===Cache | + | ===Cache Implementation In View or Controller === |
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
$cache = CFactory::getCache('Core'); | $cache = CFactory::getCache('Core'); | ||
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 16: | Line 14: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br /> | <br /> | ||
− | ===Cache | + | |
− | + | ===Cache Implementation / Removal In Model Class=== | |
+ | To implement cache in model class, add the following code in the constructor: | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
// addMethod(methodName, [ACTION_SAVE, ACTION_REMOVE], [COMMUNITY_CACHE_TAG_ALL, array(TAG)]) | // addMethod(methodName, [ACTION_SAVE, ACTION_REMOVE], [COMMUNITY_CACHE_TAG_ALL, array(TAG)]) | ||
Line 24: | Line 23: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br /> | <br /> | ||
− | + | To remove cache in model class, add following code in models's constructor: | |
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
$oCache = CCache::inject($this); | $oCache = CCache::inject($this); | ||
Line 33: | Line 32: | ||
===Cache Remove In Table Class === | ===Cache Remove 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 /> | ||
− | + | Add the following code in the constructor: | |
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
// Get cache object. | // Get cache object. | ||
Line 45: | 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 52: | Line 50: | ||
===Cache Remove in User Plugin === | ===Cache Remove in User Plugin === | ||
− | Cache will be | + | Cache will be removed automatically when the following functions are triggered: |
− | + | [[onAfterStoreUse|onAfterStoreUser()]] | |
− | + | [[onAfterDeleteUser|onAfterDeleteUser()]] | |
− | + |
Latest revision as of 01:29, 8 March 2013
Contents
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()