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



    
        
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />

   

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

package="cn.sxt.pojo">
    <class name="Book" table="t_book">
        
        
            class="native">
        
        
        
        
        
        
        
    class>

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


        true
        org.hibernate.cache.ehcache.EhCacheRegionFactory
        org.hibernate.cache.ehcache.EhCacheRegionFactory

  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中添加通用配置


        true

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

@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);
    }

相关