【问题标题】:How to have permanent cache using ehcache in spring mvc如何在spring mvc中使用ehcache进行永久缓存
【发布时间】:2014-12-31 22:41:23
【问题描述】:

我想在我的 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] 直到缓存是永久的,并且在服务器重置后不会被删除。但是这个异常发生元素不允许嵌套元素。 我也使用 ehcach-core2.7.0.jar

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

【问题讨论】:

    标签: java spring-mvc caching ehcache


    【解决方案1】:

    您不应将旧配置选项 - cache 元素上的属性 diskPersistentoverflowToDisk 与推荐的 persistence 元素混合使用。

    但是,要获得开源磁盘持久性设置,您需要坚持使用旧选项。

    所以你的配置应该删除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"timeToLiveSecondstimeToIdleSeconds 对。出于兼容性原因,两者都不是 Ehcache 中的错误,但会让人很难知道您最初的意图。

    最后的建议是,我会将缓存内容移动到一个名称更具描述性的文件夹,而不是 c:/tmp

    请注意,开源磁盘持久层不是容错的,因此不正确地关闭 CacheCacheManager 或在执行 IO 时出现异常可能会损坏数据。如果发生这种情况,您必须先清除数据文件夹,然后才能重新启动缓存。

    更多详情请见the Ehcache 2.7 persistence documentation

    【讨论】:

    • 为什么重新运行项目后所有数据缓存都被删除了?
    • 你能检查一下你的日志,看看有没有关于磁盘数据的警告吗?否则,很难得出任何结论……
    • 当我重置服务器时缓存数据保存在路径(C:/mycache)但是当启动服务器时所有请求都不使用缓存。示例:当获取此 id(20) 的请求时,我缓存了一个 id=20 的请求,该方法的主体运行以从 db 获取数据。而此 id 已缓存。
    • 当服务器没有被重置时,所有请求都使用表单缓存。
    • 如何使用BootstrapCacheLoaderFactory将diskStore加载到内存中?
    【解决方案2】:

    你试过了吗?

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

    【讨论】:

    • 缓存数据保存在哪里?我使用 来保存它,但缓存后它的大小为零。
    • 您需要发布一个完整的工作示例供他人检查。这些信息不足以评论正在发生的事情和原因。
    • 使用 Ehcache,只有在内存存储已满时,才会将条目写入磁盘存储。如果将maxElementsInMemory 设置为1000,则不能期望缓存中的第一个缓存条目被写入磁盘。您需要将maxElementsInMemory 设置为0,以便将每个缓存条目写入磁盘。在许多情况下,这会破坏拥有缓存的目的,因为每次缓存访问都会导致磁盘 I/O,但是如果您想要在磁盘上拥有每个缓存条目,那么这是完成它的唯一方法。请参阅 (ehcache.xml)[ehcache.org/ehcache.xml] 了解完整详情。
    • Manish,您的评论不正确。从 2.6.0 版本开始,Ehcache 不再使用溢出模型,而是使用所有条目都存在于较低层(在本例中为磁盘)的模型。见answer there
    • 在控制器和数据库之间建立缓存的方法当然是正确的。问题似乎在于您正在处理的确切应用程序,而不是 Ehcache 的一般行为。如果可能的话,请查看我的sample app,它正在完全工作。您可以在那里尝试配置以获得所需的结果,然后将配置移植到您的应用程序。
    猜你喜欢
    • 1970-01-01
    • 2016-08-24
    • 2016-11-22
    • 1970-01-01
    • 2020-12-18
    • 2011-06-20
    • 2012-08-15
    • 1970-01-01
    • 2014-03-16
    相关资源
    最近更新 更多