.NET Forum / ASP.NET / Caching / September 2006
Problems Caching dynamic images in the HttpHandler...
|
|
Thread rating:  |
emer.kurbegovic@gmail.com - 08 Aug 2006 08:29 GMT 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&id=e7810e9f-2abe-4a54-9b58-d82cd2b8fa6d&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@gmail.com - 09 Aug 2006 22:34 GMT i can't believe nobody knows what i am talking about...
@gmail.com wrote:
> 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 [quoted text clipped - 33 lines] > context.Response.OutputStream.Write(picInfo.PicBytes, 0, > picInfo.PicBytes.Length); Alvin Bruney [MVP] - 12 Aug 2006 23:20 GMT >i can't believe nobody knows what i am talking about... 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.
 Signature ________________________ 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 -------------------------------------------------------
>i can't believe nobody knows what i am talking about... > [quoted text clipped - 36 lines] >> context.Response.OutputStream.Write(picInfo.PicBytes, 0, >> picInfo.PicBytes.Length); emer.kurbegovic@gmail.com - 14 Aug 2006 17:31 GMT 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
> >i can't believe nobody knows what i am talking about... > We do, it's probably not that interesting that somebody would actually take [quoted text clipped - 60 lines] > >> context.Response.OutputStream.Write(picInfo.PicBytes, 0, > >> picInfo.PicBytes.Length); Alvin Bruney [MVP] - 15 Aug 2006 01:48 GMT 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)
 Signature ________________________ 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 -------------------------------------------------------
> Alvin, > thanks for replying... [quoted text clipped - 78 lines] >> >> context.Response.OutputStream.Write(picInfo.PicBytes, 0, >> >> picInfo.PicBytes.Length); emer.kurbegovic@gmail.com - 15 Aug 2006 17:15 GMT 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
> 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. [quoted text clipped - 105 lines] > >> >> context.Response.OutputStream.Write(picInfo.PicBytes, 0, > >> >> picInfo.PicBytes.Length); Freddie - 15 Aug 2006 13:40 GMT do u use JS on the browser to preload images?
> Alvin, > thanks for replying... [quoted text clipped - 76 lines] >>>>context.Response.OutputStream.Write(picInfo.PicBytes, 0, >>>>picInfo.PicBytes.Length); emer.kurbegovic@gmail.com - 15 Aug 2006 17:16 GMT no, i don't use js on the browser to preload images.
> do u use JS on the browser to preload images? > [quoted text clipped - 78 lines] > >>>>context.Response.OutputStream.Write(picInfo.PicBytes, 0, > >>>>picInfo.PicBytes.Length); liuchenzhong@hotmail.com - 08 Sep 2006 20:43 GMT There are number of ways to cache your images
>> Put your image control on to a user control and do partial caching on the >> control. set the outputcachelocation to "downstream". The output cache >> can be stored in any HTTP 1.1 cache-capable devices other than the origin >> server. This includes proxy servers and the client that made the request.
>> cache your images on the file systems of the presentation layer if there >> is sufficient resource available on the layer.
>> write content expiry settings into the http header when returns your >> image. Hope this helps
Byron
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 [quoted text clipped - 32 lines] > context.Response.OutputStream.Write(picInfo.PicBytes, 0, > picInfo.PicBytes.Length);
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|