Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Cache Images vs Static Images

Reply
Thread Tools

Cache Images vs Static Images

 
 
wardemon
Guest
Posts: n/a
 
      06-14-2007
Hi All,
I have a aspx page named: ImageProcess.aspx that creates a thumbnail
version of an image by passing the ImagePath, width, and height. The
ImagePath is taken from a table from a database, while width and
height is user specific - meaning user can change this on demand.

My question is, although this rendering images on the fly is cool, I
would like to implement some sort of mechanism/logic that it wont keep
rendering the thumbnail again and again.

I thought of two things that might solve my problem, however I am
unsure of the pros and cons and which is much faster for the client.

Solution #1: Caching
----------------------------------
using System.Web.Caching;
Cache Cache = context.Cache;

//Get the image
string path = Server.MapPath(Request.FilePath);

//Check the cache
string key = path + "-" + strWidth + "-" strHeight;
if (Cache[key] != null)
{
Image cached = (Image) Cache[key];
cached.Save(Response.OutputStream,ImageFormat.Jpeg );
return;
}
else
{
Image img = Image.FromFile(path);

//Add to the cache
Cache.Add(key, thumb, null, DateTime.Now.AddSeconds(10),
TimeSpan.Zero, CacheItemPriority.High,
new CacheItemRemovedCallback(this.RemovedCallback));
}

this solution checks if a "Cache Key" exists for a particular ImagePath
+Width+Height combo exsits, then it uses the "Saved Cached Image", if
none, it will use the image directly from the Image.FromFile(path) and
adds a new "Cache Key" for it.


Solution #2: Static Images
----------------------------------
1. Create the static image that was generated on the fly by the
ImageProcess.aspx to a directory in the webserver ex: Root
\ThumbnailImages\
2. The static image will be named as ImagePath-ImageName-ImageWidth-
ImageHeight-ImageDateCreated.ImageExtension

Where
ImagePath = Image's Full Path taken from a table.column in a db
ImageName = Image File Name taken from a table column in a db
ImageWidth = Client User specific
ImageHeight = Client User spefici
ImageDateCreated = Image Upload DateTime taken from a table.column in
a db.

3. Subsequent request logic will be:
3.1 First check if the combination filename exists in the Root
\ThumbnailImages\ folder
3.2 If it exist, use that static image
3.2 If it does not exist, create image on the fly using
ImageProcess.aspx & create a thumbnail image in the thumbnail Root
\ThumbnailImages\ folder


I am a bit confuse on which to use. I am after - which is faster
Lastly, if I use Solution#1: Caching, when the user exists his or her
browser, then opens a new browser, will the "Cache Keys" still be
there? In other words, are the "Cache Keys" stored in the webserver
for use by other concurrent users?

Thanks,
Henry Wu

 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      06-14-2007

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
Solution 2 is better, as it doesn't rely on memory to cache the images.
However, I have one suggestion: You don't need to keep the date created in
the image file name. That is an attribute of the file.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"wardemon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hi All,
> I have a aspx page named: ImageProcess.aspx that creates a thumbnail
> version of an image by passing the ImagePath, width, and height. The
> ImagePath is taken from a table from a database, while width and
> height is user specific - meaning user can change this on demand.
>
> My question is, although this rendering images on the fly is cool, I
> would like to implement some sort of mechanism/logic that it wont keep
> rendering the thumbnail again and again.
>
> I thought of two things that might solve my problem, however I am
> unsure of the pros and cons and which is much faster for the client.
>
> Solution #1: Caching
> ----------------------------------
> using System.Web.Caching;
> Cache Cache = context.Cache;
>
> //Get the image
> string path = Server.MapPath(Request.FilePath);
>
> //Check the cache
> string key = path + "-" + strWidth + "-" strHeight;
> if (Cache[key] != null)
> {
> Image cached = (Image) Cache[key];
> cached.Save(Response.OutputStream,ImageFormat.Jpeg );
> return;
> }
> else
> {
> Image img = Image.FromFile(path);
>
> //Add to the cache
> Cache.Add(key, thumb, null, DateTime.Now.AddSeconds(10),
> TimeSpan.Zero, CacheItemPriority.High,
> new CacheItemRemovedCallback(this.RemovedCallback));
> }
>
> this solution checks if a "Cache Key" exists for a particular ImagePath
> +Width+Height combo exsits, then it uses the "Saved Cached Image", if
> none, it will use the image directly from the Image.FromFile(path) and
> adds a new "Cache Key" for it.
>
>
> Solution #2: Static Images
> ----------------------------------
> 1. Create the static image that was generated on the fly by the
> ImageProcess.aspx to a directory in the webserver ex: Root
> \ThumbnailImages\
> 2. The static image will be named as ImagePath-ImageName-ImageWidth-
> ImageHeight-ImageDateCreated.ImageExtension
>
> Where
> ImagePath = Image's Full Path taken from a table.column in a db
> ImageName = Image File Name taken from a table column in a db
> ImageWidth = Client User specific
> ImageHeight = Client User spefici
> ImageDateCreated = Image Upload DateTime taken from a table.column in
> a db.
>
> 3. Subsequent request logic will be:
> 3.1 First check if the combination filename exists in the Root
> \ThumbnailImages\ folder
> 3.2 If it exist, use that static image
> 3.2 If it does not exist, create image on the fly using
> ImageProcess.aspx & create a thumbnail image in the thumbnail Root
> \ThumbnailImages\ folder
>
>
> I am a bit confuse on which to use. I am after - which is faster
> Lastly, if I use Solution#1: Caching, when the user exists his or her
> browser, then opens a new browser, will the "Cache Keys" still be
> there? In other words, are the "Cache Keys" stored in the webserver
> for use by other concurrent users?
>
> Thanks,
> Henry Wu
>



 
Reply With Quote
 
 
 
 
wardemon
Guest
Posts: n/a
 
      06-14-2007
