0

I am trying to do a simple thing like store something in the cache and retrieve it next time if it exists. For some reason everything works fine for the first time, when called the second time, everything in the cache file is removed and the cache is created again. Here is my ehcache config file

<ehcache>
  <diskStore path="<TEMP_DIR_PATH>" />
  <defaultCache maxElementsInMemory="10000" eternal="true"
      timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
      maxElementsOnDisk="10000000" diskPersistent="true"
      diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
   <cache name="mycache" maxElementsInMemory="1" eternal="true"
       overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600"
       diskPersistent="true" diskExpiryThreadIntervalSeconds="1"
       memoryStoreEvictionPolicy="LFU"/>
</ehcache>

The code actually creates 2 files one named mycache.index and the other named mycache.data. The code to put the value in to cache is given below.

Cache cache = cacheManager.getCache("mycache");
Element myElement= new Element("KEY1","This will be stored in cache");
cache.put(myElement);

Could someone please point out where things are going wrong?

I wanted to use the same stored cache file everytime and create a new file only if the data file is not present.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
siva raman
  • 17
  • 1
  • 5

2 Answers2

0
CacheManager.getInstance().addCache("test");
        CacheManager.getInstance().getCache("test").put(new Element("name", "abhi"));
        CacheManager.getInstance().getCache("test").put(new Element("class", "ten"));
        CacheManager.getInstance().getCache("test").put(new Element("age", "24"));

        Element elt = CacheManager.getInstance().getCache("test").get("class");
        //return (elt == null ? null : elt.getObjectValue());
        System.out.println(elt);

Hope this will help and will work fine for storing.

0

You need to call cache.flush() or cache.dispose() to dump the pending data to disk.

dma_k
  • 10,431
  • 16
  • 76
  • 128
  • @sivaraman: While thinking about this EhCache design I came to conclusion, that flushing to disk every time something is put into cache is effective (cache should be really fast, right?) so we can forgive EhCache this minor API unclarity. – dma_k Sep 08 '11 at 14:04