简体   繁体   English

Symfony2中的快速ajax响应

[英]Fast ajax responses in Symfony2

I have a tagging text field that while typing in a new tag, suggests similar existing tags. 我有一个标记文本字段,在输入新标记时,会建议类似的现有标记。 The suggestions are retrieved by an ajax request to a controller which pulls them with from the DB with Doctrine. 建议通过ajax请求检索到控制器,控制器从带有Doctrine的DB中提取它们。 ie: 即:

  • Request /tags/suggestions?q=foo 请求 /tags/suggestions?q=foo
  • Response ["foo","food","fool"] 回复 ["foo","food","fool"]

The problem is that the request is too slow (atleast 2 seconds, in prod ) which in this case is too much. 问题是请求太慢(至少2秒,在prod中 ),在这种情况下太多了。

Is there a way to make the request faster? 有没有办法让请求更快? According to the profiler, the main time consumer is kernel.request with 50% of the total time. 根据分析器,主要时间消费者是kernel.request ,占总时间的50%。

if kernel.request is slow , it's not necessarly a symfony side issue. 如果kernel.request很慢,那么它不一定是symfony方面的问题。

you should look to optimize your server by updating your Apache with php-fastPGM instead of php standard module. 您应该通过使用php-fastPGM而不是php标准模块更新Apache来优化您的服务器。

you should also activate a cache manager like php APC which reduce a few more the requests . 你还应该激活一个像PHP APC这样的缓存管理器,它可以减少一些请求。

for example i run a symfony project on a raspberry pi . 例如,我在树莓派上运行一个symfony项目。 Before these tweaks , one request with 8 database request took around 25 seconds to display. 在进行这些调整之前,一个带有8个数据库请求的请求大约需要25秒才能显示。 After these tweaks, a page was displayed in less than 3 seconds ( an average of 2.5 s ) 经过这些调整后,页面显示不到3秒(平均2.5秒)

another list of possible tweaks : 另一个可能的调整列表:

http://slides.liip.ch/static/2012-05-18_symfony-speed.html#9 http://slides.liip.ch/static/2012-05-18_symfony-speed.html#9

You could ask doctrine(if you're not already doing so) to fetch the result as array(without converting your whole query to a bunch of objects) by using: 您可以通过使用以下方式询问doctrine(如果您还没有这样做)将结果作为数组获取(不将整个查询转换为一堆对象):

$q = $em->createQueryBuilder('t')->(...);
$q -> getQuery() -> fetchArrayResult();

Should speed up things a little. 应该加快一点点。

对于像ajax autocomplete这样的小页面,我决定使用Silex这是一个微型Symfony2,它为我提供了一个优雅而快速的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM