gpt4 book ai didi

php - 提高 Symfony2 中的 Controller 性能

转载 作者:可可西里 更新时间:2023-11-01 10:59:16 24 4
gpt4 key购买 nike

我正在使用 symfony2 构建一个应用程序。我的数据库中有超过 200000 条数据。为此,我使用 Redis 服务器来提高性能,这实际上减少了我的学说查询时间。但我的页面加载时间仍然超过 9 秒,我在本地托管了所有 css、js 以检查加载时间的影响,然后我发现我的 Controller 占用了大部分时间:这是 Controller 的代码:

$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 秒。有人可以建议我如何减少 Controller 的加载时间吗?

这是我存储到 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;


}

在我的分析器中,我看到 15 个查询和 1035 毫秒。够好了吗?

最佳答案

由于对 isGranted() 的调用不依赖于实际的 $content,因此通过将其值保存在变量中将这些调用移出任何循环。

您还应该避免在 foreach ($contents as &$content) 循环中使用引用。

关于php - 提高 Symfony2 中的 Controller 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31967989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com