2008年1月9日水曜日

[Java>WhirlyCache]Object Cache Mechanism(Whirlycache)

EHCache or OSCache is a very famous libraries for java object cache, but Whirlycache is a simple and fast cache mechanism stored object on local JVM memory. Whirlycache doesn't have a cache replication mechanism or other complex mechanism, but this cache is very simple, useful and easy to use, and lightweight library.
Whirlycache maintenances the objects stored into the Cache instance by using the Tuner Thread. Tuner Thread is started up if the Cache instance is created. and Tuner thread loops and maintenances the stored objects.For example, if the object stored into the cache is expired, Tuner thread removes the expired object from cache instance.(Normally cache logic maintenances the stored objects when System triggers putting the object into the cache instance.)

References
  • Whirlychache home
    https://whirlycache.dev.java.net/
  • About whirlycache
    http://www.theserverside.com/news/thread.tss?thread_id=29290
Install and Setup
  1. download whirlycache library from https://whirlycache.dev.java.net/
    all of files and dependency libraries are included into the whirlycache-1.0.1.zip.
  2. unzip whirlycache-1.0.1.zip
  3. get jar files
    get 4 files(commons-collections-3.1.jar, commons-logging-1.1.jar, concurrent-1.3.4.jar, whirlycache-1.0.1.jar) at least. These files are included into the lib directory.
  4. create xml file for cache setup, cache size, cache policy class and tuner's sleeptime.
    sample xml file is https://whirlycache.dev.java.net/sample-whirlycache-xml-file.html
    default xml file including the default parameter values are included into the whirlycache's jar file(whirlycache-default.xml)
    If you need to customize the value of parameters, create a new xml file and put it into the classpath directory.
Import classes
import com.whirlycott.cache.CacheManager;
import com.whirlycott.cache.Cache;
import com.whirlycott.cache.CacheException;
Sample Program
public class CacheTest{
//define whirlycache Cache object
static Cache whirlycache=null;
//initialize whirlycache's cache instance
static{
try{
whirlycache=CacheManager.getInstance().getCache();
}catch(CacheException eCache){
//if system fails to initialize the cache instance, set null
whirlycache=null;
}
}

//check logic whether the Cache instance is initialized properly or not and
//cache key is eligible or not.
private boolean checkCache(String pKey){
if(whirlycache==null){
return false;
}
if(pKey==null || pKey.equals("")){
return false;
}
return true;
}

//get object from cache instance based on specified argument value.
private Object getObjectFromCache(String pKey){
if(checkCache(pKey)==false){
return null;
}
return whirlycache.retrieve(pKey);
}

//store object into the cache object with expire long value
private void storeObjectIntoCache(String pKey,Object pObject,long pExpireDateLong){
if(checkCache(pKey)==false){
return;
}
if(pExpireDateLong < 0){
//cache object with no expire date
whirlycache.store(pKey,pObject);
}
else{
//cache object with expire date, this object will cleanup automatically after expire date
whirlycache.store(pKey,pObject,pExpireDateLong);
}
}

//remove cached object from cache instance.
private void removeObjectFromCache(String pKey){
if(checkCache(pKey)==false){
return;
}
whirlycache.remove(pKey);
}

//check whether the object related to the key is existed into the cache instance or not.
private boolean containObjectInCache(String pKey){
if(checkCache(pKey)==false){
return false;
}
Object cachedObject=getObjectFromCache(pKey);
if(cachedObject==null){
return false;
}
return true;
}

//program main method
public static void main(String[] args){
//test cache key
String cacheKey="cache_test_date";
//get object from cache instance
Date cacheDate=getObjectFromCache(cacheKey);
if(cacheDate==null){
//if misscache, set new value to variable
cacheDate=new Date();
storeObjectIntoCache(cacheKey,cacheDate);
System.out.println("initialize new date object and put it into cache, date="+cacheDate);
}
else{
//could find and get value from cache instance
System.out.println("get date object from cache, date="+cacheDate);
}
}
}

0 件のコメント: