我是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());
}
}
我将ehcache.xml配置为<cache name="cache1"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="1000"
eternal="false"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="3000"
timeToLiveSeconds="6000"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
<persistence strategy="localTempSwap"/>
</cache>
Teracotta服务器已启动,并显示其正在运行。当我运行程序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.
我做错了什么?请一些意见。
由于
答案 0 :(得分:1)
在TestCacheGet
中指定配置文件:
CacheManager cacheManager = CacheManager.create("E:/ehcache-2.6.2/ehcache.xml");
[EDITED]
而且,localTempSwap
策略是特定于VM的。在指定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>
答案 1 :(得分:1)
您希望通过Terracotta分发(并通过JVM重新启动)缓存。在这种情况下,您必须通过在ehcache.xml中添加指向服务器的TerracottaConfig元素(与缓存相同的级别)来分配缓存
<terracottaConfig url="localhost:9510" />
在此之后,添加
<terracotta/>
标记到缓存以使其分发。
希望有所帮助!