the "it's free" problem: caching of images

a long time ago journalists were paid for investigate in story's. Today content is almost free. Blogger's dominate the world. The problem is also if 99% of the information is true and useful, 1% is wrong and dangerous. I found the following free article in web which describes some issues with images and caching.

http://www.codeproject.com/KB/aspnet/CachingImagesInASPNET.aspx

The general caching topic is not easy, it depends on a lot of variables. But caching of images is solved in a quite nearly perfect matter with IIS 6.

The article suggest that you have to write instead of default handling a ashx handler. Then you add some header information in response and send image to client like:

Response.Cache.SetExpires(Date.Now.AddHours(3))

Response.Cache.SetCacheability(HttpCacheability.Public)

Response.Cache.SetValidUntilExpires(False)

Response.AddHeader("content-dispostion", "inline: default.aspx")

You can do that also with a aspx page, should be the same result.

in short word's: thats wrong! 

1) i would not suggest network monitor to watch http traffic. Tools like fiddler are specialized for that task

2) i would not reinvent the wheel. Caching of images is solved

3) i would not redirect requests through additional code, especially when the amount of request is high and you want to optimize performance

4) Caching should be done in a 200- 304 matter. Server must decided if newer information is available and send a 304 message or the new content with code 200

5) caching of sample page of the author doesn't work like fiddler shows

 

don?t believe anything- including this post ;-)

Published Tuesday, January 22, 2008 3:36 PM by preishuber
Filed under:

Comments

# re: the "it's free" problem: caching of images

Friday, January 25, 2008 9:20 AM by Karin

I am very sorry if my article is ambiguous. I never wanted to say that you should not use IIS caching. On the contrary, I think it is the best and easiest way, too. Unfortunately I do not have access to the IIS where my web site is hosted. I only can transfer files to the server via FTP. So I had to find another solution.

The main goal was to cache images on the client to avoid unnecessary requests to the server. When done in a 200 - 304 matter the client asks the server for every single image. At best the server sends a 304 response. But this does not reduce the number request. The way I solved the problem (of course you could do that by IIS too) the client knows that it does not need to ask the server for the next 24 hours, so the number of requests gets lower. I found a good article on this at msdn2.microsoft.com/.../bb250442.aspx which says "Because an HTTP/304 response contains only headers and no body, it crosses the network much more quickly than if the full resource had been re-downloaded. However, even an HTTP/304 requires a full roundtrip to the remote Web server; by carefully setting response headers, a Web application developer can eliminate the need to issue even conditional requests."

I tested this with fiddler for www.software-architects.com and got exactly the same results as with Microsoft Network Monitor. For the first request and when pressing F5 the client sends a request for every file to the server (including images, css, js). But when clicking a link to the same or another page the client does not ask for the images ending with .ashx. Less requests are necessary.

If there is an easier solution to reduce the number of requests without touching IIS please let me know. I would really be interested in it.

# re: the "it's free" problem: caching of images

Saturday, February 09, 2008 6:14 AM by preishuber

if the task is:

"add additonal header information on specific extension"

then i would use httphandler

# re: the "it's free" problem: caching of images

Tuesday, May 27, 2008 4:41 AM by Mojtaabaa

i think that when we don't have access to iis on host,we may use solution that may look funny or wrong to those who had access to iis.

# re: the "it's free" problem: caching of images

Monday, September 15, 2008 2:30 PM by zaladane

I don't have access to IIS (share hosting). how to i set the expire header for images other than the method using the http handler?

thanks

Leave a Comment

(required) 
(required) 
(optional)
(required)