This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Caching Design Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Caching Design" Watch "Caching Design" New topic

Caching Design

mohamed zafer
Ranch Hand

Joined: Nov 03, 2000
Posts: 62
Hello Guys,
I am currently involved in developing a caching component, that will have the following features,
1. During startup load all the config data(from XML files) into memory.
2. There are a few objects which are used by many components, hence I need to cache them... The caching component should hold these components.
3. The cached data should be easily accesible, for example if I want to get address of a resource, I should be able get it as simple as
4. When the system is up and running, there is a possibility that a part of the config data(xml files) are modified. These should be reflected in the cache, without involving any restart of the system.
5. It should be independent of any container or appserver
I need inputs on how to develop the above component.
Mohamed Zafer
Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
My 2c ok.
you can use a kind of javabeans(1 for each xml file) that are populated by the data in the xml file during startup, than you store the java beans in a HashTable using for example the names of the xml files, when you need to get some data you can consult the object on the HashTable in memory avoiding parsing the xml again, this solves topics 1,2 and 3 from your post as HashTables are very easy to consult and maintain.
Now the solution to item 4 is to build a thread that checks the xml files with some interval to see if they are changed and in that case this thread should in some way notify the component who holds the hashtable to remove the data relative to the xml and parse it again puting it back on the hashtable.
About the item 5, well I think this is more a fact about you having the correct parsers registered.
hope this helps.
mohamed zafer
Ranch Hand

Joined: Nov 03, 2000
Posts: 62
Thanks Marcos,
To get started, your solution is simple and easy. I want to include more features like..
1. The component should be like a service, so that I can plug to any server and use the service..
2. Depending upon the purpose and usage the cached data can be grouped, they may follow a hierarchy. I want this hierarchy/grouping to be preserved. Also the user should be able to access the data with ease irrespective of the grouping etc..
3. The caching should be generic and configurable as far as possible.. like maintaining a pool of objects or a singleton etc..
4. The caching service should be available across cluster's.. As it may hold handle's to critical resources. failover condition should also be taken into consideration.
5. I should be able to add security to the I don't want everyone to get a handle to some critical objects form the cache..
Please add your ideas on the above issues..
Mohamed Zafer
Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
Hi, ok my opinion:
1 - This one I would have to do a research to answer but I think I would start taking a better look at xml services and JMX, you may find the solution there.
2 and 3 - this remembers me of using a builder pattern with an Abstract Factory to return a director from some group of the builder, this should solve the problem, take a look on design patterns catalog for that.
4 - This one must be a bit harder to implement but is the one I most like(I love distributed computing)
well, to be available across clusters I believe the better solution is implement some kind of peer to peer comunication using sockets or rmi(maybe implementing agents) to replicate the service in the cluster, now to implement failover you can use multicast comunication that broadcast the messages to check the available servers and this can be done using a thread that sends a "heartbeat" with some interval and some component of your application should listen to that, than you can implement a time out to determine that some server is not active anymore, you should also consider monitoring the ip sockets I�ve mentioned.
5. Well this is a subject that I also have to research better but I think you should take a look at JAAS to acomplish that.
hope this helps a bit.
Jim Baiter
Ranch Hand

Joined: Jan 05, 2001
Posts: 532
There is a new product that does some pretty sophisticated caching in a J2EE environment, see Coherence
mohamed zafer
Ranch Hand

Joined: Nov 03, 2000
Posts: 62
Hello Marcos,
If u r still following the thread, I need your suggestion. Currently I am working on caching of the config files(xml). Consider that my config file is of the following structure
I want to load this config into the cache. Once done I should be able to acces the leaf nodes/values with ease, It should NOT be something like this
ConfigCache cc = CacheFactory.getConfigCache();
String logPath = cc.get("logging").get("channel").get("flatfile").get("path");
I WANT it to be something like this
ConfigCache cc = CacheFactory.getConfigCache();
String logPath = cc.get("loggingPath");
Though I use minimal method's to access the "path", internally in the cache, the hierarchy of the xml structure should be maintained.
How can this be done, in other words I want to map the XML structure with a suitable Key-Pair values (or some other structure). One option is to concatenate all the node names and use it as a key to retrieve the value, i.e,
String logPath = cc.get("loggingChannelflatfilePath");
But I feel that there can be better alternative to this.
Mohamed Zafer
I agree. Here's the link:
subject: Caching Design
It's not a secret anymore!