在EhCache中获取存储的对象

时间:2014-07-10 02:05:41

标签: java eclipse ehcache

请帮我理解并从EhCache中获取存储的对象。 我正在使用Eclipse和Tomcat Web Server。 我正在使用ehcache-core.2.5.0.jar

Execution Flow 1:(No Problem)
1,Start Eclipse and Run Applicaton
2,Call StoreServlet.java with value 10   --- this works
3,Call FetchServlet.java                 --- this works
4,Call StoreServlet.java with value 20   --- this works
5,Call FetchServlet.java                 --- this works
6,Call ShutdownServlet.java              --- this works

即使我在尝试重新启动Eclipse之后也尝试相同的功能, 如果我尝试重新启动System(Windows)并执行以下步骤,则从存储的磁盘中获取没有对象,但在磁盘 .data文件中包含所有存储的值。

Execution Flow 2:(araises Problem)
1,Restart System,Start Eclipse and Run Applicaton
2,Call FetchServlet.java  --- no exception but objects count in cache is 0.

代码:

CacheHelper.java :
public class CacheHelper {
private static Cache profitCache = null;
private static CacheManager manager = null;
private static CacheManager getManager()
{
    if(manager == null)
    {
        Configuration cacheManagerConfig = new Configuration();
        DiskStoreConfiguration disk =new DiskStoreConfiguration();
        disk.setPath("E:/Leo-Softwares/DataStore_ehCache");
        cacheManagerConfig.addDiskStore(disk);
        manager = CacheManager.create(cacheManagerConfig);
    }
    return manager;
}
private static void getCache()
{
    CacheConfiguration cacheconfig = new CacheConfiguration("Profit",1000);
    cacheconfig.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
    .eternal(true)
    .diskPersistent(true);
    profitCache = new Cache(cacheconfig);
    getManager().addCache(profitCache); 
}
public static void insert(Profit pro)
{
    if(profitCache == null)
    {
        getCache();
    }
    Element element = new Element(pro.getId(),pro);
    profitCache.put(element);
}
public static List getAllkeys()
{
    if(profitCache == null)
    {
        getCache();
    }
    return profitCache.getKeys();
}
public static Profit get(Object obj)
{
    if(profitCache == null)
    {
        getCache();
    }
    return (Profit)profitCache.get(obj).getObjectValue();   
}
public static void shutdown()
{
    if(manager != null)
    {
        manager.shutdown();
    }
}
}

StoreServlet.java

public void doGet(HttpServletRequest request,HttpServletResponse response)
{
    int i = request.getParameter("IVAL")!=null
            ?Integer.parseInt(request.getParameter("IVAL")):1000;
            System.out.println("IVAL:"+i);
    int j =i;
    int k = i +10;
    for(  j=i;j<k;j++)
    {
        Profit pro = new Profit();
        pro.setId(j);
        pro.setAmt(j+" AMT");
        CacheHelper.insert(pro);

    }
 System.out.println("j:::"+j);
}

FetchServlet.java

public void doGet(HttpServletRequest request,HttpServletResponse response)
{
    List<Object> lst = CacheHelper.getAllkeys();
    Iterator<Object> iter = lst.iterator();
    int i = 0;
    while(iter.hasNext())
    {
        Object obj = iter.next();
        System.out.println("key:"+obj);
        Profit pro = CacheHelper.get(obj);      
        i++;
    }
    System.out.println("i in fetch@@@:"+i);
}

ShutdownServelet.java

public void doGet(HttpServletRequest request,HttpServletResponse response)
{
    CacheHelper.shutdown();
}

0 个答案:

没有答案