ehcache是Hibernate的二级缓存,Hibernate有两种缓存一种是Session里面的一级缓存,
再有就是第三方的二级缓存,这个必须手工配置,要不然是不会生效的,
用Ehcache并不代表不操作数据库了,而是它会把实体类在内存中放一个缓存, 当不修改数据库时,
它就不用时行数据库操作了,比如说读取的操作,如果进行保存的话,他也会更新缓存的。
配置方法很简单:
在Application.xml里的SessionFactory里加上如下:
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">
true
</prop>
这是启用缓存,另外还需要在ClassPath下建立:
ehcache.xml这个文件,里面这样写就行了:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!-- enable second level cache  -->
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="100" eternal="false"
timeToIdleSeconds="300" timeToLiveSeconds="180" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" overflowToDisk="false" />
<cache name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="100" eternal="true" overflowToDisk="true" />
        <cache name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="100" eternal="false" timeToLiveSeconds="120"
overflowToDisk="true" />
<cache name="com.clove.cms.model.User" maxElementsInMemory="100"
eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="180"
overflowToDisk="false" />
</ehcache>User,就是你的实体类了!
别忘了加Jar包呀!