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) );
通过CacheHelper::run()方法处理缓存键、回调方法;
2.判断缓存是否需要更新
$result = @ unserialize($result);
$update = $result['_expires'] < $nowTime;
//调用缓存回调函数帮助类获取数据
$helper = new CacheCallbackHelper();
$result = $helper->{$callback['method']}($callback['params']);
3.请求cacheCallbackHelper类
public function dataSpecial($params) {
list($data, $total) = SearchHelper::getSpecial($fid, $lastid, SearchHelper::LESS, $offset, $limit);
return $result;
}
//找到searchHelper::getSpecial方法
4.请求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.请求Comment::model()方法
public static function model($shardingVal = null)
{
CModelManager::addRule('Comment', 'pre_post_[\d]{1,2}');
self::setShardedValue($shardingVal);
return parent::model(__CLASS__);
}
//找到setShardedValue();方法
//添加表名称到model的映射关系
6.请求CShardedActiveRecord的setShardedValue()方法
将fid当做分库分表值传给全局变量:$_shardedValue
7.通过$_shardedValue选择对应的数据库 和数据表
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.请求CDbManager::conn()->sharded()方法
public function sharded($value) {
$shardedModel = CShardedMethod::model();
//找到分库键值
$this->_dbKey = $shardedModel->{$this->dbShardedMethod}($value);
$this->_config = $this->connectionConfig[$this->_dbKey];
//找到分表键值
$this->_tableKey = $shardedModel->{$this->tableShardedMethod}($value);
//对应 CShardedMethod 类中的 方法名
return $this;
}
9.分库键值方法、分表键值方法CShardedMethod类
配置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 求莫