Hibernate+EHCache配置二级缓存

来源:互联网 发布:查看守望先锋数据 编辑:程序博客网 时间:2024/05/16 15:59

 1.前言

Hibernate中的二级缓存,二级缓存是属于SessionFactory级别的缓存机制。第一级别的缓存是Session级别的缓存,是属于事务范围的缓存,由Hibernate管理,一般无需进行干预。第二级别的缓存是SessionFactory级别的缓存,是属于进程范围的缓存。


 2.Hibernate二级缓存

1.分类

二级缓存也分为了两种

内置缓存:Hibernate自带的,不可卸载,通常在Hibernate的初始化阶段,Hibernate会把映射元数据和预定义的SQL语句放置到SessionFactory的缓存中。该内置缓存是只读的。

外置缓存:通常说的二级缓存也就是外置缓存,在默认情况下SessionFactory不会启用这个缓存插件,外置缓存中的数据是数据库数据的复制,外置缓存的物理介质可以是内存或者硬盘。

hibernate二级缓存的结构





2.并发访问策略

transactional

(事务型)

仅在受管理的环境中适用

提供Repeatable Read事务隔离级别

适用经常被读,很少修改的数据

可以防止脏读和不可重复读的并发问题

缓存支持事务,发生异常的时候,缓存也能够回滚

read-write

(读写型)

提供Read Committed事务隔离级别

在非集群的环境中适用

适用经常被读,很少修改的数据

可以防止脏读

更新缓存的时候会锁定缓存中的数据

nonstrict-read-write

(非严格读写型)

适用极少被修改,偶尔允许脏读的数据(两个事务同时修改数据的情况很少见)

不保证缓存和数据库中数据的一致性

为缓存数据设置很短的过期时间,从而尽量避免脏读

不锁定缓存中的数据

read-only

(只读型)

适用从来不会被修改的数据(如参考数据)

在此模式下,如果对数据进行更新操作,会有异常

事务隔离级别低,并发性能高

在集群环境中也能完美运作


 3.二级缓存的配置

1.hibernate支持的缓存插件

EHCache: 可作为进程范围内的缓存,存放数据的物理介质可以是内存或硬盘,对Hibernate的查询缓存提供了支持

