Hibernate4之二级缓存(十六)

一、配置 Hibernate二级缓存
1. 缓存的概念:
缓存是介于物理数据源与应用程序之间, 是对数据庫中的数据复制一份l临时放在内存或者硬盘中的容器,其作用是为了减少应用程序对物理数据源访问的次数,从而提高了应用程序的运行性能, Hibemate在进行读取数据的时候,根据缓存机制在相应的缓存中査询,如果在缓存中找到了需要的数据(我们把这称做“缓存命中·),则就直接把命中的数据作为结果加以利用,避免了大量发送 sQL语句到数据库査询的性能损耗。
2. Hibemate缓存的分类
session缓存(又称作事务缓存) Hibemate内置的,不能卸除,,缓存范围:缓存只能被当前 session对象访同,缓存的生命周期依赖于 session的生命周期,当 session被关闭后,缓存也就结東生命周期。
sessionFactory缓存(又称作应用缓存):使用第三方插件,可插拔。缓存范围:所以session共享,缓存的生命周期依赖应用的生命周期,应用结束时,缓存结束生命周期,二级缓存存在于应用范围。二级缓存指sessionFactory的外置缓存,内置缓存不可修改,是请求的元数据和预编译的sql语句。
3. 二级缓存策略提供商:
提供了HashTable缓存, EHCache, 〇SCache, SwamCache, jBossCa11ie2,这些缓存机制,其中EHCache, OSCache是不能用子集群1t11境(ClusterSafe)的,而 SwarmCache, jBossCathe2是可以的, HashTable缓存主要是用来测试的,只能把对象放在内存中, EHcache, Oscache可以把对象放在内存(memory)中,也可以把对象放在硬盘(disk)上。
二、配置过程
1.导入源码项目中可扩展目录下的ehcache的jar。
这里写图片描述
2.在classpath目录下,添加ehcache.xml配置文件。

<ehcache>

    <!-- 指定一个文件目录,当EHCache把数据写到硬盘上时,将把数据写到这个目录下 -->
    <diskStore path="c:\\ehcache"/>

    <!--  
        设置缓存的默认数据过期策略 
    -->    
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />

    <!-- 
        name 设置缓存的名字,他的取值为类的完整名字或者类的集合的名字;
        maxElementsInMemory 设置基于内存的缓存可存放的对象的最大数目
        eternal 如果为true,表示对象永远不会过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds,默认为false;
        timeToIdleSeconds 设定允许对象处于空闲状态的最长时间,以秒为单位;
        timeToLiveSeconds 设定对象允许存在于缓存中的最长时间,以秒为单位;
        overflowToDisk 如果为true,表示当基于内存的缓存中的对象数目达到maxElementsInMemory界限,会把溢出的对象写到基于硬盘的缓存中;
     -->


    <!-- 设定具体的第二级缓存的数据过期策略 -->
    <cache name="com.newbeedaly.model.Class"
        maxElementsInMemory="1"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"
        />


</ehcache>

3.在hibernate.cfg.xml中启用cache二级缓存。

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!--数据库连接设置 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>


        <!-- 方言 -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!-- 控制台显示SQL -->
        <property name="show_sql">true</property>

        <!-- 自动更新表结构 -->
        <property name="hbm2ddl.auto">update</property>

         <!-- 启用二级缓存 -->
        <property name="cache.use_second_level_cache">true</property>

        <!-- 配置使用的二级缓存的产品 -->
        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

        <!-- 配置启用查询缓存 -->
        <property name="cache.use_query_cache">true</property>
        <!--指定二级缓存配置文件的位置-->  
        <!-- 
<property name="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</property>  
        -->
        <mapping resource="com/newbeedaly/model/Student.hbm.xml"/>

        <mapping resource="com/newbeedaly/model/Class.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

4.在映射文件中设置缓存策略

<class name="Class" table="t_class">
        <!-- 配置缓存策略 -->
        <cache usage="read-only"/>
        <id name="id" column="classId">
            <generator class="native"></generator>
        </id>

        <property name="name" column="className"></property>
    </class>

5.配置Test类测试

@Test
    public void testCache1() {  // 一级缓存
        Session session=sessionFactory.openSession();
        session.beginTransaction();

        Class c=(Class)session.get(Class.class, Long.valueOf(1)); // sql
        System.out.println(c.getName());
        Class c2=(Class)session.get(Class.class, Long.valueOf(1)); // 从一级缓存取对象
        System.out.println(c2.getName()); 
        System.out.println(c==c2);  // true

        session.getTransaction().commit();
        session.close();
    }

    @Test
    public void testCache2(){  // 二级缓存
        Session session1=sessionFactory.openSession();
        session1.beginTransaction();

        Class c=(Class)session1.get(Class.class, Long.valueOf(1)); //sql
        System.out.println(c.getName());

        session1.getTransaction().commit();
        session1.close();

        Session session2=sessionFactory.openSession();
        session2.beginTransaction();

        Class c2=(Class)session2.get(Class.class, Long.valueOf(1));// 从二级缓存取对象
        System.out.println(c2.getName());

        session2.getTransaction().commit();
        session2.close();

        System.out.println(c==c2);  // false
    }

源码下载地址:hibernate4二级缓存源码

猜你喜欢

转载自blog.csdn.net/willdic/article/details/80523945