2012-12-24 48 views
0

我是EhCache的新手,并且实现了分布式缓存服务。我正在尝试简单的程序,但无法解决错误。我可以将数据存储到缓存中,但无法检索。EhCache:简单的程序不能正常工作

这些是我为测试写的两个简单程序。

package com.db.tests; 

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

public class TestCachePut { 

    public TestCachePut() { 
     // TODO Auto-generated constructor stub 
    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 

     CacheManager cache= CacheManager.create("E:/ehcache-2.6.2/ehcache.xml"); 

     Cache caches = cache.getCache("cache1"); 

     Element element= new Element("testKey", "inserted to cache"); 
     caches.put(element); 

     System.out.println("Put in to cache"); 



    } 

} 

方案2:

package com.db.tests; 

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

public class TestCacheGet { 

    public TestCacheGet() { 
     // TODO Auto-generated constructor stub 
    } 

    /** 
    * @param args 
    */ 

    public static void main(String[] args) { 




     CacheManager caches = CacheManager.getInstance(); 
     Element val = caches.getCache("cache1").get("testKey"); 
     System.out.println(" cache content: "+val.getValue()); 


    } 

} 

我已经配置像<cache name="cache1" maxEntriesLocalHeap="10000" maxEntriesLocalDisk="1000" eternal="false" diskSpoolBufferSizeMB="20" timeToIdleSeconds="3000" timeToLiveSeconds="6000" memoryStoreEvictionPolicy="LFU" transactionalMode="off"> <persistence strategy="localTempSwap"/> </cache>

陶土服务器启动的ehcache.xml中,并显示其运行。当我运行程序1时,它运行时没有任何错误。这之后,我跑了第二个,我得到了错误的NPE,

24 Dec, 2012 12:05:42 PM net.sf.ehcache.config.ConfigurationFactory parseConfiguration 
WARNING: No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/E:/ehcache-2.6.2/lib/ehcache-core-2.6.2.jar!/ehcache-failsafe.xml 
Exception in thread "main" java.lang.NullPointerException 
    at com.db.tests.TestCacheGet.main(TestCacheGet.java:22) 

,如果我做的指定在两种情况下的XML配置,其发电再次NPE一个警告:

4 Dec, 2012 12:22:34 PM net.sf.ehcache.DiskStorePathManager resolveAndLockIfNeeded 
WARNING: diskStorePath 'E:\Users\SKRISH~1\AppData\Local\Temp' is already used by an existing CacheManager either in the same VM or in a different process. 
The diskStore path for this CacheManager will be set to E:\Users\SKRISH~1\AppData\Local\Temp\ehcache_auto_created1315701513913502723diskstore. 
To avoid this warning consider using the CacheManager factory methods to create a singleton CacheManager or specifying a separate ehcache configuration (ehcache.xml) for each CacheManager instance. 

我在做什么错误?请输入一些内容。

感谢

回答

1

看起来像要通过Terracotta分发(并坚持JVM重新启动)您的缓存。在这种情况下,你必须通过添加指向您的服务器TerracottaConfig元素的ehcache.xml中内分布(在同一级别的高速缓存)缓存

<terracottaConfig url="localhost:9510" /> 

在此之后的很到位,加上

<terracotta/> 

标记到您的缓存,使其分布式。

希望有帮助!

1

指定配置文件中TestCacheGet也:

CacheManager cacheManager = CacheManager.create("E:/ehcache-2.6.2/ehcache.xml"); 

将帖子

而且,localTempSwap策略是虚拟机的具体。尝试使用localRestartable并指定diskStore path。 (仅适用于企业版)

<ehcache> 
<diskStore path="/Users/me/store/data"/> 
<cache name="cache1" maxEntriesLocalHeap="10000" maxEntriesLocalDisk="1000" eternal="false" diskSpoolBufferSizeMB="20" timeToIdleSeconds="3000" timeToLiveSeconds="6000" memoryStoreEvictionPolicy="LFU" transactionalMode="off"> <persistence strategy="localRestartable" synchronousWrites="true"/> </cache> 
</ehcache> 
+0

没有。它再次给我NPE。请检查编辑的问题 – Kris