1.以獲取主持人嘉賓等的留言為例:
$cacheKey = implode('_', array('cache_special', $fid, $lastid, $replyNum, $page, $limit, $withTop, $withAvatar));
$callback = array(
'method'=> 'dataSpecial',
'params'=>array($result, $fid, $lastid, $replyNum, $withTop, $withAvatar, $offset, $limit)
);
ResponseHelper::outputList( CacheHelper::run($cacheKey, $callback) );
通過(guò)CacheHelper::run()方法處理緩存鍵、回調(diào)方法;
2.判斷緩存是否需要更新
$result = @ unserialize($result);
$update = $result['_expires'] < $nowTime;
//調(diào)用緩存回調(diào)函數(shù)幫助類(lèi)獲取數(shù)據(jù)
$helper = new CacheCallbackHelper();
$result = $helper->{$callback['method']}($callback['params']);
3.請(qǐng)求cacheCallbackHelper類(lèi)
public function dataSpecial($params) {
list($data, $total) = SearchHelper::getSpecial($fid, $lastid, SearchHelper::LESS, $offset, $limit);
return $result;
}
//找到searchHelper::getSpecial方法
4.請(qǐng)求SearchHelper::getSpecial方法
public static function getSpecial($fid, $lastid = 0, $rule = self::LARGER, $offset = 0, $limit = 20) {
$users = DataHelper::getSpecialUsers($fid);
$criteria = new CDbCriteria;
$criteria->select = self::SELECT;
$criteria->compare('fid', $fid);
$criteria->compare('invisible', 0);
$criteria->compare('replyid', 0);
$criteria->addInCondition('authorid', array_keys($users));
$total = Comment::model($fid)->count($criteria);
$data = Comment::model($fid)->findAll($criteria);
return array($data, (int)$total);
}
//找到Comment::model()->findAll();
5.請(qǐng)求Comment::model()方法
public static function model($shardingVal = null)
{
CModelManager::addRule('Comment', 'pre_post_[\d]{1,2}');
self::setShardedValue($shardingVal);
return parent::model(__CLASS__);
}
//找到setShardedValue();方法
//添加表名稱(chēng)到model的映射關(guān)系
6.請(qǐng)求CShardedActiveRecord的setShardedValue()方法
將fid當(dāng)做分庫(kù)分表值傳給全局變量:$_shardedValue
7.通過(guò)$_shardedValue選擇對(duì)應(yīng)的數(shù)據(jù)庫(kù) 和數(shù)據(jù)表
private static function choose() {
if(null === self::$_shardedValue) throw new CDbException('Sharded value is not set! Please call self::setShardedValue($value) first.');
CDbManager::conn()->sharded(self::$_shardedValue);
self::chooseDb();
self::chooseTable();
}
8.請(qǐng)求CDbManager::conn()->sharded()方法
public function sharded($value) {
$shardedModel = CShardedMethod::model();
//找到分庫(kù)鍵值
$this->_dbKey = $shardedModel->{$this->dbShardedMethod}($value);
$this->_config = $this->connectionConfig[$this->_dbKey];
//找到分表鍵值
$this->_tableKey = $shardedModel->{$this->tableShardedMethod}($value);
//對(duì)應(yīng) CShardedMethod 類(lèi)中的 方法名
return $this;
}
9.分庫(kù)鍵值方法、分表鍵值方法CShardedMethod類(lèi)
配置dbManager_v3文件中的屬性:
'dbShardedMethod'=> 'mod8',
'tableShardedMethod'=> 'mod64',
public function mod8($number) {
return $number % 8;
}
public function mod16($number) {
return $number % 16;
}
public function mod64($number) {
return $number % 64;
}
//用$fid跟8 或者 64 求莫