简体   繁体   中英

How to have permanent cache using ehcache in spring mvc

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.

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 .

Have you tried this?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM