Firstly, I persist the portraits (images) of all users in the db as Strings (large one, say length 5000). Then, I want to get the String and display the portraits of users on the web page(like a topic in a forum). Here I know three solutions but none of them is efficient:
1. Because the image is large, it's not efficient to store it in objects(beans).
2. Because I have many users(say 300), if I store all the maps(userId and image) in the session, the session will be huge.
3. Because I have many users(say 300), if I access the db for each user's image, the worse case is I will have to access db for 300 times.
Is there any solution (such as a design pattern or interceptor) for this performance issue? Many thanks!
I usually place images in the file system. If you don't have a lot of changes in your population, you can probably get away with building when you add/delete them, use ordinary HTML IMG tags and include the images in your WAR.
If you have a more dynamic environment, store them outside the WAR and write a quick-and-dirty Servlet to take a parameter like the user ID, look up or construct the image name and return the image data.
5k * 300 == 1500k, call it 2M for the sake of argument. That's not that much memory, although I tend to keep them on the filesystem too-they'll be cached by the browser.
will zhang
Ranch Hand
Joined: Sep 11, 2008
Posts: 46
posted
0
Thanks, so if I want to persist the images in the db instead of local files, method 2 is the best and durable solution?
will zhang
Ranch Hand
Joined: Sep 11, 2008
Posts: 46
posted
0
Joe Ess wrote:I usually place images in the file system. If you don't have a lot of changes in your population, you can probably get away with building when you add/delete them, use ordinary HTML IMG tags and include the images in your WAR.
If you have a more dynamic environment, store them outside the WAR and write a quick-and-dirty Servlet to take a parameter like the user ID, look up or construct the image name and return the image data.
Because the user can change their portrait, if I want to store their portraits in the file system, I can only store them outside WAR? I'm not familiar with quick-and-dirty Servlet. Is there any examples?
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Performance issue with large data(such as images)