这就是我的ehcache.xml的样子:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="false" name="defaultCache1">
<diskStore path="java.io.tmpdir" />
<defaultCache name="defaultCache" maxElementsInMemory="10000" eternal="false" statistics="true" timeToIdleSeconds="10"
timeToLiveSeconds="10" overflowToDisk="false" diskPersistent="false" memoryStoreEvictionPolicy="LRU" />
<cache name="PreferenceValueEntity" eternal="false" maxElementsInMemory="1000"
timeToIdleSeconds="5" timeToLiveSeconds="5" overflowToDisk="false" memoryStoreEvictionPolicy="LRU" />
</ehcache>
我的persistence.xml包含:
<!-- EHCache managed by hibernate -->
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" />
<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" />
<property name="net.sf.ehcache.configurationResourceName" value="/META-INF/ehcache.xml"/>
我正在使用 - JPA和Hibernate 5.2.x - ehcache-2.10.3
问题是timeToIdleSeconds是从defaultCache继承的,因此缓存在10秒后过期而不是5秒 .
-
我没有't need the defaultCache, but removing it from ehcache.xml throws exception on tomcat startup. Forcing me to add it to ehcache.xml. I know per ehcache documentation it is not required, but not sure what' s导致它被要求 .
-
为什么timeToLiveSeconds是从defaultCache继承的 .
解决它们中的任何一个都将解决我的问题,但是如果两个问题都解决了,那就太好了 .
谢谢,
2 回答
使用Hibernate时,需要创建相当多的缓存 . 除非您在配置中明确定义它们,否则将使用
defaultCache
机制 .这意味着当Hibernate需要缓存时,它将从
CacheManager
请求它,如果该缓存不存在,Ehcache将使用defaultCache
定义来创建它 .所以有两个选择:
根据您的需要配置
defaultCache
标识应用程序所需的所有缓存名称,并明确定义它们 .
名为
PreferenceValueEntity
的实体的缓存名称必须是实体的完全限定类名 . 例如com.my.package.PreferenceValueEntity
(我不知道PreferenceValueEntity
的包名是什么,所以我只是在这里弥补^^) .所以你的配置应该是这样的:
This explanation in the ehcache documentation给出了一个很好的例子 .
This post提供了一个关于使用Hibernate的二级缓存的好教程 .