473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Cache Images vs Static Images

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

Jun 14 '07 #1
3 3998

--
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" <he***********@gmail.comwrote in message
news:11**********************@e9g2000prf.googlegro 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

Jun 14 '07 #2
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" <unclechut...@nothinks.comwrote:
--
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" <henrycorte...@gmail.comwrote in message

news:11**********************@e9g2000prf.googlegro 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 -

Jun 14 '07 #3
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
Jun 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Bill H | last post by:
I have a routine that displays 60 items (with thumbnails) per page. You can click any item and displays a new page with item details. When the user back pages it runs the query again to display all...
11
by: Penelope Baker | last post by:
Greetings: I cannot seem to get IE 6.0 to pre-cache my rollover images. Every time the user rolls over a link, it rereads it from the server, even though it is well into the cache by that time....
4
by: dave | last post by:
hi I'm building a page that displays all phone in phoneview.asp...however from admin section we can add new phone image tht stores path in database. I wanna use cache object in phoneview.asp..becoz...
1
by: Glenn | last post by:
Hi, I have a config XML file that I am using from the application cache. I have configured the entry with a remove callback to re-populate cache automatically when the XML file changes. All is...
2
by: Peter Row | last post by:
Hi, I have a site were the pages are dynamically generated on demand. These pages like any static html page may contain image tags and references to CSS files. All the images and CSS files...
6
by: Stephane | last post by:
Hi, All images on my ASP.Net web site are reloaded almost each time I visit a page. My chache setting on IE is set to Automatically. When I visit another web site, images are rarely reloaded so...
2
by: thehuby | last post by:
I am building a CMS and as part of it a user can upload an image. Once uploaded I am displaying the image. If the user then decides they want to replace the image with another I get a caching...
2
by: raji20 | last post by:
I'm frequently updating images on my site when the users clicks on the submit button, and some users have reported they are seeing old images where new ones should be... The image that is getting...
1
by: | last post by:
I've built an application that scrapes JPG images of webpages and PDF instances of those webpages automatically from an RSS feed. References to the scraped resources are persisted to our database....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.