[英]How to have permanent cache using ehcache in spring mvc
我想在我的spring mvc web应用程序中使用ehcache。因为我的服务器每天都重置,所以我希望缓存是永久性的。 我将它保存在硬路径中吗? 和锄头保存吗? 谢谢。
在我的dispatcher-servlet.xml中,我添加了这个
<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>
我的ehcach.xml是
<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>
我添加这个[1],直到缓存是永久性的,并且在服务器重置后不能删除。但是这个异常发生Element不允许嵌套元素。 我也使用ehcach-core2.7.0.jar
Element <cache> does not allow nested <persistence> elements.
您不应该将旧的配置选项 - 使用推荐的persistence
元素在cache
元素上的diskPersistent
和overflowToDisk
属性混合。
但是,要获得开源磁盘持久性设置,您需要坚持使用旧版选项。
因此,您的配置应该将persistence
元素删除为:
<cache name="mycache"
maxElementsInMemory="0"
eternal="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="1200"
memoryStoreEvictionPolicy="LRU">
</cache>
但是,您还应该为maxElementsInMemory提供有意义的值,这样您就可以拥有一组热门条目,在访问它们时无需支付反序列化价格。
您还需要决定是否需要永久元素或过期。 为此,删除eternal="true"
或timeToLiveSeconds
和timeToIdleSeconds
对。 出于兼容性原因,两者都不是Ehcache中的错误,但是很难知道您最初的意图。
作为最后的建议,我会将缓存内容移动到一个名称更具描述性的文件夹而不是c:/tmp
。
请注意,开源磁盘持久层不具有容错能力,因此在执行IO时不正确地关闭Cache
或CacheManager
或异常会破坏数据。 如果发生这种情况,您必须先清除数据文件夹,然后才能重新启动缓存。
有关更多详细信息,请参阅Ehcache 2.7持久性文档 。
你试过这个吗?
<cache eternal="true"
maxElementsInMemory="0"
name="<cache name>"
overflowToDisk="true"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.