我有带有路由规则((1000+))的路由表,可以在初始化时加载到 UrlManager。但是瓶颈是foreach循环,太慢了。
可能的答案:NoSQL 数据库、内存缓存?你能给点建议吗?
$oldRules = $this->rules;
$this->rules = array();
$dependency = new CDbCacheDependency('SELECT MAX(created_at) FROM route');
$routes = Route::model()->cache(Yii::app()->params['cache_expire_time'], $dependency)->findAll();
Yii::app()->cache->set('routes', $routes, Yii::app()->params['cache_expire_time'], $dependency);
// @todo Bottle neck on 1000+ rows (~1.3s on 1000 rows)
foreach ($routes as $route) {
$rule = array($route->routeable_controller . '/' . $route->routeable_action);
if ($route->routeable_id) {
$rule['defaultParams'] = array('id' => $route->routeable_id);
}
$this->rules[$route->pattern] = $rule;
}
$this->rules += $oldRules;