Hi Kevin,
Thanks for you reply. I'll do solution 2 then.

Sorry, it's actually ImageDateLastModified and not ImageDateCreated,
making the filename:
ImagePath-ImageName-ImageWidth-ImageHeight-
ImageDateLastModified.ImageExtension

The ImageDateLastModified will be retreived from a table.column in the
DB.
This is needed to handle cases where the user overwrites the image of
the same file name.

Just curious though, would you know if I use Solution#1: Caching,
will the "Cache Keys" objects be stored in the webserver for use by
other concurrent users?
Thus maximizing cached objects?

Thanks,
Henry Wu



On Jun 14, 7:07 pm, "Kevin Spencer" <(E-Mail Removed)> wrote:
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
>
> Printing Components, Email Components,
> FTP Client Classes, Enhanced Data Controls, much more.
> DSI PrintManager, Miradyne Component Libraries:http://www.miradyne.net
> Solution 2 is better, as it doesn't rely on memory tocachethe images.
> However, I have one suggestion: You don't need to keep the date created in
> theimagefile name. That is an attribute of the file.
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
>
> Printing Components, Email Components,
> FTP Client Classes, Enhanced Data Controls, much more.
> DSI PrintManager, Miradyne Component Libraries:http://www.miradyne.net
>
> "wardemon" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) ups.com...
>
>
>
> > Hi All,
> > I have a aspx page named: ImageProcess.aspx that creates a thumbnail
> > version of animageby passing the ImagePath, width, and height. The
> > ImagePath is taken from a table from a database, while width and
> > height is user specific - meaning user can change this on demand.

>
> > My question is, although this rendering images on the fly is cool, I
> > would like to implement some sort of mechanism/logic that it wont keep
> > rendering the thumbnail again and again.

>
> > I thought of two things that might solve my problem, however I am
> > unsure of the pros and cons and which is much faster for the client.

>
> > Solution #1: Caching
> > ----------------------------------
> > using System.Web.Caching;
> >CacheCache= context.Cache;

>
> > //Get theimage
> > string path =Server.MapPath(Request.FilePath);

>
> > //Check thecache
> > string key = path + "-" + strWidth + "-" strHeight;
> > if (Cache[key] != null)
> > {
> >Imagecached = (Image)Cache[key];
> > cached.Save(Response.OutputStream,ImageFormat.Jpeg );
> > return;
> > }
> > else
> > {
> >Imageimg =Image.FromFile(path);

>
> > //Add to thecache
> >Cache.Add(key, thumb, null, DateTime.Now.AddSeconds(10),
> > TimeSpan.Zero, CacheItemPriority.High,
> > new CacheItemRemovedCallback(this.RemovedCallback));
> > }

>
> > this solution checks if a "CacheKey" exists for a particular ImagePath
> > +Width+Height combo exsits, then it uses the "Saved CachedImage", if
> > none, it will use theimagedirectly from theImage.FromFile(path) and
> > adds a new "CacheKey" for it.

>
> > Solution #2: Static Images
> > ----------------------------------
> > 1. Create the staticimagethat was generated on the fly by the
> > ImageProcess.aspx to a directory in the webserver ex: Root
> > \ThumbnailImages\
> > 2. The staticimagewill be named as ImagePath-ImageName-ImageWidth-
> > ImageHeight-ImageDateCreated.ImageExtension

