Ehcache not storing anything in the file
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.
You need to call cache.flush()
or cache.dispose()
to dump the pending data to disk.
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.
精彩评论