我正在用symfony2构建一个应用程序。我的数据库中有超过200000+的数据,为此我使用redis服务器来提高性能,这实际上减少了我的条令查询时间。但我的页面加载时间还是超过了9秒,我在本地托管了所有css、js来检查加载时间的影响,然后我发现我的控制器占用了大部分时间:下面是控制器的代码:
$episodesByContent = array();
foreach ($episodes as $episode) {
if (!array_key_exists($episode['content_id'], $episodesByContent)) {
$episodesByContent[$episode['content_id']] = array();
}
$episodesByContent[$episode['content_id']][] = $episode;
}
$data = array();
foreach ($contents as &$content) {
$content['select'] = '<input type="checkbox" value="' . $content['id'] . '" id="'. $content['id'] . '" name="multiselect_checkbox" class="multiselect_checkbox">';
$content['actions'] = '';
if ($this->get('security.authorization_checker')->isGranted(array('ROLE_CONTENT_NEW_RO','ROLE_CONTENT_NEW_RW',' ROLE_CONTENT_GENERAL_RW'))) {
$link = $this->generateUrl('content_show', array(
'id' => $content['id'],
));
$content['actions'] .= '<a href="' . $link . '" rel="tooltip" title="Show" class="btn btn-info btn-sm btn-icon icon-left" role="button">
<i class="entypo-info"></i> Show
</a>';
}
if ($this->get('security.authorization_checker')->isGranted('ROLE_CONTENT_NEW_RW')) {
$link = $this->generateUrl('content_edit', array(
'id' => $content['id'],
));
$content['actions'] .= '<a href="' . $link . '" rel="tooltip" title="Edit" class="btn btn-default btn-sm btn-icon icon-left" role="button" onclick="return confirm(\'Are you sure?\')">
<i class="entypo-pencil"></i> Edit
</a>';
}
$data[] = $content;
if (array_key_exists($content['id'], $episodesByContent)) {
foreach ($episodesByContent[$content['id']] as $episode) {
$episode['select'] = '';
$episode['priority'] = $content['priority'];
$episode['owner'] = $content['owner'];
$episode['sequence'] = $content['sequence'];
$episode['category'] = $content['category'];
$episode['category_sequence'] = $content['category_sequence'];
$episode['actions'] = '';
$data[] = $episode;
}
}
}
$encoders = array(new JsonEncoder());
$normalizers = array(new GetSetMethodNormalizer());
$serializer = new Serializer($normalizers, $encoders);
$datatable = $this->get("bbd_datatables.content");
$datatable->setData($serializer->serialize($data, "json"));
这部分需要8秒。有人能建议我如何减少控制器的加载时间吗?
这是IM存储到Redis缓存的查询:
public function getContentList(){
$cacheDriver = new RedisCache();
$cacheDriver->setRedis(new Client());
if ($cacheDriver->contains('_content')){
return $cacheDriver->fetch('_content');
}
$qb = $this->createQueryBuilder('c')
->select('c.id, c.title, c.sequence, c.sequence_count, c.category_sequence, c.unique_id, c.priority, c.status, c.created_at,c.kaltura_id')
->addSelect('o.slug as owner')
->addSelect('cat.slug as category')
->addSelect("group_concat(m.name SEPARATOR ',') AS media")
->addSelect("group_concat(a.name SEPARATOR ',') AS album")
->innerJoin('c.content_owner', 'o')
->innerJoin('c.category', 'cat')
->leftJoin('c.media', 'm')
->leftJoin('c.albums', 'a')
->groupBy('c.id');
$query= $qb->getQuery()->getArrayResult();
$cacheDriver->save('_content', $query, 3600);
return $query;
}
在我的profiler中,我看到了15个查询和1035ms,这足够了吗?
最佳答案
由于对isGranted()
的调用不依赖于实际的$content
,所以通过将其值保存在变量中,将这些调用移出任何循环。
您还应该避免在foreach ($contents as &$content)
循环中使用引用。
关于php - 在Symfony2中提高 Controller 性能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31967989/