我已经阅读了很多答案并查看了ehcache文档,但我找不到解决此问题的方法。 Ehache只是不想为我工作。
我正在使用spring 4.1.6 RELEASE和hibernate 4.3.5 Final,我想启用缓存,所以我不必每次都访问DB。
这是我的pom.xml的一部分:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate-version}</version>
</dependency>
hibernate.cfg.xml的相关部分:
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
这是ehcache.xml:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true">
<diskStore path="java.io.tmpdir"/>
<defaultCache
eternal="false"
maxElementsInMemory="100"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
<cache name="model.entities.User" maxElementsInMemory="100" eternal="true" timeToIdleSeconds="300" timeToLiveSeconds="600" />
</ehcache>
一个问题是,如果我将maxEntriesLocalHeap属性添加到defaultCache,我得到
error: Element <defaultCache> does not allow attribute "maxEntriesLocalHeap".
虽然XSD将“maxEntriesLocalHeap”指定为defaultCache的属性。 我不太关心那个参数,所以我把它删除了,但这很奇怪。 好吧,至少我知道SOMETHING :)读取ehcache.xml。 我知道这也是因为我得到这样的输出:
WARN : org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory - HHH020003: Could not find a specific ehcache configuration for cache named [model.entities.Role]; using defaults.
此输出中没有User类,因此在ehcache.xml中添加最后一个缓存标记确实有一些效果。
我必须说我也把这一行放在每个hbm.xml文件中:
<cache usage="read-write" />
这一切似乎都有效,我没有得到任何错误,但是看看mysql日志文件显示mysql服务器非常频繁地获得具有相同参数的相同选择查询,尽管此行未被修改。这就是我想要避免的。
我没有在spring配置文件中做任何具体的事情。 我应该吗?
提前感谢您的帮助。我为此疯狂。