java之hibernate之hibernate缓存

这篇主要讲 hibernate缓存

1.缓存的作用是为了提高效率

2.Hibernate的开发效率比较高,但是执行效率相对较低。

3.Hibernate提供了缓存来提高效率。hibernate缓存分为:一级缓存,二级缓存,查询缓存。

4.一级缓存又称为 session缓存,是线程级别的缓存。

get 和 load 方法查询数据 首先检查session缓存中是否有该数据,如果有,从缓存中直接获取数据,如果没有则查询数据库,并且写入缓存。

@Test
    public void testGet(){
        Session session = HibernateUtil.getSession();
        Book b=(Book)session.get(Book.class, 1);
        System.out.println(b.getName()+"---"+b.getAuthor());
        b=(Book)session.get(Book.class, 1);
        System.out.println(b.getName()+"---"+b.getAuthor());
    }
@Test
    public void testLoad(){
        Session session = HibernateUtil.getSession();
        Book b=(Book)session.load(Book.class, 1);
        System.out.println(b.getName()+"---"+b.getAuthor());
        b=(Book)session.load(Book.class, 1);
        System.out.println(b.getName()+"---"+b.getAuthor());
    }

在这两个方法中,第一次会执行sql语句查询,第二次则不会生成sql语句查询,直接从缓存中取数据了。

list方法查询数据,不会查看session缓存,直接从数据库中获取。list 查询数据后,会将数据写入 Session缓存。

@Test
    public void testIterate(){
        Session session = HibernateUtil.getSession();
        Iterator<Book> iter = session.createQuery("from Book").iterate();
        while(iter.hasNext()){
            Book b = iter.next();
            System.out.println(b.getName()+"---"+b.getAuthor());
        }
        System.out.println("==============================");
        iter = session.createQuery("from Book").iterate();
        while(iter.hasNext()){
            Book b = iter.next();
            System.out.println(b.getName()+"---"+b.getAuthor());
        }
    }

Session的管理:clear 清空缓存中数据,close 关闭, evict 清除指定对象

5.二级缓存又称为 SessionFactory缓存,是进程级别的缓存。声明周期很长。一般有缓存数据清理算法来清除缓存中的数据。

LRU ---最近最少使用,FIFO 、LFU 、LRU ; 

6.二级缓存的实现步骤

  a). 导入 jar 包   --lib\optional\ehcache 下的所有包

    ehcache-core-2.4.3.jar

    hibernate-ehcache-4.3.10.Final.jar

    slf4j-api-1.6.1.jar

  b). 导入ehcache.xml 文件  project\etc 下的ehcache.xml 放入 src 下

<ehcache>

    <diskStore path="java.io.tmpdir"/>
        <!-- maxElementsInMemory 最大存放元素个数 
             eternal 是否永久存储
             timeToIdleSeconds 空闲秒数
             timeToLiveSeconds 存活时间数
             overflowToDisk 溢出是否写入磁盘
        -->
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />

   
</ehcache>

  c). 在映射文件 添加 cache 标签,指明使用二级缓存的方式

<hibernate-mapping package="cn.sxt.pojo">
    <class name="Book" table="t_book">
        <cache usage="read-only"/>
        <id name="id">
            <generator class="native"></generator>
        </id>
        <property name="name"/>
        <property name="author"/>
        <property name="price"/>
        <property name="pubDate"/>
        <!-- 多对一的关联关系设置   column指定外键的名称 -->
        <many-to-one name="category" column="cid" fetch="join"/>
    </class>
</hibernate-mapping>

  d). 在 hibernate.cfg.xml 的通用配置中,开启二级缓存和 3.x 不一致

<!-- 开启二级缓存 -->
        <property name="cache.use_second_level_cache">true</property>
        <property name="cache.provider_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
        <property name="cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

  e)测试 如果session被关闭后,数据只查询一次,那么二级缓存开启成功

@Test
    public void testGet(){
        Session session = HibernateUtil.getSession();
        Book b=(Book)session.get(Book.class, 1);
        System.out.println(b.getName()+"---"+b.getAuthor());
        System.out.println("=========================");
        HibernateUtil.close();
        session = HibernateUtil.getSession();
        b=(Book)session.get(Book.class, 1);
        System.out.println(b.getName()+"---"+b.getAuthor());
    }

7. 查询缓存,查询缓存是在二级缓存的基础上的。也就是首先要先开启二级缓存,查询缓存的配置,在hibernate.cfg.xml中添加通用配置

 

<!-- 开启查询缓存 -->
        <property name="cache.use_query_cache">true</property>

 

在查询时需要指明使用查询缓存: 以下代码如果只查询一次,那么查询缓存设置成功

 

@Test
    public void testList(){
        Session session = HibernateUtil.getSession();
        session.createQuery("from Book")
                .setCacheable(true)//设置使用查询缓存
                .list();
        System.out.println("=================");
        session.createQuery("from Book")
                .setCacheable(true)//设置使用查询缓存
                .list();
        System.out.println("====================");
        session.load(Book.class, 1);
    }
posted @ 2019-07-20 09:15  Vincent-yuan  阅读(1180)  评论(0编辑  收藏  举报