persistent disk storage in ehcache

Requirement – To have persistent storage in ehcahce

Solution-
cacheManagerPeerProviderFactory –It is used for saying “Hello, I’m here!” and allows to discover the other CacheManager in the cluster and be discovered by other nodes.

It accepts only two arguments (class and properties).

cacheEventListenerFactory — It is used for receiving notification about cache update by other nodes in cache cluster.

bootstrapCacheLoaderFactory —It is used for starting cache system and synchronize the cached elements in the cluster.

package com.techartifact.caching;

import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

// this is main class.
public class MainClass {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MainClass mainClass = new MainClass();
		mainClass.runMainClass();
	}

	private void runMainClass() {
		// TODO Auto-generated method stub
		int TOTAL=3;
	         CacheManager cacheManager = new CacheManager();
		Cache myCache = cacheManager.getCache("persistentEhcacheNew");

		System.out.println("the size of my cache at startup is: "+myCache.getSize());
		System.out.println("disk store size: "+myCache.getDiskStoreSize());
		System.out.println("memory store size: "+myCache.getMemoryStoreSize());
	        System.out.println("memory DiskStoreSize size: "+myCache.getDiskStoreSize());
	        System.out.println("boot strapCacheLoader is Asynchronous: "+myCache.getBootstrapCacheLoader().isAsynchronous());
                System.out.println("cache status: "+myCache.getStatus());
	        System.out.println("cache name: "+myCache.getName());
                List listOfKeys= myCache.getKeys();
                //myCache.removeAll();
                if(myCache.getDiskStoreSize()>0){
               for (Object temp : listOfKeys) {
                        System.out.println((String)temp.toString());
	                Element element = myCache.get(temp);  
	                     
	                      if (element != null) {  
	                       System.out.println(temp + " is in the cache!!!"); 
	                       String val = (String)element.getValue().toString();
                               System.out.println(val + " value is in the cache!!!");
                              }
	            }
                }else{
              
		System.out.println("now add 3 elements to cache");
                    for (int i = 0; i < TOTAL; i++) {
                            DataObject dataObject = new DataObject();
                           dataObject.setRandomFloat(Randomizer.returnRandomFloat());
                            dataObject.setRandomString(Randomizer.returnRandomString());			
                            Element element = new Element(i,  dataObject.getRandomFloat());
                            myCache.put(element);
                   }
                }

		cacheManager.shutdown();
		
	}

}


BootstrapCacheLoaderFactory – An abstract factory for creating BootstrapCacheLoader instances. Implementers should provide their own concrete factory extending this factory. It can then be configured in ehcache.xml.

Create a MyBootstrapCacheLoaderFactory that extends BootStrapCacheLoaderFactory, and override method load(EhCache ehcacheparam)to bring the cache up on server startup

package com.techartifact.caching;

import java.util.List;
import java.util.Properties;

import net.sf.ehcache.CacheException;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.bootstrap.BootstrapCacheLoader;
import net.sf.ehcache.bootstrap.BootstrapCacheLoaderFactory;

// 
public class MyBootstrapCacheLoaderFactory extends BootstrapCacheLoaderFactory implements BootstrapCacheLoader{
	

	@Override
	public BootstrapCacheLoader createBootstrapCacheLoader(Properties properties) {
		// TODO Auto-generated method stub
		return new MyBootstrapCacheLoaderFactory();
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		return super.clone();
	}
	public boolean isAsynchronous() {
		// TODO Auto-generated method stub
		return false;
	}
	public void load(Ehcache myCache) throws CacheException {
		// TODO Auto-generated method stub
		System.out.println("load your cache with whatever you want....");
		List keys = myCache.getKeys();
		for (int i = 0; i < keys.size(); i++) {
			Element element = myCache.get((keys.get(i).toString()));
		}
		System.out.println("load complete!");
	}



}