我目前正在开发一个将由多个客户使用的网络应用程序,我希望在每个页面上显示客户的徽标。该应用程序是使用 ASP.NET MVC3 和 C# 开发的。
正常惯例建议将图像存储在数据库中,但考虑到该图像将显示在每个页面上,我担心这样做会对性能造成影响。如果我将图像存储在文件系统上,浏览器将缓存该图像并且不会每次都请求它。
如果我将图像存储在数据库中,浏览器是否也会缓存该图像,或者每次都会调用函数来请求图像?
在这种情况下,我是否将图像存储在服务器的硬盘上会更好?
I am currently developing a web application which will be used by multiple clients, and I wish to display the clients logo on each page. The application is being developed using ASP.NET MVC3 and C#.
Normal convention suggests storing images in a database, but given that this image will be displayed on every page, I am concerned about the performance hit of doing this. If I store the image on the file system, the browser will cache the image and wont request it each time.
If I store the image in the database, will the browser also cache this, or will it call the function to request the image each time?
In such a case would I be better off storing the images on the hard disk of the server?
发布评论
评论(3)
(现代)浏览器不知道服务器如何存储图像。如果 URL 相同,浏览器将只提取缓存的版本,除非明确告知不要这样做。
在某些情况下,您可能需要禁用缓存:
http://www.example.com/random-image
可能会在每次访问时提供不同的图像,并且浏览器将缓存第一个图像图像已送达。The (modern) browser doesn't know what the server did to store the image. If the URL is the same, the browser will just pull up the cached version unless it was explicitly told not to.
In some circumstances you may need to disable caching:
http://www.example.com/random-image
would probably serve up a different image each time it was accessed, and the browser would cache the first image served up.在动态图像控制器中适当设置 Response.Cache 标头,它来自动态源并不重要。
查看这篇文章,了解使用 MVC ActionFilter 设置缓存的想法: http://weblogs.asp.net/rashid/archive/2008/03/28/asp-net-mvc-action-filter-caching-and-compression.aspx。它还说明了您需要调整的标题。
Set the Response.Cache headers appropriately in your dynamic image controller, and it won't matter that it comes from a dynamic source.
Check this post for an idea for setting caching using an MVC ActionFilter: http://weblogs.asp.net/rashid/archive/2008/03/28/asp-net-mvc-action-filter-caching-and-compression.aspx. It also illustrates the headers you'll need to tweak.
图像通过其 URL 进行缓存。如果自上次加载页面后 URL 未发生更改,则将从缓存中获取图像。
Images are cached by their URLs. If URL has not changed since last load of the page, the image will be taken from cache.