问题描述
我有这个网站,从数据库(SQL2008)流图像,我认为这是一个引起我的服务器上非常高的CPU使用率。 CPU使用率至少为60-90%。
I have this site that streams images from the database (SQL2008), and I think it's the one causing the very high CPU usage on my server. The CPU usage is at least 60-90%.
我使用MVC3以下是我的控制器的code。该图像发送到视图:
I am using MVC3 and below is the code in my Controller that sends the image to the View:
[OutputCache(Duration = 86400, VaryByParam = "GUID")]
public FileStreamResult GetFile(string guid)
{
Guid id = new Guid(guid);
Thumbnail thumbnail = thumbService.GetThumbnailByGUID(id);
Stream stream = new MemoryStream(thumbnail.FileContent.ToArray());
var fsr = new FileStreamResult(stream, "image");
return fsr;
}
视图:
<div style="background:url('@url');background-repeat:no-repeat;background-position:50% top;background-color:#fff;" class="photoThumb">
以上是@url /的GetFile / GUID
The @url above is /GetFile/guid
谁能告诉我什么,我做错了吗?
Can anyone tell me what I'm doing wrong?
感谢
更新的答案,另一个问题:
Updates on the answer and another question:
在[的OutputCache(持续时间= 86400,的VaryByParam =GUID)]从回答以下工作,提高了站点的性能。 CPU使用率已经下降到8-60%,但我也想确保一切配置,所以我想知道,如果FileStreamResult做,对我或者我应该做手工?
The [OutputCache(Duration = 86400, VaryByParam = "GUID")] from the answer below worked and has improved the performance of the site. The CPU usage has now went down to 8-60% but I also want to make sure that everything is disposed so I want to know if FileStreamResult is doing that for me or should I do it manually?
推荐答案
我猜有每个图像请求的数据库访问,每个页面请求多张图片 - 这可能会导致CPU使用率过高。你应该尝试缓存图像。
I'd guess that there is a database hit for each image request, with multiple images per page request - this could lead to high CPU usage. You should try caching the images.
如果您与装饰你的行动
[的OutputCache(持续时间= 86400,的VaryByParam =ID)]
这应该缓存的结果图像一天,你的服务器意味着更少的数据库查询,并希望更少的负载。
this should cache the result image for one day, meaning fewer db queries, and hopefully less load on your server.
这篇关于从数据库MVC3 +图像流导致我的服务器上非常高的CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!