>
> > Where
> > ImagePath =Image'sFull Path taken from a table.column in a db
> > ImageName =ImageFile Name taken from a table column in a db
> > ImageWidth = Client User specific
> > ImageHeight = Client User spefici
> > ImageDateCreated =ImageUpload DateTime taken from a table.column in
> > a db.

>
> > 3. Subsequent request logic will be:
> > 3.1 First check if the combination filename exists in the Root
> > \ThumbnailImages\ folder
> > 3.2 If it exist, use that staticimage
> > 3.2 If it does not exist, createimageon the fly using
> > ImageProcess.aspx & create a thumbnailimagein the thumbnail Root
> > \ThumbnailImages\ folder

>
> > I am a bit confuse on which to use. I am after - which is faster
> > Lastly, if I use Solution#1: Caching, when the user exists his or her
> > browser, then opens a new browser, will the "CacheKeys" still be
> > there? In other words, are the "CacheKeys" stored in the webserver
> > for use by other concurrent users?

>
> > Thanks,
> > Henry Wu- Hide quoted text -

>
> - Show quoted text -



 
Reply With Quote
 
bruce barker
Guest
Posts: n/a
 
      06-14-2007
iis serves up images faster than asp.net can. iis also supports the head
request which the browser will use with images.

-- bruce (sqlwork.com)

wardemon wrote:
> Hi All,
> I have a aspx page named: ImageProcess.aspx that creates a thumbnail
> version of an image by passing the ImagePath, width, and height. The
> ImagePath is taken from a table from a database, while width and
> height is user specific - meaning user can change this on demand.
>
> My question is, although this rendering images on the fly is cool, I
> would like to implement some sort of mechanism/logic that it wont keep
> rendering the thumbnail again and again.
>
> I thought of two things that might solve my problem, however I am
> unsure of the pros and cons and which is much faster for the client.
>
> Solution #1: Caching
> ----------------------------------
> using System.Web.Caching;
> Cache Cache = context.Cache;
>
> //Get the image
> string path = Server.MapPath(Request.FilePath);
>
> //Check the cache
> string key = path + "-" + strWidth + "-" strHeight;
> if (Cache[key] != null)
> {
> Image cached = (Image) Cache[key];
> cached.Save(Response.OutputStream,ImageFormat.Jpeg );
> return;
> }
> else
> {
> Image img = Image.FromFile(path);
>
> //Add to the cache
> Cache.Add(key, thumb, null, DateTime.Now.AddSeconds(10),
> TimeSpan.Zero, CacheItemPriority.High,
> new CacheItemRemovedCallback(this.RemovedCallback));
> }
>
> this solution checks if a "Cache Key" exists for a particular ImagePath
> +Width+Height combo exsits, then it uses the "Saved Cached Image", if
> none, it will use the image directly from the Image.FromFile(path) and
> adds a new "Cache Key" for it.
>
>
> Solution #2: Static Images
> ----------------------------------
> 1. Create the static image that was generated on the fly by the
> ImageProcess.aspx to a directory in the webserver ex: Root
> \ThumbnailImages\
> 2. The static image will be named as ImagePath-ImageName-ImageWidth-
> ImageHeight-ImageDateCreated.ImageExtension
>
> Where
> ImagePath = Image's Full Path taken from a table.column in a db
> ImageName = Image File Name taken from a table column in a db
> ImageWidth = Client User specific
> ImageHeight = Client User spefici
> ImageDateCreated = Image Upload DateTime taken from a table.column in
> a db.
>
> 3. Subsequent request logic will be:
> 3.1 First check if the combination filename exists in the Root
> \ThumbnailImages\ folder
> 3.2 If it exist, use that static image
> 3.2 If it does not exist, create image on the fly using
> ImageProcess.aspx & create a thumbnail image in the thumbnail Root
> \ThumbnailImages\ folder
>
>
> I am a bit confuse on which to use. I am after - which is faster
> Lastly, if I use Solution#1: Caching, when the user exists his or her
> browser, then opens a new browser, will the "Cache Keys" still be
> there? In other words, are the "Cache Keys" stored in the webserver
> for use by other concurrent users?
>
> Thanks,
> Henry Wu
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET Cache vs Window System Cache Sergey via DotNetMonster.com ASP .Net 0 11-15-2006 03:33 PM
no-cache - need to reload page from server and not the cache John HTML 2 10-29-2006 05:35 PM
client-side cache vs server-side cache vs ajax vs asp.net callback =?Utf-8?B?b25l?= ASP .Net 1 03-08-2006 12:25 PM
Page.Cache vs HttpContext.Current.Cache DesignerX ASP .Net 5 01-20-2004 10:55 PM
Cache::Cache Stale Segments Jeff Nokes Perl 0 09-30-2003 04:34 PM



Advertisments