Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ehcache persist to disk issues

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...

  1. Write a value to a disk persistent cache. Shut down.

  2. Start up again and read that value.

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(); } 

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> 

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):

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!

like image 211
hross Avatar asked Nov 13 '09 14:11

hross


People also ask

Is Ehcache persistent?

Ehcache offers persistence using the local disk as a cache storage tier. While Ehcache offers various disk usage choices, as of version 2.6, the recommended option for persistence is the Fast Restart store, which is available in BigMemory Go and BigMemory Max.

What happens when Ehcache is full?

In Ehcache, the MemoryStore may be limited in size (see How to Size Caches for more information). When the store gets full, elements are evicted. The eviction algorithms in Ehcache determine which elements are evicted.

Is Ehcache in memory?

1. Overview. In this article, we will introduce Ehcache, a widely used, open-source Java-based cache. It features memory and disk stores, listeners, cache loaders, RESTful and SOAP APIs and other very useful features.

Where does Ehcache store data?

Data stores supported by Ehcache include: On-Heap Store - Utilizes Java's on-heap RAM memory to store cache entries. This tier utilizes the same heap memory as your Java application, all of which must be scanned by the JVM garbage collector.


1 Answers

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!).

Thanks for the comments guys.

like image 61
hross Avatar answered Sep 30 '22 19:09

hross