Gavin Coates
Gavin Coates

Reputation: 1425

Does the browser cache dynamic images?

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?

Upvotes: 4

Views: 4211

Answers (3)

James McCormack
James McCormack

Reputation: 9954

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.

Upvotes: 0

Hnatt
Hnatt

Reputation: 5935

Images are cached by their URLs. If URL has not changed since last load of the page, the image will be taken from cache.

Upvotes: -2

zzzzBov
zzzzBov

Reputation: 179086

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.

Upvotes: 3

Related Questions