Ehcache not storing anything in the file - ehcache

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.

Related

Share secondary cache accross different applications

We are using a CMS with secondary cache enabled:
Application.cfc
<cfset THIS.ormsettings.secondarycacheenabled = true />
<cfset THIS.ormsettings.cacheprovider = "ehcache" />
<cfset THIS.ormsettings.cacheConfig="ehcache.xml" />
ehcache.xml
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
The entities are set to cacheuse="transactional" like so <cfcomponent persistent="true" entityname="news" table="mews" cacheuse="transactional">
Saving an article in the CMS works great and it instantly reflects changes after saving.
One of the sites which is managed by this CMS should share the cache with the CMS. Otherwise the application reflects the changes only after timeout value set in the site's ehcache.xml:
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="1800"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
Is it possible to share the cache between two different applications? Would it be possible to configure this in the two Application.cfc's?

apache shiro - session is lost when the server restarts?

I'm using Apache Shiro Security.
My settings ehcache.xml
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
<cache name="shiro-activeSessionCache"
maxElementsInMemory="10000"
overflowToDisk="true"
eternal="true"
timeToLiveSeconds="0"
timeToIdleSeconds="0"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="600"/>
EHCache Session Cache Configuration
I use these settings on two different Tomcats. I do restart the server, the first sessions are saved, the second is not. What could it be?

How to clear a specific cache region : ehcache

I have a ehcache configuration like the below :
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<cache
name="reg1"
maxElementsInMemory="100000"
eternal="false"
overflowToDisk="true"
timeToLiveSeconds="1">
</cache>
<cache
name="reg2"
maxElementsInMemory="1000000"
eternal="false"
overflowToDisk="true"
timeToLiveSeconds="100"/>
</ehcache>
I am unable to figure out how I can clear just "reg1".
I have seen a method to clear all but not a specific region.
Anyone who has figured out how to do this, please share.
Thanks
From the Ehcache documentation :
http://ehcache.org/apidocs/2.4.4/net/sf/ehcache/Cache.html#removeAll()
Removes all cached items.

ehcache diskStore location and access

I am using following configuration. I could like to see the cache file and see access the data from textpad/noteoad? is that possible? is there i can verify the data in Cache?
<cache name="cDBResponse" eternal="false"
maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LRU">
<cacheEventListenerFactory class="com.optumhealth.authorization.service.MyCacheEventListenerFactory"/>
</cache>
You should at least set diskPersistent="true" and check the location of the default diskstorage :
by default it's defined in this node
<diskStore path="java.io.tmpdir"/>
where java.io.tmpdir will depend on your OS

How to disable the Ehcache update checker?

12:18:55,541 INFO [UpdateChecker] New update(s) found: 2.0.0 [http://ehcache.org/news.html]
How do I suppress ehcache checking for new update(s), this is happening while loading my j2ee application and when ehcache is getting initialized.
One way is to place a ehcache.xml file on your classpath with the attribute updateCheck="false" in the root tag.
For example:
<ehcache updateCheck="false">
<defaultCache
maxElementsInMemory="0"
eternal="false"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
diskPersistent="false"/>
</ehcache>
Another way is to set an environment variable:
System.setProperty("net.sf.ehcache.skipUpdateCheck", "true");
simply put, in your ehcache.xml configuration file, make sure you disable the updateCheck :
<ehcache updateCheck="false">
<defaultCache
maxElementsInMemory="0"
eternal="false"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
diskPersistent="false"
/>
</ehcache>

Resources