How to add Auxiliary cache programitically in JCS

In JCS we can enable cache overflow/persistence to disk by using indexed disk cache. We can easily configure this in cache.ccf file like this :

jcs.auxiliary.DC=org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactory
jcs.auxiliary.DC.attributes=org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes
jcs.auxiliary.DC.attributes.DiskPath=c:/downloads/cache
jcs.auxiliary.DC.attributes.MaxPurgatorySize=10000
jcs.auxiliary.DC.attributes.MaxKeySize=10000

What if you want to set this auxiliary cache from Java program or perhaps update the diskstore to some new value. Though there is no proper documentation, but I was able to figure out how to do that.

 

Here cache is CompositeCache object, diskStore is the new diskpath for indexed cache and map contains other parameters which we could have passed in ccf file also

 private void setAuxiliaryCache(CompositeCache<Object, Object> cache, String diskStrore, Map<String, String> map)
    {
        AuxiliaryCache<Object, Object>[] auxs = cache.getAuxCaches();
        IndexedDiskCacheAttributes idca = new IndexedDiskCacheAttributes();
        idca.setCacheName(cache.getCacheName());
        idca.setDiskPath(diskStrore);
        if(null != map && null != map.get(PROP_MAXELEMENTSONDISK)){
            idca.setMaxPurgatorySize(Integer.valueOf(map.get(PROP_MAXELEMENTSONDISK))); 
        }
        if(null != map && null != map.get(PROP_MAXKEYSIZE)){
            idca.setMaxKeySize(Integer.valueOf(map.get(PROP_MAXKEYSIZE))); 
        }
        AuxiliaryCache<Object, Object> aux = new IndexedDiskCache<Object, Object>(idca);
        List<AuxiliaryCache<Object, Object>> auxList = Arrays.asList(aux);
        for (AuxiliaryCache<Object, Object> auxCache : auxs)
        {
            if (!(auxCache instanceof IndexedDiskCache))
            {
                /*
                 * ((IndexedDiskCacheAttributes) ((IndexedDiskCache) auxCache).getAuxiliaryCacheAttributes())
                 * .setDiskPath(diskStrore);
                 */
                auxList.add(auxCache);
            }
        }
        cache.setAuxCaches((AuxiliaryCache<Object, Object>[]) auxList.toArray());
 
    }

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *