Symfony - 图像响应 - 速度、优化
我在控制器中生成动态图形:
如果文件不存在(唯一文件名)则生成图形并保存
return RedirectResponse($ url)
(3.我也尝试过:)
$fp = fopen($url, "rb");
$str = stream_get_contents($fp);
fclose($fp);
$response = new Response($str, 200);
$response->headers->set('Content-Type', 'image/png');
return $response;
我
需要它为用户动态生成用户栏[在这个图形上,如果我的网站上进行了一些更改,一些文本将自动更新]。
所以对我来说重要的是这个脚本运行速度快并且不会杀死我的服务器/bandwitch。为了实现这一点,我在存储所有生成的图形的地方创建了缓存。但我遇到了速度问题,我上面描述的解决方案非常慢,当我使用开发人员工具(F12)在 Chrome 中测试它时,它告诉我这两个解决方案(2.和3.)都需要大约~1s(当相同的图形[没有控制器等,只需直接url到.png]加载44ms)。
因此,如果仅对我而言,每个图形大约需要 1 秒,那么如果例如 1000 个用户同时生成该图形,我认为它可能会减慢我的网站速度。
请帮忙,你知道有什么更快的方法吗?
附言。重定向对我来说并不是最想要的,因为我更愿意保留 url,例如 smth/username/userbar/ 代替 smth/img/userbars/cache/blabla.png。但如果它会快得多,那么我可以忍受:)。
编辑:要明确的是:我不是在寻找缓存图像的解决方案,我的问题是通过控制器返回图像(已经生成的图像,仅从 ftp)大约需要 1 秒,而通常情况下,如果您这样做,则
<img src="direct_url_to_png">
需要大约 44 毫秒。
I generate dynamically graphic in my controller:
if not file exists (unique filename) then generate graphic and save it
return RedirectResponse($url)
(3. I've tried also:
$fp = fopen($url, "rb");
$str = stream_get_contents($fp);
fclose($fp);
$response = new Response($str, 200);
$response->headers->set('Content-Type', 'image/png');
return $response;
)
I need it to generate dynamically userbars for users [on this graphic some text will be automatically updating if some changes will be done on my websites].
So it's important for me that this script works fast and doesn't kill my server/bandwitch. To achieve that I've made cache where all generated graphics are stored. But i've got a problem with speed, solutions which i've described above are pretty slow, when i've tested it in chrome with developer tool (F12) then it shows me that both solution (2. and 3.) takes about ~1s (when the same graphics [without controllers etc. just direct url to .png] loads 44ms).
So if only for me it takes about ~1s per graphic then if for example 1000 users will generate at the same time this graphic it can slower my website I think.
Please help, do you know any faster ways to do that?
PS. Redirect isn't most desire thing for me, because I would prefer to leave url as it is, for example smth/username/userbar/ insted of smth/img/userbars/cache/blabla.png. But if it will be much faster then I can stand it :).
EDIT: To be clear: I'm not looking for solution to cache images, my problem is that returning images via controller (already generated images, just from ftp) takes about 1s, when normally if you do for example
<img src="direct_url_to_png">
it takes about 44ms.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 Varnish 来缓存图像,Varnish 是位于网络服务器前面的 HTTP 代理。这需要一些时间&知识来配置它,但它是一个非常的软件。
或者,您可以使用共享内存将图像缓存在内存中, memcache,或 redis 。这可能不如 Varnish 有效,但它会节省文件系统 IO。
您应该测试花费最多时间的地方,我希望它是文件 I/O 和 PHP 解析时间的组合,但正如我们用荷兰语所说,“测量就是了解”;)
这是一个简单的示例,应该可以让您了解总体思路关于如何做到这一点:
另一个可能的选择是使用 HTML/CSS 来显示图表。这可能有效也可能无效,具体取决于您的图表的外观,但我过去使用过它并且效果很好。
You can use Varnish to cache the images, Varnish is a HTTP proxy that sits in front of the webserver. It takes some time & knowledge to configure it, but it is a very good piece of software.
Alternatively, you may cache the images in memory using shared memory, memcache, or redis. This would probably be less effectly than Varnish, but it would save the filesystem IO.
You should test where the most time is spent, I expect it's a combination of file I/O and PHP parsing time, but as we saying in Dutch, "Measuring is knowing" ;)
Here's a simple example which should give you the general idea on how to do that:
Another thing that may be an option is to use HTML/CSS to show the graphs. This may or may not work depending in what your graphs look like but I've used this in the past and it worked quite well.