您的位置:首页 > 其它

Hibernate+EHCache配置二级缓存

2017-11-08 15:49 591 查看
 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

<class name="cn.itcast.domain.Customer" table="customers" catalog="hibernate3day4" >  

     <!-- 类级别缓存 -->  

      <cache usage="read-write"/>  

        <set name="orders" cascade="all-delete-orphan" inverse="true" >  

          <!-- 关联集合级别缓存 -->  

         <cache usage="read-write"/>     

      </set>  

</class> 

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

[html] view
plain copy

<span style="font-family:SimSun;font-size:18px;"><!-- 类级别缓存 -->  

            <class-cache usage="read-write" class="cn.itcast.domain.Customer"/>  

            <class-cache usage="read-write" class="cn.itcast.domain.Order"/>  

            <!-- 集合缓存 -->  

            <collection-cache usage="read-write" collection="cn.itcast.domain.Customer.orders"/>  

</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

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">  

  

    <diskStore path="java.io.tmpdir"/> 配置二级缓存硬盘临时目录位置   

         <defaultCache    

            maxElementsInMemory="10000" // 内存中最大对象数量 ,超过数量,数据会被缓存到硬盘   

            eternal="false"  // 是否缓存为永久性 false 不永久 

            timeToIdleSeconds="120"  //
对象空闲状态时间

            timeToLiveSeconds="120" // 存活时间,对象不管是否使用,到了时间回收  

            overflowToDisk="true" // 是否可以缓存到硬盘  

            maxElementsOnDisk="10000000" // 硬盘缓存最大对象数量   

            // 当jvm结束时是否持久化对象 true false 默认是false  

            diskExpiryThreadIntervalSeconds="120"  // 指定专门用于清除过期对象的监听线程的轮询时间   

            memoryStoreEvictionPolicy="LRU"   

  

            />  

</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

package com.lanhuigu.hibernate.test;  

  

import org.hibernate.Session;  

import org.hibernate.SessionFactory;  

import org.hibernate.Transaction;  

import org.hibernate.cfg.Configuration;  

  

import com.lanhuigu.hibernate.entity.Customer;  

  

public class TestHibernate {  

    public static void main(String[] args) {  

        Configuration configuration = new Configuration().configure();  

        SessionFactory sessionFactory = configuration.buildSessionFactory();  

        Session session1 = sessionFactory.openSession();  

        Transaction tr1 = session1.beginTransaction();  

        //加载一个OID为1的对象  

        Customer customer1 = (Customer) session1.get(Customer.class, new Long(1));  

        System.out.println("session1"+customer1.getName());  

        tr1.commit();  

        session1.close();  

          

        //加载同样OID为2的对象  

        Session session2 = sessionFactory.openSession();  

        Transaction tr2 = session2.beginTransaction();  

        Customer customer2 = (Customer) session2.get(Customer.class, new Long(1));  

        System.out.println("session2"+customer2.getName());  

        tr2.commit();  

        session2.close();  

    }  

}  

控制台输出:

[java] view
plain copy

Hibernate:   

    select  

        customer0_.ID as ID1_0_0_,  

        customer0_.NAME as NAME2_0_0_,  

        customer0_.EMAIL as EMAIL3_0_0_,  

        customer0_.PASSWORD as PASSWORD4_0_0_,  

        customer0_.PHONE as PHONE5_0_0_,  

        customer0_.ADDRESS as ADDRESS6_0_0_,  

        customer0_.SEX as SEX7_0_0_,  

        customer0_.IS_MARRIED as IS8_0_0_,  

        customer0_.DESCRIPTION as DESCRIPT9_0_0_,  

        customer0_.IMAGE as IMAGE10_0_0_,  

        customer0_.BIRTHDAY as BIRTHDA11_0_0_,  

        customer0_.REGISTERED_TIME as REGISTE12_0_0_,  

        customer0_.HOME_PROVINCE as HOME13_0_0_,  

        customer0_.HOME_CITY as HOME14_0_0_,  

        customer0_.HOME_STREET as HOME15_0_0_,  

        customer0_.HOME_ZIPCODE as HOME16_0_0_,  

        customer0_.COMP_PROVINCE as COMP17_0_0_,  

        customer0_.COMP_CITY as COMP18_0_0_,  

        customer0_.COMP_STREET as COMP19_0_0_,  

        customer0_.COMP_ZIPCODE as COMP20_0_0_   

    from  

        CUSTOMERS customer0_   

    where  

        customer0_.ID=?  

session1test  

session2test  

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

转载地址:http://blog.csdn.net/luckyzhoustar/article/details/47748179
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息