【问题标题】:Ehcache in Spring not find the name?Spring找不到Ehcache的名字?
【发布时间】:2015-12-21 19:16:01
【问题描述】:

我有一个带有休眠和 MySQL 的 Spring Rest 应用程序。

服务的 ehcache 在测试中工作,在 tomcat 中失败。

Spring EhCache

    <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/cache.xml" />
        <property name="shared" value="true" />
   </bean>

上下文

<cache:annotation-driven />
<context:component-scan base-package="com.example" />
  ...
<import resource="classpath:spring/spring-cache.xml" />

Cache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true" maxBytesLocalHeap="150M">

    <diskStore path="java.io.tmpdir" />

    <cache name="byCategory" eternal="false" diskSpoolBufferSizeMB="20"
    timeToIdleSeconds="300" timeToLiveSeconds="600"
    memoryStoreEvictionPolicy="LFU" transactionalMode="off">

    </cache>
</ehcache>

休眠缓存仅在生产中

    <prop key="hibernate.cache.use_query_cache">true</prop>
    <prop key="hibernate.cache.use_second_level_cache">true</prop>
    <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
    <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
    </prop>

我有一个门面,有一个 mtehod 有注释

@Cacheable(value = { "byCategory" }, key = "#ids.toString()")

这在测试中运行正常,因为未使用休眠 ehcache,但在 prod 中,CacheManager 不会加载我在 xml 中定义的名称。

可以合并hibernate和own的名字吗?

【问题讨论】:

  • Hibernate 和基于 Spring 的缓存是不同的野兽,因此缓存不可共享。

标签: java spring hibernate ehcache


【解决方案1】:

您应该能够在同一个应用程序中使用多个CacheManager

但请确保它们具有不同的名称。根据您的设置,首先通过命名您使用 classpath:ehcache/cache.xml 配置的 CacheManager 进行测试可能是最简单的。

类似下面的东西应该可以解决问题:

<ehcache name="aName"
  <!-- Your configuration -->
</ehcache>

【讨论】:

    猜你喜欢
    • 2017-06-19
    • 2022-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 2016-09-08
    • 1970-01-01
    相关资源
    最近更新 更多