471,350 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Web - Images From A Database

Currently, when I want to display an image from a database in my
website, I reference another page with a query string argument of that
image's unique ID in the table. Then that other page grabs the image
and the content type from the database and writes it out to the client.

However, this model seems to be a bit heavy on database overhead when
dealing with a page with many such images. For example, a page which
lists all users on a website and displays the avatar for each user.

I've been trying to come up with a different way to display the images,
but haven't been able to think of anything. The only idea I had was to
grab the content type and image data when populating that datalist of
users and pass them as arguments somehow to that image-outputting page
so it doesn't have to hit the database. If this sounds reasonable, what
would be a good way to do it? I would think the query string would be
unable to handle arguments like that.

Any other ideas on what to do here? Anybody have this problem before
and come up with some other way to streamline the data access?
Regards,
David P. Donahue
dd******@ccs.neu.edu
http://www.cyber0ne.com
Dec 8 '05 #1
5 1189
David,

This sounds like a perfect use for the System.Web.Caching namespace.

Basically, I would store items in the Cache exposed through the Page
that you are running. You can load your Image object instances into it that
represent the images to draw. Then, on your page, you check the cache. If
the item in the cache is there (keyed on whatever input parameters you
need), then you use that image. Otherwise, you can fetch it from the
database.

To make it even faster, I would store the images on disk, instead of the
database. I would name them according to a scheme you come up with
yourself, and store them all in one directory. This will reduce your
overhead by a bit, I believe, since you are not doing all of these database
hits.

You could even tie the item in the cache to a dependency in the file.
Basically, if the file changes, the item in the cache is invalidated, which
requires you to then get the image again and place it in the cache.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:ug**************@TK2MSFTNGP11.phx.gbl...
Currently, when I want to display an image from a database in my website,
I reference another page with a query string argument of that image's
unique ID in the table. Then that other page grabs the image and the
content type from the database and writes it out to the client.

However, this model seems to be a bit heavy on database overhead when
dealing with a page with many such images. For example, a page which
lists all users on a website and displays the avatar for each user.

I've been trying to come up with a different way to display the images,
but haven't been able to think of anything. The only idea I had was to
grab the content type and image data when populating that datalist of
users and pass them as arguments somehow to that image-outputting page so
it doesn't have to hit the database. If this sounds reasonable, what
would be a good way to do it? I would think the query string would be
unable to handle arguments like that.

Any other ideas on what to do here? Anybody have this problem before and
come up with some other way to streamline the data access?
Regards,
David P. Donahue
dd******@ccs.neu.edu
http://www.cyber0ne.com

Dec 8 '05 #2
> Basically, I would store items in the Cache exposed through the Page
that you are running. You can load your Image object instances into it that
represent the images to draw. Then, on your page, you check the cache. If
the item in the cache is there (keyed on whatever input parameters you
need), then you use that image. Otherwise, you can fetch it from the
database.
I'll have to look into that one, I've never used the .NET Cache before.
Basically, am I right in thinking that, at application startup, I
would fetch the images from the database and store them in active memory
for various user sessions to access?
To make it even faster, I would store the images on disk, instead of the
database. I would name them according to a scheme you come up with
yourself, and store them all in one directory. This will reduce your
overhead by a bit, I believe, since you are not doing all of these database
hits.


I've thought about storing them on the filesystem, but I'd prefer not to
open any kind of write access to the filesystem itself. Not so much for
the security problems that I can think of, but for the ones I can't
think of.

Regards,
David P. Donahue
dd******@ccs.neu.edu
http://www.cyber0ne.com
Dec 8 '05 #3
David,

See inline:
I'll have to look into that one, I've never used the .NET Cache before.
Basically, am I right in thinking that, at application startup, I would
fetch the images from the database and store them in active memory for
various user sessions to access?
Yes, that's the basic idea.
I've thought about storing them on the filesystem, but I'd prefer not to
open any kind of write access to the filesystem itself. Not so much for
the security problems that I can think of, but for the ones I can't think
of.
If that is the case, then this is fine. If you don't have control over
the box, it's another issue as well. If you do, I think that you should
consider it.

However, see how your app performs using the cache. Also, there is a
way to indicate that you want the item in the cache to be dependent on a
column in a table in sql server. If the cache receives notification that
the column has changed, then it will invalidate the cache. This can be
pretty helpful for what you are doing.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

Regards,
David P. Donahue
dd******@ccs.neu.edu
http://www.cyber0ne.com

Dec 8 '05 #4
> If that is the case, then this is fine. If you don't have control over
the box, it's another issue as well. If you do, I think that you should
consider it.
If caching turns out to be more complicated than I want, or has some
other drawback that doesn't fit my needs, then I may end up implementing
some filesystem access. Since it may be the only option left.
However, see how your app performs using the cache. Also, there is a
way to indicate that you want the item in the cache to be dependent on a
column in a table in sql server. If the cache receives notification that
the column has changed, then it will invalidate the cache. This can be
pretty helpful for what you are doing.


A lot of these images rarely, if ever, change. One thing I won't want,
though, is to have a cache outlive a change in any way. I doubt users
would appreciate it if they update something and can't see their changes
right away.

I'm finding a number of articles and tutorials on the subject. Do you
recommend any? I don't need any extra bells and whistles, just caching
images from the database on the application level and keeping the cache
updated when the table(s) get updated.

Regards,
David P. Donahue
dd******@ccs.neu.edu
http://www.cyber0ne.com
Dec 8 '05 #5
David,

Check out the documentation for the SqlCacheDependency class, as well as
the Cache class. Those two areas in the documentation should give you a
good starting point for what you are trying to do.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:OQ**************@tk2msftngp13.phx.gbl...
If that is the case, then this is fine. If you don't have control
over the box, it's another issue as well. If you do, I think that you
should consider it.


If caching turns out to be more complicated than I want, or has some other
drawback that doesn't fit my needs, then I may end up implementing some
filesystem access. Since it may be the only option left.
However, see how your app performs using the cache. Also, there is a
way to indicate that you want the item in the cache to be dependent on a
column in a table in sql server. If the cache receives notification that
the column has changed, then it will invalidate the cache. This can be
pretty helpful for what you are doing.


A lot of these images rarely, if ever, change. One thing I won't want,
though, is to have a cache outlive a change in any way. I doubt users
would appreciate it if they update something and can't see their changes
right away.

I'm finding a number of articles and tutorials on the subject. Do you
recommend any? I don't need any extra bells and whistles, just caching
images from the database on the application level and keeping the cache
updated when the table(s) get updated.

Regards,
David P. Donahue
dd******@ccs.neu.edu
http://www.cyber0ne.com

Dec 8 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by yawnmoth | last post: by
3 posts views Thread by Srdjan Pejic | last post: by
6 posts views Thread by bissatch | last post: by
10 posts views Thread by Neo Geshel | last post: by
9 posts views Thread by Wayne Smith | last post: by
10 posts views Thread by eholz1 | last post: by
1 post views Thread by ttamilvanan81 | last post: by
7 posts views Thread by Keith Hughitt | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.