【发布时间】: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