Java で ehcache を使用して、非常に単純なはずのことをしたいと思っていますが、ドキュメントにイライラするのに十分な時間を費やしました...
ディスク永続キャッシュに値を書き込みます。シャットダウン。
もう一度起動して、その値を読み取ります。
ここに私のJava関数があります:
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)があります:
<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 ファイルが表示されますが、この関数からの出力は常に次のようになります (ディスクからキャッシュをロードすることはないようです)。
キャッシュが見つかりません。キャッシュを作成しています...
ディスクにキャッシュします。ディスク上のキャッシュ サイズ: 2
私はここでばかげたことをしているに違いありませんが、何がわからないのですか!