Web - Images From A Database

  • Thread starter Thread starter David P. Donahue
  • Start date Start date
D

David P. Donahue

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
(e-mail address removed)
http://www.cyber0ne.com
 
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.
 
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
(e-mail address removed)
http://www.cyber0ne.com
 
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.
 
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
(e-mail address removed)
http://www.cyber0ne.com
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top