Discussions

General J2EE: Caching obejcts using hashtable

  1. Caching obejcts using hashtable (8 messages)

    Hi, I have a static Hashtable object that is created in my servlet web application to store user related data. I use the user's session id as the key. Can I use a HashMap instead of Hashtable? Do I have to think about synchronization in such a scenario? Thanks /R
  2. Re: Caching obejcts using hashtable[ Go to top ]

    You could use hashmap, but you might want to consider using cookies instead; they are more generally used for this sort of thing. While your solution works, consider its impact on memory requirements under heavy load
  3. Re: Caching obejcts using hashtable[ Go to top ]

    You could use hashmap, but you might want to consider using cookies instead; they are more generally used for this sort of thing.
    While your solution works, consider its impact on memory requirements under heavy load
    I can't use cookies becuase the user related data which needs to be stored are actually (quite large) rowsets fetched from the database. Storing them as cookies would not be practical. /R
  4. What's wrong with the HttpSession?
  5. What's wrong with the HttpSession?
    Yes ofcourse this will be the best place to store the data. But I wanted to minimize the amount of data stored in the session (to facilitate faster session replciation), as a result I only keep the most recently used data in the session and the rest of the user data out of the session. FYI: I also have an HttpSessionListner which removes the data from the hashtable when sessionDestroyed() is fired using the session id as the key /R
  6. What kind of cache service you are looking for? 1. Distributed / Local 2. Huge Objects / Simple objects 3. Replications 4. Auto / Manual Refresh 5. Syncronization 6. LRU Algo If you want 3 out of 6 things, go for open source cache management soultion. This is much eaiser than builiding from zero.
  7. Assuming what you are building is associated with a project and that that project has a budget. . . There are a few well established commercial products that could assist you here. As I work for GigaSpaces, I will naturally suggest first that possibility and I can offer you assurance that your project will be enhanced by the addition of the technology GigaSpaces offers. As far as I can derive from your post we have already provided such solutions to some of our customers. For Example: H3G uses our stuff to do very much what it is you describe. It may be that you can benefit from our experience and that we can provide you with a valuable addition to your toolbox. Cheers, Owen Taylor Senior Director, Worldwide Technical Communications GigaSpaces http://jroller.com/page/owentaylor http://www.gigaspaces.com/
  8. Re: Caching obejcts using hashtable[ Go to top ]

    Hi R, I think hashtable is the best way of using cache as it takes care of your synchronization issues if you are using the same via multiple threads. If you use a hashmap - you have to be careful of two things: 1) Your updates need to be atomic - like replacing reference of a single object. 2) your application should be tolerant to having stale data for one transaction. I had recently put together my thoughts on the same here Do drop me a note for furnther details. Pranshu
  9. Re: Caching obejcts using hashtable[ Go to top ]

    I have a static Hashtable object that is created in my servlet web application to store user related data. I use the user's session id as the key. Can I use a HashMap instead of Hashtable? Do I have to think about synchronization in such a scenario?
    First, ask yourself what you are trying to accomplish. Second, focus on the important things. For example, using a HashMap instead of a Hashtable gains you almost nothing, and in a multi-threaded environment it could potentially lose a lot (e.g. your data). Third, keep it simple. If you are using the session ID as the key, then maybe you should just put the data (or the Hashtable) into the session. Peace, Cameron Purdy Tangosol Coherence: The Java Data Grid