1.什么是缓存?
缓存是介于物理数据源与应用程序之间,是对数据库中的数据复制一份临时放在内存中的容器,其作用是为了减少应用程序对物理数据源访问的次数,从而提高了应用程序的运行性能。Hibernate在进行读取数据的时候,根据缓存机制在相应的缓存中查询,如果在缓存中找到了需要的数据(我们把这称做“缓存命 中"),则就直接把命中的数据作为结果加以利用,避免了大量发送SQL语句到数据库查询的性能损耗。
Hibernate缓存分类:
一、Session缓存(又称作事务缓存):Hibernate内置的,不能卸除。
缓存范围:缓存只能被当前Session对象访问。缓存的生命周期依赖于Session的生命周期,当Session被关闭后,缓存也就结束生命周期。
二、SessionFactory缓存(又称作应用缓存):使用第三方插件,可插拔。
缓存范围:缓存被应用范围内的所有session共享,不同的Session可以共享。这些session有可能是并发访问缓存,因此必须对缓存进行更新。缓存的生命周期依赖于应用的生命周期,应用结束时,缓存也就结束了生命周期,二级缓存存在于应用程序范围。
2.一级缓存
Hibenate中一级缓存,也叫做session的缓存,当调用session的save/saveOrUpdate/get/load/list/iterator方法的时候,都会把对象放入session的缓存中。
一级缓存可以在session范围内减少数据库的访问次数,只在session范围有效,session关闭,一级缓存失效。
session的缓存由hibernate维护, 用户不能操作缓存内容; 如果想操作缓存内容,必须通过hibernate提供的evit/clear方法操作。
特点:
只在当前session范围有效,作用时间短,效果不是特别明显!
在短时间内多次操作数据库,效果比较明显!
list和iterator的区别
list:
一次把所有的记录都查询出来
会放入缓存,但不会从缓存中获取数据
Iterator:
N+1查询; N表示所有的记录总数,即会先发送一条语句查询所有记录的主键(1),再根据每一个主键再去数据库查询(N)会放入缓存,也会从缓存中取数据
public void test5()throws Exception{
Session session = sf.openSession();
session.beginTransaction();
User user = new User();
user.setUserName("林黛玉");
session.save(user);
user.setUserName("嘉宝");
session.save(user);
session.getTransaction().commit();
session.close();
}
由于一级缓存的作用,user对象只会被保存一次。
数据从缓存中清除:
1. evit()将指定的持久化对象从缓存中清除,释放对象所占用的内存资源,指定对象从持久化状态变为脱管状态,从而成为游离对象。
2. clear()将缓存中的所有持久化对象清除,释放其占用的内存资源。
其他缓存操作:
1. contains()判断指定的对象是否存在于缓存中。
2. flush()刷新缓存区的内容,使之与数据库数据保持同步。
3.二级缓存
Hibernate提供了基于应用程序级别的缓存, 可以跨多个session,即不同的session都可以访问缓存数据。 这个缓存也叫二级缓存。
Hibernate提供的二级缓存有默认的实现,且是一种可插配的缓存框架!如果用户想用二级缓存,只需要在hibernate.cfg.xml中配置即可; 不想用,直接移除,不影响代码。如果用户觉得hibernate提供的框架框架不好用,自己可以换其他的缓存框架或自己实现缓存框架。
开启二级缓存:
list() 默认情况只会放入缓存,不会从一级缓存中取,配置查询缓存,可以让list()查询从二级缓存中取数据。
<!--开启二级缓存-->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!--指定使用的缓存框架-->
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<!--开启查询缓存-->
<property name="hibernate.cache.use_query_cache">true</property>
指定需要二级缓存的类:
如果设置了集合缓存,集合所属的元素对象也要放入二级缓存,即Employee。
<!--指定哪一些类需要加入二级缓存-->
<class-cache class="com.juaner.department.Employee" usage="read-only"/>
<class-cache class="com.juaner.department.Dept" usage="read-only"/>
<!--集合缓存,集合所属的类型也要放入二级缓存-->
<collection-cache collection="com.juaner.department.Dept.emps" usage="read-only"/>
使用二级缓存:
如果设置了查询缓存,需要手动设置setCacheable(true)。
@Test
public void test1(){
Session session = sf.openSession();
session.beginTransaction();
//setCacheable 指定从二级缓存中找,或放入二级缓存,针对list不从一级缓存中取数据的情况
//从缓存中读数据,查询条件必须一致
//缓存机制为Map<条件,结果>
Query query = session.createQuery("from Dept").setCacheable(true);
System.out.println(query.list());
session.getTransaction().commit();
session.close();
Session session1 = sf.openSession();
session1.beginTransaction();
query = session1.createQuery("from Dept").setCacheable(true);
System.out.println(query.list());
session1.getTransaction().commit();
session1.close();
}
4.get和load
get: 及时加载,只要调用get方法立刻向数据库查询
load:默认使用懒加载,当用到数据的时候才向数据库查询
5.懒加载
当用到数据的时候才向数据库查询,这就是hibernate的懒加载特性。
lazy 值
true 使用懒加载
false 关闭懒加载
extra 在集合数据懒加载时候提升效率,在真正使用数据的时候才向数据库发送查询的sql,如果调用集合的size()/isEmpty()方法,只是统计,不真正查询数据!
6.二级缓存使用实例
(1)打开二级缓存:
为Hibernate配置二级缓存:
在主配置文件中hibernate.cfg.xml :
<!-- 使用二级缓存 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!--设置缓存的类型,设置缓存的提供商-->
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
或者当hibernate与Spring整合后直接配到Spring配置文件applicationContext.xml中
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/lp/ecjtu/model/Employee.hbm.xml</value>
<value>com/lp/ecjtu/model/Department.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.OracleDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.generate_statistics=true
</value>
</property>
</bean>
(2)配置ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!--
缓存到硬盘的路径
-->
<diskStore path="d:/ehcache"></diskStore>
<!--
默认设置
maxElementsInMemory : 在內存中最大緩存的对象数量。
eternal : 缓存的对象是否永远不变。
timeToIdleSeconds :可以操作对象的时间。
timeToLiveSeconds :缓存中对象的生命周期,时间到后查询数据会从数据库中读取。
overflowToDisk :内存满了,是否要缓存到硬盘。
-->
<defaultCache maxElementsInMemory="200" eternal="false"
timeToIdleSeconds="50" timeToLiveSeconds="60" overflowToDisk="true"></defaultCache>
<!--
指定缓存的对象。
下面出现的的属性覆盖上面出现的,没出现的继承上面的。
-->
<cache name="com.suxiaolei.hibernate.pojos.Order" maxElementsInMemory="200" eternal="false"
timeToIdleSeconds="50" timeToLiveSeconds="60" overflowToDisk="true"></cache>
</ehcache>
(3)使用二级缓存需要在实体类中加入注解:
需要ehcache-1.2.3.jar包:
还需要 commons_loging1.1.1.jar包
在实体类中通过注解可以配置实用二级缓存:
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
Load默认使用二级缓存,就是当查一个对象的时候,它先会去二级缓存里面去找,如果找到了就不去数据库中查了。
Iterator默认的也会使用二级缓存,有的话就不去数据库里面查了,不发送select语句了。
List默认的往二级缓存中加数据,假如有一个query,把数据拿出来之后会放到二级缓存,但是执行查询的时候不会到二级缓存中查,会在数据库中查。原因每个query中查询条件不一样。
(4)也可以在需要被缓存的对象中hbm文件中的<class>标签下添加一个<cache>子标签:
<hibernate-mapping>
<class name="com.suxiaolei.hibernate.pojos.Order" table="orders">
<cache usage="read-only"/>
<id name="id" type="string">
<column name="id"></column>
<generator class="uuid"></generator>
</id>
<property name="orderNumber" column="orderNumber" type="string"></property>
<property name="cost" column="cost" type="integer"></property>
<many-to-one name="customer" class="com.suxiaolei.hibernate.pojos.Customer"
column="customer_id" cascade="save-update">
</many-to-one>
</class>
</hibernate-mapping>
存在一对多的关系,想要在在获取一方的时候将关联的多方缓存起来,需要再集合属性下添加<cache>子标签,这里需要将关联的对象的 hbm文件中必须在存在<class>标签下也添加<cache>标签,不然Hibernate只会缓存OID。
<hibernate-mapping>
<class name="com.suxiaolei.hibernate.pojos.Customer" table="customer">
<!-- 主键设置 -->
<id name="id" type="string">
<column name="id"></column>
<generator class="uuid"></generator>
</id>
<!-- 属性设置 -->
<property name="username" column="username" type="string"></property>
<property name="balance" column="balance" type="integer"></property>
<set name="orders" inverse="true" cascade="all" lazy="false" fetch="join">
<cache usage="read-only"/>
<key column="customer_id" ></key>
<one-to-many class="com.suxiaolei.hibernate.pojos.Order"/>
</set>
</class>
</hibernate-mapping>