1

I want to use ehcache in my spring mvc web application.because my server reset every day so i want caching be permanent. do i save it in hard path? and hoe save it? thanks.

in my dispatcher-servlet.xml i add this

 <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
   <property name="cacheManager" ref="ehcache"/>
 </bean>
 <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  <property name="configLocation" value="classpath:ehcache.xml"/>
  <property name="shared" value="true"/>
</bean>

and my ehcach.xml is

  <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="c:/tmp"/>

<defaultCache
        maxElementsInMemory="500" eternal="true" overflowToDisk="false" memoryStoreEvictionPolicy="LFU"/>
      <cache name="mycache"
       maxElementsInMemory="0"
       eternal="true"
       timeToIdleSeconds="120"
       timeToLiveSeconds="120"
       overflowToDisk="true"
       maxElementsOnDisk="10000000"
       diskPersistent="true"
       diskExpiryThreadIntervalSeconds="1200"
       memoryStoreEvictionPolicy="LRU">
      [1] <persistence strategy="localRestartable" synchronousWrites="false" /> 
      </cache>

i add this [1] until caching be permanent and after server reset not be remove.but this exception occur Element does not allow nested elements. also i use ehcach-core2.7.0.jar

    Element <cache> does not allow nested <persistence> elements.

2 Answers 2

3

You should not mix legacy configuration options - attributes diskPersistent and overflowToDisk on the cache element with recommended persistence element.

However, to get to the open source disk persistent setting, you need to stick with the legacy options.

So your configuration should drop the persistence element to become:

<cache name="mycache"
     maxElementsInMemory="0"
     eternal="true"
     timeToIdleSeconds="120"
     timeToLiveSeconds="120"
     overflowToDisk="true"
     maxElementsOnDisk="10000000"
     diskPersistent="true"
     diskExpiryThreadIntervalSeconds="1200"
     memoryStoreEvictionPolicy="LRU">
</cache>

However, you should also give a meaningful value to maxElementsInMemory, so you can have a hot set of entries for which you do not need to pay the deserialization price when accessing them.

You also need to decide if you want eternal elements or have expiration. For this, remove either eternal="true" or the timeToLiveSeconds and timeToIdleSeconds pair. Having both is not an error in Ehcache for compatibility reasons, but makes it hard to know what you intended initially.

And as a last advice, I would move the cache content to a folder with a more descriptive name instead of c:/tmp.

Note that the open source disk persistent tier is not fault tolerant, so improper shutdown of the Cache or CacheManager or exceptions while doing IO can corrupt the data. If that happens, you will have to clear the data folder before you can restart your cache.

For more details, see the Ehcache 2.7 persistence documentation.

Sign up to request clarification or add additional context in comments.

9 Comments

why after rerun project all data cache removed?
Can you check your logs and see if there is any warning about the disk data? Otherwise, pretty hard to conclude anything...
when i reset server the cache data save at path(C:/mycache) but when start server all request not use cache. example: i cache a request with id=20 when get request for this id(20) the body of method run for get data from db.while this id cached.
while the server not be reset all request use form cache.
How to use BootstrapCacheLoaderFactory to load diskStore into memory?
|
0

Have you tried this?

<cache eternal="true"
  maxElementsInMemory="0"
  name="<cache name>"
  overflowToDisk="true"/>

7 Comments

where the cache data save? i use <diskStore path="c:/tmp"/> to save in it but after caching it's size is zero.
You will need to post a full working example for someone to check. The information is insufficient to comment on what is happening and why.
With Ehcache, entries are written to the disk store only if the memory store is full. If you set maxElementsInMemory to 1000 you cannot expect the first cache entry in the cache to be written to the disk. You need to set maxElementsInMemory to 0 in order to write every single cache entry to the disk. This will in many cases defeat the purpose of having the cache because every cache access will result in disk I/O, but if what you want is having every single cache entry on the disk, this is the only way to get it done. See (ehcache.xml)[ehcache.org/ehcache.xml] for full details.
Manish, your comment is incorrect. Ehcache, starting with version 2.6.0, no longer uses an overflow model but rather a model where all entries are present in the lower tier - disk in this case. See answer there
The approach to have a cache between the controller and the database is certainly correct. The issue seems to be with the exact application you are working on rather than the general behaviour of Ehcache. If it is possible for you, do take a look at my sample app which is fully working. You can play around with the configuration there to get the results you want and then port the configuration to your application.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.