一.hibernate缓存
缓存(Cache): 计算机领域非常通用的概念。它介于应用程序和永久性数据存储源(如硬盘上的文件或者数据库)之间,其作用是降低应用程序直接读写永久性数据存储源的频率,从而提高应用的运行性能。缓存中的数据是数据存储源中数据的拷贝。缓存的物理介质通常是内存
Hibernate中提供了两个级别的缓存
第一级别的缓存是 Session 级别的缓存,它是属于事务范围的缓存。这一级别的缓存由 hibernate 管理的
第二级别的缓存是 SessionFactory 级别的缓存,它是属于进程范围的缓存
适合放入二级缓存中的数据:
很少被修改
不是很重要的数据, 允许出现偶尔的并发问题
不适合放入二级缓存中的数据:
经常被修改
财务数据, 绝对不允许出现并发问题
与其他应用程序共享的数据
二级缓存的架构图
二.二级缓存的使用案例
案例一
@Test
public void testGet() {
Employee employee=(Employee) session.get(Employee.class, 15);
System.out.println(employee);
Employee employee2=(Employee) session.get(Employee.class, 15);
System.out.println(employee);
}
大家都知道第一遍查出来的结果存放在session的一级缓存中,所以只发送一条sql语句
但是如果中间关闭session缓存再新建一个呢?
@Test
public void testGet() {
Employee employee=(Employee) session.get(Employee.class, 15);
System.out.println(employee);
session.close();
session=factory.openSession();
Transaction ts=session.beginTransaction();
Employee employee2=(Employee) session.get(Employee.class, 15);
System.out.println(employee);
}
测试结果会发送两条sql语句.
问题: 如果我们需要在这种情况下只发送一条sql语句呢?
这时候就需要用到二级缓存
这时候就需要用到二级缓存
如何配置二级缓存?
1.加入所需jar包
位于:hibernate-release-4.3.11.Final\lib\optional\ehcache
2.加入ehcache.xml
位于:hibernate-release-4.3.11.Final\project\etc
3.配置cfg,xml文件里的二级缓存
<!-- 启用二级缓存 -->
<property name="cache.use_second_level_cache">true</property>
<!-- 配置使用二级缓存的产品 -->
<property name="hibernate.cache.region.factory_class"> org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<!-- 设置二级缓存对哪些类的并发策略 -->
<class-cache usage="read-write" class="com.eduask.chp.bean.Employee"/>
也可以在hbm,xml文件里设置并发策略
设置完毕然后你再测试会发现只发送一条sql语句
案例二
基于集合的设置
@Test
public void testGet() {
Department department=(Department) session.get(Department.class, 5);
System.out.println(department.getDeptName()+department.getEmps().size());
session.close();
session=factory.openSession();
Transaction ts=session.beginTransaction();
Department department2=(Department) session.get(Department.class, 5);
System.out.println(department2.getDeptName()+department2.getEmps().size());
}
<!-- 启用二级缓存 -->
<property name="cache.use_second_level_cache">true</property>
<!-- 配置使用二级缓存的产品 -->
<property name="hibernate.cache.region.factory_class"> org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<!-- 加载对应类的执行策略 -->
<class-cache usage="read-only" class="com.eduask.chp.bean.Employee"/>
<class-cache usage="read-only" class="com.eduask.chp.bean.Department"/>
<collection-cache usage="read-only" collection="com.eduask.chp.bean.Department.emps"/>
同样你也可以在hbm.xml文件里设置
并发访问策略
三.ehcache
<diskStore path="java.io.tmpdir"/>
<!--<diskStore>: 指定一个目录:当 EHCache 把数据写到硬盘上时, 将把数据写到这个目录下.
-->
<defaultCache
maxElementsInMemory="10000"
<!-- maxInMemory:设置基于内存的缓存中可存放的对象最大数目 -->
eternal="false"
<!--设置对象是否为永久的,true表示永不过期,此时将忽略timeToIdleSeconds 和 timeToLiveSeconds属性; 默认值是false
-->
timeToIdleSeconds="120"
<!--设置对象空闲最长时间,以秒为单位, 超过这个时间,对象过期。当对象过期时,EHCache会把它从缓存中清除。如果此值为0,表示对象可以无限期地处于空闲状态。
-->
timeToLiveSeconds="120"
<!--设置对象生存最长时间,超过这个时间,对象过期。如果此值为0,表示对象可以无限期地存在于缓存中. 该属性值必须大于或等于 timeToIdleSeconds 属性值
-->
overflowToDisk="true"
<!--设置基于内存的缓存中的对象数目达到上限后,是否把溢出的对象写到基于硬盘的缓存中
-->
/>
四.查询缓存
查询缓存配置
两步:
1.在cfg.xml文件里加入以下
<!--启用查询缓存 -->
<property name="cache.use_query_cache">true</property>
2.然后
@Test
public void testGet() {
String hql="from Employee where empId=?";
Query query=session.createQuery(hql);
query.setCacheable(true);//设置为true
List<Employee> list=query.setInteger(0, 19).list();
System.out.println(list);
list=query.setInteger(0, 19).list();
System.out.println(list);
}
五.时间戳缓存
了解下,可以做个测试,然后查看sql语句