Groups | Blog | Home
all groups > asp.net caching > august 2006 >

asp.net caching : Problems Caching dynamic images in the HttpHandler...


emer.kurbegovic NO[at]SPAM gmail.com
8/8/2006 12:29:52 AM
I've got a custom built HttpHandler that I use to display the image
blobs from the db. I am getting the image straight from the db, resize
it if neccessary, cache it and display on the web page. The problem I
am having is that, eventhough the images are being cached in my temp
internet folder they are reloaded from the db on each request.

Actually, when i rebuild the complete solution the images get cached
and they are also pulled from the cache on each call. but if i use my
web app for few minutes, all of the sudden, i get few images here and
there that are not being pulled from the cache. The more I use my web
app the more pics quit being loaded from the cache.

i have no clue what am i doing wrong here... Is this a bug and if so
how do I fix it?

Why are my images being saved to cache, then being pulled from cache
and then just simply quit being pulled from cache?



Images are loaded like this:
<img border="0"
src="image.axd?type=Cat1&amp;id=e7810e9f-2abe-4a54-9b58-d82cd2b8fa6d&amp;width=150"
/>

I cache the image like this in the HttpHandler:
context.Cache.Insert(cacheKey, picInfo);
context.Response.Clear();
context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(expireInMinutes));
context.Response.Cache.SetCacheability(httpCacheability);
context.Response.Cache.SetValidUntilExpires(true);
context.Response.Cache.SetLastModified(DateTime.Now);
context.Response.Cache.SetMaxAge(new TimeSpan(0, expireInMinutes, 0));

Then I output the image like this:
context.Response.ContentType = picInfo.ContentType;
context.Response.BufferOutput = false;
context.Response.OutputStream.Write(picInfo.PicBytes, 0,
picInfo.PicBytes.Length);
emer.kurbegovic NO[at]SPAM gmail.com
8/9/2006 2:34:30 PM
i can't believe nobody knows what i am talking about...

[quoted text, click to view]
Alvin Bruney [MVP]
8/12/2006 6:20:47 PM
[quoted text, click to view]
We do, it's probably not that interesting that somebody would actually take
a look.

You haven't shown the code that tests the cache. If you don't have it, you
need to add it in there. The problem you are most likely running into is
cache scavenging. The run-time makes no guarantee that what you put in cache
will be available when you need it. Your code needs to take that into
account. By testing the cache for null and loading it as appropriate, the
problem will be reduced. However, you'll still run into it under heavy load
because of concurrent accesses when the cache is flushed.

--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------


[quoted text, click to view]

emer.kurbegovic NO[at]SPAM gmail.com
8/14/2006 9:31:33 AM
Alvin,
thanks for replying...

basically i am trying to cache the dynamic images in the client's
browser cache so that they are not requested from the db on every
request. the pics are currently in the db and i would like to keep them
there (i don't want to save them on a separate server). i thought the
way i was doing this would accomplish that. i don't really need the
pics to be saved in the server's cache, just the client's browser
cache.


thank you,
emer


[quoted text, click to view]
Alvin Bruney [MVP]
8/14/2006 8:48:46 PM
Ok, well that's the problem. Since the cache is on the client, different
clients connecting to the application will not have cached images initially.
Therefore your application will keep servicing requests for the same images
over and increasing the load on the db unnecessarily. Granted, once a client
receives the image, they likely will read from cache on the subsequent
visit, however as you can see, this heralds poor performance for new
clients, or clients with disable client caches (for one reason or the
other), or clients who run multiple browsers etc etc.

The better approach is to cache on the server, that way no matter who
connects or what the state of the browser is, they always read from cache
and pull from the db exactly once irrespective of load, browser usage etc
etc. (well not really exactly once because the cache can flush but you get
my drift)

--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------


[quoted text, click to view]

emer.kurbegovic NO[at]SPAM gmail.com
8/15/2006 9:15:41 AM
i currently do not cache on the server at all. i wanted to make sure
the pics are
caching properly in the client's browser first. i am aware that caching
in the browser depends
on client's cache settings. the things is that, even though everything
is setup correctly on my own client,
the images quit loading from the local browser cache folder after few
minutes.

i am going to setup a file cache provider for the images on the server.
right now i am trying to figure out why the images quit being loaded
from the browser cache. does it matter that the images are cached like
this: "image.axd?type=cat1&id=343433343&width=150".

Does querystring attached to the image has anything to do with this
behavior?

thank you

[quoted text, click to view]
emer.kurbegovic NO[at]SPAM gmail.com
8/15/2006 9:16:24 AM
no, i don't use js on the browser to preload images.
[quoted text, click to view]
Freddie
8/15/2006 3:40:17 PM
do u use JS on the browser to preload images?

[quoted text, click to view]
liuchenzhong NO[at]SPAM hotmail.com
9/8/2006 8:43:04 PM
There are number of ways to cache your images

[quoted text, click to view]

Hope this helps

Byron



[quoted text, click to view]
AddThis Social Bookmark Button