views:

431

answers:

3

Im building a image gallery which reads file from disk, create thumbnails on the fly and present them to the user. This works good, except the processing takes a bit time.

I then decided to cache the processed images using the ASP .NET Application Cache. When a image is processed I add the byte[] stream to the cache. As far as I know this is beeing saved into the system memory. And this is working perfect, the loading of the page is much faster.

My question is if there are thousands of images which gets cached in the Application Cache, will that affect the server performance in any way?

Are there other, better ways to do this image caching?

+4  A: 

By default Application Cache stores data on server memory; depending on your website navigation pattern, maybe you don't get too many cache hits.

You could to preprocess all images to generate its thumbnails at once and store it with your original image. This way you don't need to deal with that cache layer and, probably, won't take too much more disk space.

Rubens Farias
I think this should have been a comment rather than an actual answer.
Tobias
Definitely, create the thumbnails as the images are loaded. This is much less intensive than constantly creating thumbnails, and won't use a massive amount of space if they are optimised properly.
ck
+1 to create thumbnails. Disk space is relatively cheap, thumbnails, by their nature, are small. @Tobias - why? It is a good answer!
AdaTheDev
Thanks people, only problem is that I cant mix the thumbs with the real images since the administrator is using the gallery folder as administrator tool, meaning he add/delete files directly in the gallery folder. It would be confusing to have original image and thumb in the same folder, and also both images would be shown on the website since it is in the same folder. But I guess I can add the thumbs to their own paralell folder-structure.
Martin
Yes, create a now browsable folder to store your thumbnails and, once a new image get uploaded, create its thumbnail in correct folder
Rubens Farias
Or make your folder gallery to ignore images with some pattern, like filename ending with "-thumb", for instance
Rubens Farias
What if the original image is deleted from the folder, there is no great way to know this? With the cache all the images gets deleted once in a while, but I cant detect a deleted image in any way. Guess I have to set up a weekly wipe of the thumbnail folder then?
Martin
lets say your image is "abc.jpg", you can name your thumb as "abc-thumb.jpg", so can be easy to check if you thumb refer to an existing image.
Rubens Farias
+1  A: 

It depends how many images you have, and memory on your server. My preference would be to write the thumbnails to disk and not put them an in-memory cache. You will presumably be running the site from a raid-enabled disk so read speeds will be quick, alongside IIS being optimised for disk reads.

Chris S
A: 

Yes, you can definitely impact web server performance by putting too much stuff in cache.

One reason Cache was added to ASP.Net was to add its functionality of clearing the oldest/least-used items from the cache if server memory starts getting maxed out. If you cache the items in the Application object, there will be no clean-up, and the server can ultimately run out of memory. Using the Cache object will help you avoid that.

The Cache object also allows you to set various rules about expiration. So, for example, you might put a time limit on how long those images are retained in cache.

DOK