cache (VaryByParam) doesn't work

D

Dmitry Duginov

Hi,

I have image.aspx that can accept 3 query string parameters (FolderID,
ImageID, Size) and it returns full jpg image or image thumbnail (depending
on size).

I included the following directive:

<%@ OutputCache Duration="3600" VaryByParam="FolderID;ImageID;Size" %>

to cache images/thumbnails for an hour

The images ate taken programmaticaly from file system and returned this way:

Response.ContentType = "image/jpeg";

image.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
// for thumbnails

//or

//Response.WriteFile(imagepath); // for full images

Response.End;

But I noticed that caching doesn't work. After first request for specific
picture all subsequent calls execute the code over and over again instead
taking the first result from cache. What I am missing and how to fix it?



Regards,

Dmitry
 
H

Hermit Dave

I have been in a similar situation before.... here is what i suggest you do.

a. instead of writing an aspx to handle image rendering and cacheing.. use a
custom handler .ashx (about 5 % faster than .aspx).
b. instead of caching image and its thumbnail (i presume you getting
thumbnail by calling Image.GetThumbnailImage()) you could just catch the
image and resize it every time.. it will balance the extra memory
utilisation.
c. Do you really want to catch for one hour ? how many images do you have
and what is the configuration of the server ?
d. use smart caching.. ie use sliding time expiration and use a value like
say 10 mins.. if the image is accessed in the next 10 mins it will be reset.
you will just overload the server by caching for an hour..
e. i went through this article like an year back and i almost does
everything you are trying to do. you might not want a custom extension.. if
so just use .ashx.

http://www.microsoft.com/belux/nl/msdn/community/columns/desmet/httphandler.mspx

--

Regards,

Hermit Dave (D'way)
http://hdave.blogspot.com
 
D

Dmitry Duginov

Well, thank you, maybe I'll follow some of your recommendations, but now I'd
like to know why caching doesn't work in my current solution.

Dmitry.
 
S

Steven Cheng[MSFT]

Hi Dmitry,

Thanks for your posting. As for the problem you mentioned, I've also tested
and found that. It is likely due to the the asp.net's outputCache's
internal mechanism on evaluating the comming request's CAchekey. Anyway,
I'll do some further consulting to see whether there is any detailed
explanition. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
D

Dmitry Duginov

Thanks for your posting. As for the problem you mentioned, I've also tested
and found that. It is likely due to the the asp.net's outputCache's
internal mechanism on evaluating the comming request's CAchekey. Anyway,
I'll do some further consulting to see whether there is any detailed
explanition. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Thank you Steven, I'll try some workarounds meanwhile. I hope you'll find
what's going on and let me know.

Dmitry
 
S

Steven Cheng[MSFT]

Hi Dmitry,

After some further research, I think the problem is caused by the
Response.End(or Response.Flush) we called after we output the image. This
will make the current request's thread be terminated and the response
content be sent to client immediatly, so some further ASP.NET page
processing haven't been finished and the Output Caching is one thing
that'll be done at that time, so we need to remove the Response.End or
Response.Flush after we write out the image stream. Below is a sample page
code, I've tested on my side.

========================
private void Page_Load(object sender, System.EventArgs e)
{
RenderImage(DateTime.Now.ToLongTimeString());

}

private void RenderImage(string text)
{
Response.ClearContent();
Response.ContentType="Image/gif";


Bitmap bmp = new Bitmap(400,300);
Graphics graphic = Graphics.FromImage(bmp);
graphic.FillRectangle(new
SolidBrush(Color.Yellow),0,0,bmp.Width,bmp.Height);
graphic.DrawString(text,new Font("Tahoma",
30,System.Drawing.FontStyle.Bold),new
SolidBrush(Color.Blue),50,50,StringFormat.GenericDefault);


bmp.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Gif);
bmp.Dispose();

//don't call Response.End or Response.Flush here
}
=========================

Please have a try to see whether it works. Hope help. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
D

Dmitry Duginov

Steven Cheng said:
Hi Dmitry,

After some further research, I think the problem is caused by the
Response.End(or Response.Flush) we called after we output the image. This
will make the current request's thread be terminated and the response
content be sent to client immediatly, so some further ASP.NET page
processing haven't been finished and the Output Caching is one thing
that'll be done at that time, so we need to remove the Response.End or
Response.Flush after we write out the image stream. Below is a sample page
code, I've tested on my side.

Thank you, your example works fine. But I already rebuild this thing using
HttpHandler and explicit data caching.

It would be good to mention thit Response.End and .Flush behavior in
connection with OutputCache somewhere in documentation...

Regards,
Dmitry
 
S

Steven Cheng[MSFT]

Thanks for your followup Dmitry,

Well, I'm glad that you've alreay worked through the problem by using the
HttpHandler, infact, that's also the recommended means for service image in
web application. Also, as for this problem, it is also my mistake that I
should have found the cause early( the Response.End break down the caching
mechanism). Anyway, thanks again for your posting and understanding.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top