hibernate缓存分为两级
第一级别为Session级别的缓存,由hibernate进行管理
第二级别为SessionFactory级别,可分为两类
内置缓存:Hibernate自带的。在Hibernate的初始化阶段,hibernate会把映射元数据和预定义的SQL放到SessionFactory缓存中,该内置缓存是只读的
外置缓存(二级缓存):一个可配置的缓存插件。默认情况下SessionFactory不会启用这个缓存插件
二级缓存适用场景:
适合放入二级缓存的数据
1.很少被修改
2.不是很重要的数据,允许偶尔出现并发的问题
不适合放入二级缓存的数据
1.经常被修改的
2.财务数据,绝对不允许出现并发问题
3.与其他应用程序共享的数据
hibernate支持的二级缓存插件
EHCache
OpenSymphonyOSCache
SwarmCache
JBOSSCache
配置二级缓存的步骤(EHCache)
1.加入二级缓存插件的jar包及配置文件
(1)复制hibernate-release-4.2.4.Final\lib\optional\ehcache\*.jar到当前hibernate应用的lib路径下,Add to Build Path(2)复制hibernate-release-4.2.4.Final\project\etc\ehcache.xml到当前应用的src路径下
2.配置 hibernate,cfg.xml
2.1 配置hibernate的二级缓存<property name="cache.use_second_level_cache">true</property>
2.2 配置hibernate二级缓存使用的产品
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
2.3 配置对哪些类使用hibernate的二级缓存
2.3.1类级别的二级缓存
<class-cache usage="read-write" class="com.atguigu.hibernate.entities.Employee"/>
也可以在.hbm.xml 文件中配置对哪些类使用二级缓存,及二级缓存的策略
(例:在Employee.hbm.xml中增加 <cache usage="read-write">)
2.3.2 集合级别的二级缓存
(1)配置对集合使用二级缓存
<collection-cache usage="read-write" collection="com.atguigu.hibernate.entities.Department.emps"/>
也可以在.hbm.xml中进行配置
<set name="emps" table="GG_EMPLOYEE" inverse="true" lazy="true">
<cache usage="read-write"/>
<key>
<column name="DEPT_ID" />
</key>
<one-to-many class="com.atguigu.hibernate.entities.Employee" />
</set>
(2)还需要配置集合中的元素对应的持久化类也使用二级缓存,否则将会多出n条SQL语句
3.ehcache.xml配置说明
3.1 配置硬盘缓存的目录
<!--
指定一个目录:当 EHCache 把数据写到硬盘上时, 将把数据写到这个目录下.
-->
<diskStore path="d:\\tempDirectory"/>
3.2设置缓存的默认过期策略
(每个属性的含义写在3.3中)
<!--
设置缓存的默认数据过期策略
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
3.3设定具体的命名缓存的数据过期策略
缓存区域(region):一个具有名称的缓存块,可以给每一个缓存块设置不同的缓存策略。
如果没有设置任何的缓存区域,则所有被缓存的对象,都将使用默认的缓存策略。即:<defaultCache.../>
Hibernate 在不同的缓存区域保存不同的类/集合。
对于类而言,区域的名称是类名。如:com.atguigu.domain.Customer
对于集合而言,区域的名称是类名加属性名。如com.atguigu.domain.Customer.orders
示例:
<!--
name: 设置缓存的名字,它的取值为类的全限定名或类的集合的名字
maxElementsInMemory: 设置基于内存的缓存中可存放的对象最大数目
eternal: 设置对象是否为永久的, true表示永不过期,
此时将忽略timeToIdleSeconds 和 timeToLiveSeconds属性; 默认值是false
timeToIdleSeconds:设置对象空闲最长时间,以秒为单位, 超过这个时间,对象过期。
当对象过期时,EHCache会把它从缓存中清除。如果此值为0,表示对象可以无限期地处于空闲状态。
timeToLiveSeconds:设置对象生存最长时间,超过这个时间,对象过期。
如果此值为0,表示对象可以无限期地存在于缓存中. 该属性值必须大于或等于 timeToIdleSeconds 属性值
overflowToDisk:设置基于内存的缓存中的对象数目达到上限后,是否把溢出的对象写到基于硬盘的缓存中
-->
<cache name="com.atguigu.hibernate.entities.Employee"
maxElementsInMemory="1"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
<cache name="com.atguigu.hibernate.entities.Department.emps"
maxElementsInMemory="1000"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/>
3.4 配置查询缓存
默认情况下,设置的缓存对 HQL,QBC 查询是无效的,但可以通过以下方式使其有效
(1)hibernate.cfg.xml中声明开启查询缓存
<!-- 配置启用查询缓存 -->
<property name="cache.use_query_cache">true</property>
(2)java中调用Query或Criteria 的setCacheable(true)方法
Query query = session.createQuery("FROM Employee");
query.setCacheable(true);
注:查询缓存依赖于二级缓存
查询缓存是只存储对象的id的,并不去存储真正的对象。需查询的类设置了二级缓存,查询过后,就能把id,查询出来的对象都存入二级缓存