一、使用Hibernate二级缓存的步骤
1)加入二级缓存插件的jar包及配置文件
① 复制\hibernate-release-4.3.11.Final\lib\optional\ehcache\*.jar
到当前Hibernate项目的类路径下
② 复制二级缓存配置文件(\hibernate-release-4.3.11.Final\project\etc\ehcache.xml
)到当前应用的类路径下
2)配置hibernate.cfg.xml
① 配置启动hibernate的二级缓存
<property name="cache.use_second_level_cache">true</property>
② 配置hibernate二级缓存使用的产品
<property name="hibernate.cache.region.factory_class">
org.hibernate.cache.ehcache.EhCacheRegionFactory
</property>
③ 配置哪些类使用二级缓存及缓存策略
<!--
class:指定哪个类使用二级缓存
usage:使用的缓存策略
-->
<class-cache class="com.zm.bean.Employee" usage="read-write"></class-cache>
二、类级别的二级缓存
在hibernate.cfg.xml文件中进行配置时:
<!--类级别的二级缓存
class:指定哪个类使用二级缓存
usage:使用的缓存策略
- read-only:放入二级缓存的对象,只读
- nonstrict-read-write:非严格的读写
- read-write:放入二级缓存的对象可以读、写
- transactional:基于事务的策略
-->
<class-cache class="com.zm.bean.Employee" usage="read-write"></class-cache>
在*.hbm.xml映射文件中配置时:
在<class></class>
标签中使用<cache usage=""/>
配置
测试例子:
@Test
public void testClassLevelSecondCache(){
Employee emp01 = (Employee) session.get(Employee.class, 1);
System.out.println(emp01.getName());
transaction.commit();
session.close();
//新开一个session
session = sessionFactory.openSession();
transaction = session.beginTransaction();
Employee emp02 = (Employee) session.get(Employee.class, 1);
System.out.println(emp02.getName());
transaction.commit();
session.close();
}
三、集合级别的二级缓存
在hibernate.cfg.xml文件中配置时:
不仅要配置当前持久化类的二级缓存,还要配置集合中的元素所对的持久化类的二级缓存。
<class-cache class="com.zm.bean.Employee" usage="read-write"></class-cache>
<class-cache class="com.zm.bean.Department" usage="read-write"/>
<!--设置集合集合的二级缓存-->
<collection-cache collection="com.zm.bean.Department.emps" usage="read-write"/>
在映射文件中配置时:
在<class></class>
和<set></set>
标签中使用<cache usage=""/>
配置
四、ehcache的配置文件:ehcache.xml
<ehcache>
<!--<diskStore>: 指定一个目录:当 EHCache 把数据写到硬盘上时, 将把数据写到这个目录下-->
<diskStore path="java.io.tmpdir"/>
<!--<defaultCache>: 设置缓存的默认数据过期策略 -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
<!--
<cache> 设定具体的命名缓存的数据过期策略。每个命名缓存代表一个缓存区域
缓存区域(region):一个具有名称的缓存块,可以给每一个缓存块设置不同的缓存策略。如果没有设置任何的缓存区域,则所有被缓存的对象,都将使用默认的缓存策略。即:<defaultCache.../>
Hibernate在不同的缓存区域保存不同的类/集合。
对于类而言,区域的名称是类名。如:com.zm.bean.Employee
对于集合而言,区域的名称是类名加属性名。如:com.zm.bean.Department#emps
-->
<cache name="sampleCache1"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
<!-- cache 元素的属性
name:设置缓存的名字,它的取值为类的全限定名或类的集合的名字
maxInMemory:设置基于内存的缓存中可存放的对象最大数目
eternal:设置对象是否为永久的,true表示永不过期,此时将忽略timeToIdleSeconds 和 timeToLiveSeconds属性; 默认值是false
timeToIdleSeconds:设置对象空闲最长时间,以秒为单位, 超过这个时间,对象过期。当对象过期时,EHCache会把它从缓存中清除。如果此值为0,表示对象可以无限期地处于空闲状态。
timeToLiveSeconds:设置对象生存最长时间,超过这个时间,对象过期。如果此值为0,表示对象可以无限期地存在于缓存中. 该属性值必须大于或等于 timeToIdleSeconds 属性值
overflowToDisk:设置基于内存的缓存中的对象数目达到上限后,是否把溢出的对象写到基于硬盘的缓存中
-->
<cache name="sampleCache2"
maxElementsInMemory="1000"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/>
</ehcache>
五、查询缓存
对于经常使用的查询语句,如果启用了查询缓存,当第一次执行查询语句时,Hibernate 会把查询结果存放在查询缓存中。以后再次执行该查询语句时,只需从缓存中获得查询结果,从而提高查询性能
查询缓存使用于如下场合:
- 应用程序运行时经常使用查询语句
- 很少对与查询语句检索到的数据进行插入,删除和更新操作
启用查询缓存的步骤
- 配置二级缓存,因为查询缓存依赖于二级缓存
- 在 hibernate 配置文件中启用查询缓存
<!--开启查询缓存-->
<property name="cache.use_query_cache">true</property>
- 对于希望启用查询缓存的查询语句,调用
Query
的setCacheable()
方法
//查询缓存
@Test
public void testQueryCache(){
Query query = session.createQuery("from Employee");
//使用查询缓存
query.setCacheable(true);
List<Employee> emps = query.list();
System.out.println(emps.size());
emps = query.list();
System.out.println(emps.size());
}
默认情况下,设置的缓存对HQL及QBC查询是无效的,但可以通过以下方式使其有效:
(1)在hibernate配置文件中声明开启查询缓存
<property name="cache.use_query_cache">true</property>
(2)调用Query
或Criteria
的setCacheable()
方法
Criteria criteria = session.createCriteria(Employee.class);
criteria.setCacheable(true);