Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Howto use global a map in a web application?  RSS feed

 
Dennis Zandvliet
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to create a simple cache, for holding lots of user data objects.

From time to time all data is being discarded and again being populated by a single update thread.
Also single entries can be update, modified, inserted by (LDAP) notification events.

The map is being read by many threads.

Some threads only look up single entries while others loop through the whole map.

I've read many articles about this, but you've to take so many thinks into account, that I'm kind of lost.

Also should you store this map into the context, which can be awkward because you have to pass the context around, or use static methods to access the map?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd advise to look into one of the available cache libraries (see http://faq.javaranch.com/java/CachingStrategies for which ones are still actively developed). If the library in question doesn't handle concurrency issues, I'd look into using a java.util.concurrent.locks.ReadWriteLock to guard the operations that alter the cache while it might be read by another thread.
 
Dennis Zandvliet
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:I'd advise to look into one of the available cache libraries (see http://faq.javaranch.com/java/CachingStrategies for which ones are still actively developed). If the library in question doesn't handle concurrency issues, I'd look into using a java.util.concurrent.locks.ReadWriteLock to guard the operations that alter the cache while it might be read by another thread.


Thanks, but I don't want to use a cache library (yet), because I want to get my own hands dirty.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, what do you have so far, and where are you stuck making progress?
 
Dennis Zandvliet
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An existing application in which i want to add some LDAP caching. First I only wanted to load all the ldap entries periodically in a separate thread for read only access for periodically generating some javascript. But than i thought he if all the data is cached why not use it for other part of the program which need this. And than I thought well instead of periodically do a complete refresh, why not use the JNDI LDAP notification system to keep the cache up to date, but still periodically, may be once per day do a complete refresh of the cache to be sure that we due to unknown errors some records hasn't been synchronized properly.

For now i have off course the application itself :-), and a simple cache which consist of a class which holds a reference to a map which is periodically being replaced by a new instance of a map which has been populated by the same task. Why ever time creating a new map and replacing the old one?
Because at the moment this seems the easiest way for me to avoid the dreaded race conditions without locking the whole current map in use because updating from ldap takes a long time about 15 seconds.

I guess this can be done better without using every time a new map. If this works fine I also want to add, updating individual records/objects by adding the ldap notification support, which off course adds more complexity.

I'm not afraid that their will be memory leakage due to the fact that their are still references to the old map, because all references exist only for the duration of a request.

So writes should only come from the ldap notification event handlers and from the task which updates the whole map. And reads from the rest of the application. Reads can come from individual look up of entries are from looping through all map entries.

I all ready know that you have all kind of map implementations which have been optimized for concurrent use, but I also know that you have to be very careful and know exactly what you are doing, otherwise things can go terribly wrong.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!