Thursday, November 4, 2010

Jboss - Performance Improvement

Well.. to start off.. I used Open Symphony --> oscache.jar

1. place it in build path

2. place oscache.properties in src dir so that on building, it directly goes in classes dir
Although I am not sure how to use it. I dint use it

3. Create a class to cache the data. The class should have a init method that runs when jboss starts.

4. cache all data that u want to cache in this method

5. sample code here:

make a class object--

GeneralCacheAdministrator admin=null;
...
...
..
in the method write:

init(){
..
admin.putInCache(cacheName,object);
..
}

Now create a get method to retrieve or access this mem cached data where ever u want

get(String myKey){
..
int myRefreshPeriod=180;
try {
// Get from the cache
String key=myKey;

Object data=(Object)admin.getFromCache(key,myRefreshPeriod);
System.out.println("=============== from cache 1================");
} // end try
catch(NeedsRefreshException nre) {
try {
// Get the value (probably by calling an EJB)
data = xyzMethodGetFromDataBase();
// Store in the cache
addToCache(myKey,dashboardData);
System.out.println("=============== direct db ================");
} // end try
catch(Exception exp) {
// We have the current content if we want fail-over.
data=(Object)nre.getCacheContent();
// It is essential that cancelUpdate is called if the
// cached content is not rebuilt
admin.cancelUpdate(myKey);
System.out.println("=============== old cache ================");
} // end catch
} // end catch


the refresh period is the amount of time (in seconds) after which the data would expire. So when ever next time call goes to memory cache get() method, the time is checked. If the request time has exceeded the refreshperiod time ( in our case 3 mins) then the data is considered to be stale and a NeedsRefreshException is thrown. This Exception is catch(ed) in 1st catch block in our code. Here we make a fresh call to db and save the new entry in cachekey.
Note: this exception is also thrown if the kwy was not found. So in our case, a new entry is made , freshly baked from DB, and is stored in cache.. in case we might need it in future ( and we will coz its all about concurrent access made easy thing we are working upon here)

Thats it. Job DOne!