Java ehcache xml需要defaultCache元素,其他缓存正在获取defaultCache属性
这就是我的ehcache.xml的样子:Java ehcache xml需要defaultCache元素,其他缓存正在获取defaultCache属性,java,hibernate,jpa,ehcache,Java,Hibernate,Jpa,Ehcache,这就是我的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 nam
<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>
My 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秒后过期
谢谢,使用Hibernate时,需要创建大量缓存。除非在配置中明确定义它们,否则将使用
defaultCache
机制
这意味着当Hibernate需要缓存时,它将从CacheManager
请求缓存,如果该缓存不存在,Ehcache将使用defaultCache
定义来创建它
因此,有两种选择:
defaultCache
名为
PreferenceValueEntity
的实体的缓存名称必须是该实体的完全限定类名。例如,com.my.package.PreferenceValueEntity
(我不知道PreferenceValueEntity
的包名是什么,所以我只是在这里编造^^^)
因此,您的配置应该如下所示:
<cache name="com.my.package.PreferenceValueEntity" eternal="false" maxElementsInMemory="1000"
timeToIdleSeconds="5" timeToLiveSeconds="5" overflowToDisk="false" memoryStoreEvictionPolicy="LRU" />
这是一个很好的例子
提供了使用Hibernate二级缓存的良好教程。有什么例外?