HighTechTalks DotNet Forums  

http handler image caching

ASP.net Caching microsoft.public.dotnet.framework.aspnet.caching


Discuss http handler image caching in the ASP.net Caching forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Andrew Robinson
 
Posts: n/a

Default http handler image caching - 03-14-2005 , 06:50 PM






I am using a http handler to read and generate dynamic images from SQL and
other sources. The following code snippet works fine, but caching needs some
work. (I have not provided all of my image code.)

The image does appear to be cached at the server, as there is little or no
SQL activity when a client is repeatedly hitting the web site, but there
does seem to be a lot of w3wp.exe activity. (Somewhat unscientific.)

What am I missing to get the client to cache the image and not make requests
to the server?

Thanks,

Quote:
public class Content : IHttpHandler
{
public void ProcessRequest(HttpContext httpContext)
{
httpContext.Response.Cache.SetExpires(DateTime.Tod ay.AddDays(1));
httpContext.Response.Cache.SetCacheability(HttpCac heability.Public);

// httpContext.Response.Cache.SetLastModified(DateTim e.Today); ??


httpContext.Response.ContentType = "image/jpeg";

//
// get and write image
//

httpContext.Response.BinaryWrite((byte[])dr["ContentStream"]);




Reply With Quote
  #2  
Old   
AT
 
Posts: n/a

Default RE: http handler image caching - 03-14-2005 , 09:20 PM






Hello,

Can you explain more on "there is little or no SQL activity when a client
is repeatedly hitting the web site, but there does seem to be a lot of
w3wp.exe activity. "? If you create a trace with SQL server profile and
check if it will retrieve data from database everytime you refresh the
client page. If it just query the database once, the client should cache
the image.

Luke


Reply With Quote
  #3  
Old   
Joerg Jooss
 
Posts: n/a

Default Re: http handler image caching - 03-15-2005 , 04:35 PM



Andrew Robinson wrote:

Quote:
I am using a http handler to read and generate dynamic images from
SQL and other sources. The following code snippet works fine, but
caching needs some work. (I have not provided all of my image code.)

The image does appear to be cached at the server, as there is little
or no SQL activity when a client is repeatedly hitting the web site,
but there does seem to be a lot of w3wp.exe activity. (Somewhat
unscientific.)
It's potentially cached "everywhere" -- you're specifying
Cache-Control: public.

Quote:
What am I missing to get the client to cache the image and not make
requests to the server?
Are you sure you're not just seeing conditional GET requests that
result in 304 responses?

Cheers,
--
http://www.joergjooss.de
mailto:news-reply (AT) joergjooss (DOT) de


Reply With Quote
  #4  
Old   
geedubb
 
Posts: n/a

Default Re: http handler image caching - 03-31-2005 , 01:20 PM




Hi Andrew

I too have the same problem. Did you get a resolution? I have trie
using HTTP Fiddler to check the HTTP response codes and connot for th
life of me work out which Response.Cache headers to send to allow th
client to cache the image, and result in an HTTP 304 response.

(I wish this was as easy and less confusing to do this like with PHP!)

Here's what I am sending (context is the current HTTPContext):

context.Response.Clear();
context.Response.ContentType = "image/png";
context.Response.Cache.SetExpires(DateTime.Now.Add Days(1));
context.Response.Cache.SetCacheability(HttpCacheab ility.Public);
context.Response.Cache.SetValidUntilExpires(true);
context.Response.Cache.VaryByParams["text"] = true;

(as far as I can see Andrew this is essentially the same)

Any help appreciated.

Cheers
G


-
geedub
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------


Reply With Quote
  #5  
Old   
geedubb
 
Posts: n/a

Default Re: http handler image caching - 03-31-2005 , 01:32 PM




Hi

I meant to ask - Andrew what is the filetype of the 'path' attribute in
your web.config that ties the request to the handler in the code
behind?

eg. mine is:

<add verb="GET" path="title.axd" type="... etc

are you using a .jpg file, or an axd like me? I chose to use an axd
after reading a good article, but does this affect how the browser
deals with such a file? Would it make any difference if I used a .jpg
and reconfigured IIS to process it correctly?

TIA
GW



--
geedubb
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------


Reply With Quote
  #6  
Old   
Andrew Robinson
 
Posts: n/a

Default Re: http handler image caching - 04-01-2005 , 06:56 PM



GW,

A couple of things:

1. I am not making any changes to my web.config to register the handle. I
simple include a file of type .ashx and it has a single line in it:

<%@ WebHandler Class="AbcProject.WebTier.Content" %>
then I have a class in my project with the name: Content and the name
space: AbcProject.WebTier

Seems to work well and I don't have to muck with the config file.

2. I am setting the following cach parameters:

httpContext.Response.Cache.SetExpires(DateTime.Now .AddMinutes(20));
httpContext.Response.Cache.SetCacheability(HttpCac heability.Public);
httpContext.Response.Cache.SetValidUntilExpires(tr ue);
httpContext.Response.Cache.SetLastModified(created Date);


IE seems to cach at the browser. Firefox does not. I wounder if this is
somehow related to the browser caps config info or lack of for Firefox.?


"geedubb" <geedubb.1ms39r (AT) mail (DOT) codecomments.com> wrote

Quote:
Hi

I meant to ask - Andrew what is the filetype of the 'path' attribute in
your web.config that ties the request to the handler in the code
behind?

eg. mine is:

add verb="GET" path="title.axd" type="... etc

are you using a .jpg file, or an axd like me? I chose to use an axd
after reading a good article, but does this affect how the browser
deals with such a file? Would it make any difference if I used a .jpg
and reconfigured IIS to process it correctly?

TIA
GW



--
geedubb
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------




Reply With Quote
  #7  
Old   
Joerg Jooss
 
Posts: n/a

Default Re: http handler image caching - 04-04-2005 , 04:11 PM



Andrew Robinson wrote:

Quote:
GW,

A couple of things:

1. I am not making any changes to my web.config to register the
handle. I simple include a file of type .ashx and it has a single
line in it:

%@ WebHandler Class="AbcProject.WebTier.Content" %
then I have a class in my project with the name: Content and the name
space: AbcProject.WebTier

Seems to work well and I don't have to muck with the config file.

2. I am setting the following cach parameters:

httpContext.Response.Cache.SetExpires(DateTime.Now .AddMinutes(20));
httpContext.Response.Cache.SetCacheability(HttpCac heability.Public);
httpContext.Response.Cache.SetValidUntilExpires(tr ue);
httpContext.Response.Cache.SetLastModified(created Date);


IE seems to cach at the browser. Firefox does not. I wounder if this
is somehow related to the browser caps config info or lack of for
Firefox.?
I would add a call

httpContext.Response.Cache.SetMaxAge(new TimeSpan(0, 20, 0));

since this is the preferred way to specify expiration times in HTTP 1.1.

Did you verify that Firefoy really does try to fetch the image again?

Cheers,
--
http://www.joergjooss.de
mailto:news-reply (AT) joergjooss (DOT) de


Reply With Quote
  #8  
Old   
Andrew Robinson
 
Posts: n/a

Default Re: http handler image caching - 04-04-2005 , 04:30 PM



Joerg,

Thanks for the info. Added the line, but it appears that the same thing is
still happening.

I am using SQL Profiler to see that there is SQL activity.

When I run the same page with IE6, I get no SQL activity.

hummm?


-Andrew



"Joerg Jooss" <news-reply (AT) joergjooss (DOT) de> wrote

Quote:
Andrew Robinson wrote:

GW,

A couple of things:

1. I am not making any changes to my web.config to register the
handle. I simple include a file of type .ashx and it has a single
line in it:

%@ WebHandler Class="AbcProject.WebTier.Content" %
then I have a class in my project with the name: Content and the name
space: AbcProject.WebTier

Seems to work well and I don't have to muck with the config file.

2. I am setting the following cach parameters:

httpContext.Response.Cache.SetExpires(DateTime.Now .AddMinutes(20));
httpContext.Response.Cache.SetCacheability(HttpCac heability.Public);
httpContext.Response.Cache.SetValidUntilExpires(tr ue);
httpContext.Response.Cache.SetLastModified(created Date);


IE seems to cach at the browser. Firefox does not. I wounder if this
is somehow related to the browser caps config info or lack of for
Firefox.?

I would add a call

httpContext.Response.Cache.SetMaxAge(new TimeSpan(0, 20, 0));

since this is the preferred way to specify expiration times in HTTP 1.1.

Did you verify that Firefoy really does try to fetch the image again?

Cheers,
--
http://www.joergjooss.de
mailto:news-reply (AT) joergjooss (DOT) de



Reply With Quote
  #9  
Old   
Joerg Jooss
 
Posts: n/a

Default Re: http handler image caching - 04-06-2005 , 04:15 PM



Andrew Robinson wrote:

Quote:
Joerg,

Thanks for the info. Added the line, but it appears that the same
thing is still happening.

I am using SQL Profiler to see that there is SQL activity.

When I run the same page with IE6, I get no SQL activity.

hummm?
You should rather trace the network traffic before looking for DB hits.
Have a look at Fiddler (www.fiddlertool.com).

Cheers,
--
http://www.joergjooss.de
mailto:news-reply (AT) joergjooss (DOT) de


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.