ehcache持续存在磁盘问题 [英] ehcache persist to disk issues
问题描述
我想用Java中的ehcache做一些我认为应该非常简单的事情,但是我花了足够的时间使自己对文档感到沮丧...
I want to do something with ehcache in Java that I think should be extremely simple, but I've spent enough time frustrating myself with the docs...
-
将值写入磁盘持久性缓存.闭嘴.
Write a value to a disk persistent cache. Shut down.
再次启动并读取该值.
这是我的Java函数:
Here is my Java function:
private static void testCacheWrite() {
// create the cache manager from our configuration
URL url = TestBed.class.getClass().getResource("/resource/ehcache.xml");
CacheManager manager = CacheManager.create(url);
// check to see if our cache exits, if it doesn't create it
Cache testCache = null;
if (!manager.cacheExists("test")) {
System.out.println("No cache found. Creating cache...");
int maxElements = 50000;
testCache = new Cache("test", maxElements,
MemoryStoreEvictionPolicy.LFU, true, null, true, 60, 30,
true, Cache.DEFAULT_EXPIRY_THREAD_INTERVAL_SECONDS, null);
manager.addCache(testCache);
// add an element to persist
Element el = new Element("key", "value");
testCache.put(el);
testCache.flush();
System.out.println("Cache to disk. Cache size on disk: " +
testCache.getDiskStoreSize());
} else {
// cache exists so load it
testCache = manager.getCache("test");
Element el = testCache.get("key");
if (null == el) {
System.out.print("Value was null");
return;
}
String value = (String) el.getObjectValue();
System.out.println("Value is: " + value);
}
manager.shutdown();
}
这是我的缓存配置(ehcache.xml):
And here is my cache configuration (ehcache.xml):
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<diskStore path="C:/mycache"/><!-- java.io.tmpdir -->
<defaultCache
maxElementsInMemory="10000"
eternal="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
即使我第一次运行后在磁盘上看到test.index和test.data文件,该函数的输出始终如下(它似乎从未从磁盘加载缓存):
Even though I see test.index and test.data files on disk after the first run, output from this function is always the following (it never seems to load the cache from disk):
未找到缓存.正在创建缓存...
缓存到磁盘.磁盘上的缓存大小:2
No cache found. Creating cache...
Cache to disk. Cache size on disk: 2
我必须在这里做些愚蠢的事,但是我不确定是什么!
I must be doing something dumb here, but I 'm not sure what!
推荐答案
好的,我要解决的办法是使用配置文件配置缓存.这是更新的配置:
Okay, well what I did to fix this was configure my cache using the configuration file. Here is the updated config:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<diskStore path="C:/mycache" />
<defaultCache
maxElementsInMemory="10000"
eternal="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />
<cache
name="test"
maxElementsInMemory="500"
eternal="true"
overflowToDisk="true"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="1"
memoryStoreEvictionPolicy="LFU" />
</ehcache>
所以基本上我没有使用构造函数来定义缓存.
So basically I didn't use the constructor to define the cache.
我想这行得通,但是我仍然想知道为什么以编程方式定义的缓存不能在磁盘上持久存在(特别是因为它们仍被写入磁盘!).
I suppose this will work, but I still wonder why programatically defined caches can't persist on disk (especially since they are still written to disk!).
感谢大家的评论.
这篇关于ehcache持续存在磁盘问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!