Unable to get the stored Cache

View: New views
1 Messages — Rating Filter:   Alert me  

Unable to get the stored Cache

by Jawahar Nayak :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

 I am using the for caching. I am able to put and get the items upto 3 lac. If I try the application with more than 3 lac, I am not able to get some of items back. It returns null.

I am posting the code :

package ehcache;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;

import net.sf.ehcache.Element;
import net.sf.ehcache.hibernate.EhCache;



/**
 *
 * @author Tejkiran
 */
public class EHCacheTest {
    Cache cache=null;
 
    String Key ="thisiskey";
     CacheManager manager=null;
     Cache memoryOnlyCache=null;
        public InputStream getConfigStream() {
        InputStream fis = null;
        try {
            fis = new FileInputStream(new File("conf/ehcache.xml").getAbsolutePath());
           
           // return EHCacheTest.class.getResourceAsStream("/ehcache.xml");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(EHCacheTest.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(EHCacheTest.class.getName()).log(Level.SEVERE, null, ex);
            }
            return fis;
        }
        }
        public void writeToEHCache(long nList) {
            try{
            long nStart = (Long) new Date().getTime();
            manager = CacheManager.create("E:\\workarea\\Chambal\\Projects\\EHCache\\conf\\ehcache.xml");
            manager = CacheManager.create();
            memoryOnlyCache = new Cache("testCache", 500000, true, true, 5, 2);
            manager.addCache(memoryOnlyCache);
            cache = manager.getCache("testCache");
            for (long i = 0; i < nList; i++) {
               
                Element key = new Element(Key+i, "d"+i);
                cache.put(key);
           
            }
           System.err.println("Memory : "+ cache.getMaxElementsInMemory() + " Disk : "+ cache.getMemoryStoreSize());
            long nEnd = (Long) new Date().getTime();
            System.out.println("Total consumed EHCache Time : " + (nEnd - nStart));
           
            }catch(RuntimeException ex){
               
                System.out.println("Exception :" +ex.toString()+" Message"+ ex.getMessage());
            }
        }
        public void readFromEHCache(long nlist){
            long i=0;
            long nStart=0, nEnd=0;
             try{
           nStart = (Long) new Date().getTime();
           
           //Cache cache1 = manager.getCache("testCache");
           //EhCache cache1 = cache;
           System.err.println("Memory : "+ cache.getMaxElementsInMemory());
           //System.out.print(cache1.getKeys().size());
            for ( i = 0; i < nlist; i++) {
                String key = Key+(i);
                Element hello = cache.get(key);
                //String sValue = (String) cache1.read(key);
                System.err.println(hello.getObjectValue());
                //System.err.println(sValue);
               
            }
          //  manager.shutdown();
             nEnd = (Long) new Date().getTime();
            System.out.println("Total Get consumed EHCache Time : " + (nEnd - nStart));
           
            }catch(RuntimeException ex){
                System.out.println(":- "+i);
                System.out.println("Exception :" +ex.toString()+" message :" + ex.getMessage());
                nEnd = (Long) new Date().getTime();
                System.out.println("Total Get consumed EHCache Time : " + (nEnd - nStart));
            }
        }
        public static void main(String[] arg){
            EHCacheTest objEHCacheTest = new EHCacheTest();
            long nmax=2000;
            objEHCacheTest.writeToEHCache(nmax);
            objEHCacheTest.readFromEHCache(nmax);
        }
    }

 I am using the default configuration.