zoukankan      html  css  js  c++  java
  • hibernate中的二级缓存

    二级缓存使用场景:不经常修改的数据,但是经常的访问的数据会放到缓存中去

    一级缓存仅仅是session内部的缓存,用来存取sql语句,比如说连续调用两次相同参数的get方法,就是session缓存

    二级缓存是sessiionFactory层面的缓存,即不同线程,不同程序之间的缓存

    1.在hibernate.hbm.xml中加载配置

    <property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>

    <!-- 配置缓存项 可以在hibernate.hbm.xml下配置 也可以在各自类下的映射文件下配 -->
    <class-cache usage="read-write" class="cache.Employee"/>
    <class-cache usage="read-write" class="cache.Department"/>

    类缓存

    public void testCahce()
    {
    Session session=sessionFactory.openSession();

    Transaction tx=session.beginTransaction();
    Employee employee=(Employee) session.get(Employee.class, 1);
    // System.out.println(employee);

    Session session2=sessionFactory.openSession();
    Employee employee1=(Employee) session.get(Employee.class,1);

    //sessionFactory 二级缓存 当两个session用get查询相同的数据就会调用使用


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

    2.集合缓存

    <collection-cache usage="read-write" collection="cache.Department.employees"/> 

    /**
    * 二级缓存中的集合缓存
    */
    @Test
    public void testCahce2()
    {
    Session session=sessionFactory.openSession();

    Transaction tx=session.beginTransaction();
    Department department=(Department) session.get(Department.class, 1);
    System.out.println(department.getEmployees());//懒加载
    // System.out.println(employee);
    Session session2=sessionFactory.openSession();
    department=(Department) session2.get(Department.class, 1);
    System.out.println(department.getEmployees()); //由于集合为设置二级缓存 所以相同数据也会加载两次
    //sessionFactory 二级缓存 当两个session用get查询相同的数据就会调用使用

    tx.commit();
    session.close();

    不是用集合缓存


    }

    使用集合缓存

    3.查询语句缓存(只针对相同sql语句的缓存)

    (1)iterator实现

    /**
    * 查询缓存 n+1次查询 按一模一样的查询语句来缓存
    */
    @Test
    public void testQueryCache()
    {
    Session session=sessionFactory.openSession();

    Transaction tx=session.beginTransaction();
    Iterator<Employee> iterator=session.createQuery("from Employee where id<5").iterate();
    while(iterator.hasNext())
    {
    System.out.println(iterator.next());
    }

    Iterator<Employee> iterator1=session.createQuery("from Employee where id<5").iterate();
    while(iterator1.hasNext())
    {
    System.out.println(iterator1.next());
    }
    tx.commit();
    session.close();
    }

    (2)使用setCacheable实现

    需加载额外配置

    <property name="cache.use_query_cache">true</property>

    @Test
    public void testQueryCachetrue()
    {
    Session session=sessionFactory.openSession();

    Transaction tx=session.beginTransaction();
    List list=session.createQuery("from Employee where id<5").setCacheable(true).list();
    System.out.println(list);
    tx.commit();
    session.close();
    Session session1=sessionFactory.openSession();
    Transaction tx1=session1.beginTransaction();
    List list1=session1.createQuery("from Employee where id<5").setCacheable(true).list();
    System.out.println(list1);
    tx1.commit();
    session1.close();

    }

    但是sql语句有一点不一样 ,都不能使用缓存

     

  • 相关阅读:
    生日小助手源码运行的步骤
    关于生日小助手跨平台兼容性的临时解决方案
    生日小助手V3.0——跨平台的农历生日提醒软件
    生日小助手V3.1——跨平台多语言的农历生日提醒软件
    有关生日小助手的内容,请浏览生日小助手官方网站……
    生日小助手的详细规划——本博文随时更新,持续有效
    生日小助手V2.0发布了——可以正式投入使用!
    前端开发入门的几本推荐书籍
    多想一想,JS中函数声明和函数表达式的区别
    table固定宽度大小
  • 原文地址:https://www.cnblogs.com/lt123/p/7241812.html
Copyright © 2011-2022 走看看