評(píng)論系統(tǒng)代碼閱讀筆記

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 求莫
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容