OpenSymphony`:可作为进程范围内的缓存,存放数据的物理介质可以是内存或硬盘,提供了丰富的缓存数据过期策略,对Hibernate的查询缓存提供了支持

SwarmCache:可作为集群范围内的缓存,但不支持Hibernate的查询缓存

JBossCache:可作为集群范围内的缓存,支持Hibernate的查询缓存


四种缓存插件支持的并发范围策略如下图




2.二级缓存配置

下面以ehcache缓存为例,来讲一下二级缓存的配置

2.1 拷贝jar包

如要第三方的jar包ehcache-1.5.0.jar,并且依赖于

依赖backport-util-concurrent 和 commons-logging

2.2 在hibernate.cfg.xml中开启二级缓存

<propertyname="hibernate.cache.use_second_level_cache">true</property>

2.3 配置二级缓存技术提供商

<propertyname="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

2.4 配置缓存数据对象并发策略

方式一 在hbm文件中配置

[html] view plai copy
  1. <class name="cn.itcast.domain.Customer" table="customers" catalog="hibernate3day4" >  
  2.      <!-- 类级别缓存 -->  
  3.       <cache usage="read-write"/>  
  4.         <set name="orders" cascade="all-delete-orphan" inverse="true" >  
  5.           <!-- 关联集合级别缓存 -->  
  6.          <cache usage="read-write"/>     
  7.       </set>  
  8. </class> 


方式二 在cfg文件配置(集中配置)


[html] view plain copy
  1. <span style="font-family:SimSun;font-size:18px;"><!-- 类级别缓存 -->  
  2.             <class-cache usage="read-write" class="cn.itcast.domain.Customer"/>  
  3.             <class-cache usage="read-write" class="cn.itcast.domain.Order"/>  
  4.             <!-- 集合缓存 -->  
  5.             <collection-cache usage="read-write" collection="cn.itcast.domain.Customer.orders"/>  
  6. </span>  


方式三 在实体类上,使用hibernatede的@Cache注解

@Entity@Table(name = "S_CONFIG")@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)  //缓存策略:NONSTRICT_READ_WRITE:只偶尔需要更新数据,不需要严格的事物隔离public class SConfig {

2.5 添加二级缓存配置文件

在src中配置ehcache.xml,将ehcache.jar包中的ehcache-failsafe.xml 改名 ehcache.xml 放入 src 

[html] view plain copy
  1. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">  
  2.   
  3.     <diskStore path="java.io.tmpdir"/> 配置二级缓存硬盘临时目录位置   
  4.          <defaultCache    
  5.             maxElementsInMemory="10000" // 内存中最大对象数量 ,超过数量,数据会被缓存到硬盘   
  6.             eternal="false"  // 是否缓存为永久性 false 不永久 
  7.             timeToIdleSeconds="120"  // 对象空闲状态时间
  8.             timeToLiveSeconds="120" // 存活时间,对象不管是否使用,到了时间回收  
  9.             overflowToDisk="true" // 是否可以缓存到硬盘  
  10.             maxElementsOnDisk="10000000" // 硬盘缓存最大对象数量   
  11.             // 当jvm结束时是否持久化对象 true false 默认是false  
  12.             diskExpiryThreadIntervalSeconds="120"  // 指定专门用于清除过期对象的监听线程的轮询时间   
  13.             memoryStoreEvictionPolicy="LRU"   
  14.   
  15.             />  
  16. </ehcache>  
cache参数详解: 
● name:指定区域名 
● maxElementsInMemory :缓存在内存中的最大数目 
● maxElementsOnDisk:缓存在磁盘上的最大数目 
● eternal :设置是否永远不过期 
● overflowToDisk : 硬盘溢出数目 
● timeToIdleSeconds :对象处于空闲状态的最多秒数后销毁 
● timeToLiveSeconds :对象处于缓存状态的最多秒数后销毁 
● memoryStoreEvictionPolicy:缓存算法,有LRU(默认)、LFU、LFU 

关于缓存算法,常见有三种: 
● LRU:(Least Rencently Used)新来的对象替换掉使用时间算最近很少使用的对象 
● LFU:(Least Frequently Used)替换掉按命中率高低算比较低的对象 

● LFU:(First In First Out)把最早进入二级缓存的对象替换掉 


扩展: timeToIdleSeconds  vs  timeToLiveSeconds

If you set both, the expirationTime will be Math.min(ttlExpiry, ttiExpiry), where

ttlExpiry = creationTime + timeToLive  //创建时间 + 存活时间ttiExpiry = mostRecentTime + timeToIdle  //最近一次访问时间 + 空闲时间 


 4.Demo测试二级缓存

[java] view plain copy
  1. package com.lanhuigu.hibernate.test;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.Transaction;  
  6. import org.hibernate.cfg.Configuration;  
  7.   
  8. import com.lanhuigu.hibernate.entity.Customer;  
  9.   
  10. public class TestHibernate {  
  11.     public static void main(String[] args) {  
  12.         Configuration configuration = new Configuration().configure();  
  13.         SessionFactory sessionFactory = configuration.buildSessionFactory();  
  14.         Session session1 = sessionFactory.openSession();  
  15.         Transaction tr1 = session1.beginTransaction();  
  16.         //加载一个OID为1的对象  
  17.         Customer customer1 = (Customer) session1.get(Customer.classnew Long(1));  
  18.         System.out.println("session1"+customer1.getName());  
  19.         tr1.commit();  
  20.         session1.close();  
  21.           
  22.         //加载同样OID为2的对象  
  23.         Session session2 = sessionFactory.openSession();  
  24.         Transaction tr2 = session2.beginTransaction();  
  25.         Customer customer2 = (Customer) session2.get(Customer.classnew Long(1));  
  26.         System.out.println("session2"+customer2.getName());  
  27.         tr2.commit();  
  28.         session2.close();  
  29.     }  
  30. }  
控制台输出:

[java] view plain copy
  1. Hibernate:   
  2.     select  
  3.         customer0_.ID as ID1_0_0_,  
  4.         customer0_.NAME as NAME2_0_0_,  
  5.         customer0_.EMAIL as EMAIL3_0_0_,  
  6.         customer0_.PASSWORD as PASSWORD4_0_0_,  
  7.         customer0_.PHONE as PHONE5_0_0_,  
  8.         customer0_.ADDRESS as ADDRESS6_0_0_,  
  9.         customer0_.SEX as SEX7_0_0_,  
  10.         customer0_.IS_MARRIED as IS8_0_0_,  
  11.         customer0_.DESCRIPTION as DESCRIPT9_0_0_,  
  12.         customer0_.IMAGE as IMAGE10_0_0_,  
  13.         customer0_.BIRTHDAY as BIRTHDA11_0_0_,  
  14.         customer0_.REGISTERED_TIME as REGISTE12_0_0_,  
  15.         customer0_.HOME_PROVINCE as HOME13_0_0_,  
  16.         customer0_.HOME_CITY as HOME14_0_0_,  
  17.         customer0_.HOME_STREET as HOME15_0_0_,  
  18.         customer0_.HOME_ZIPCODE as HOME16_0_0_,  
  19.         customer0_.COMP_PROVINCE as COMP17_0_0_,  
  20.         customer0_.COMP_CITY as COMP18_0_0_,  
  21.         customer0_.COMP_STREET as COMP19_0_0_,  
  22.         customer0_.COMP_ZIPCODE as COMP20_0_0_   
  23.     from  
  24.         CUSTOMERS customer0_   
  25.     where  
  26.         customer0_.ID=?  
  27. session1test  
  28. session2test  

查询了两次,只打印了一次sql,说明二级缓存起了作用。


转载地址:http://blog.csdn.net/luckyzhoustar/article/details/47748179

原创粉丝点击