星空网 > 软件开发 > Java

初识Hibernate 缓存

    生活就像一杯咖啡,让你我慢慢的品尝,品尝它的苦涩和甘甜......

一、什么是Hibernate缓存

   解析:白话来说就是缓存数据的容器

      官方标准点缓存:是计算机领域的概念,它介于应用程序和永久性数据存储源之间。

      作用:降低应用程序直接读写数据库的频率,从而提高程序的运行性能。缓存中的数据是数据存储源中数据的拷贝。缓存的物理介质通常是内存

二、缓存一般分为三个类

  一级缓存

  二级缓存

  查看缓存

三、一级缓存

场景一:使用同一个session连续查询两次同一个对象

/查询学生信息  public static void select(){        //由班级查询该班级学生信息    Session session=HibernateUtil.currentSession();    Grade grade=(Grade) session.get(Grade.class, 14);    //输出班级信息    System.out.println(grade.getGname());    Grade grade2=(Grade) session.get(Grade.class, 14);    //输出班级信息    System.out.println(grade2.getGname());  }

执行结果:

初识Hibernate 缓存

场景一:在第一次查询完毕后,关闭session对象,重新开启一个session然后继续查询同一个对象

//查询学生信息  public static void select(){        //由班级查询该班级学生信息    Session session=HibernateUtil.currentSession();    Grade grade=(Grade) session.get(Grade.class, 14);    //输出班级信息    System.out.println(grade.getGname());    //关闭session    HibernateUtil.closeSession();    //重新获取session    session=HibernateUtil.currentSession();    Grade grade2=(Grade) session.get(Grade.class, 14);    //输出班级信息    System.out.println(grade2.getGname());  }

执行结果:

 初识Hibernate 缓存

解析:

   1:当我没有关闭session时用的同一个session两次访问同一个对象时,只会向DB端发送一条sql语句
    * 原因:因为我第一次访问数据库的时候Hibernate会自动的将我查询出来的结果保留一份查询出来的对象到一级缓存
          并且这个额对象是根据OID唯一标识的,也可以理解为数据库中的主键值,然后当我再一次访问一个对象时,Hibernate
        机制会自动的先去一级缓存中查找看有没有OID与我要查询的OID相同的对象,如果有的话,则直接从一级缓存中 拿数据
          如果没有相同的OID则说明缓存中没有我要的记录,那么就会直接去访问DB端了,这样的话,又会重新发送一条sql
    2:当我第一次查询完数据后立即关闭session,这时重新开启一个session来访问同一个对象,这时我们会发现它居然向数据库发送了两条Sql语句。这是为什么呢?
    * 原因:其实原因很简单,因为我们虽然说是访问的同一个对象,但是我们随即就关闭了这个session而重新开启了一个session,

        此时我们访问时的session是不一致的也就是说是两个不同的session发出的请求,这样理解的话,我们就不难理解了。

          所以总结出,一级缓存是一个会话级别的缓存,当一次回话结束后该会话里的缓存则会全部的销毁,所有我们自然就只能重新发送一条sql啦。

3补充以下方法支持一级缓存

初识Hibernate 缓存

 

 

 四、二级缓存

(一)配置二级缓存 

  1.引入如下jar包。

        ehcache-1.2.3.jar  核心库

        backport-util-concurrent.jar

        commons-logging.jar

  2.配置Hibernate.cfg.

       <property name="hibernate.cache.use_second_level_cache">true</property>

 

 

 

     3.配置二级缓存的供应商

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

 

 

 

  4.指定使用二级缓存的类

    方案一:在*.hbm.

         在<class元素的子元素下添加chche子节点,但该配置仅会缓存对象的简单属性,若希望缓存集合属性中的元素,必须在set元素中添加<cache>子元素

          <class name="Student" table="STUDENT">

        <cache usage="read-write"/>

 

 

 

    方案二:在大配置文件(hibernate.cfg.

         <class-cache usage="read-write" class="cn.happy.entity.Student"/>

          <collection-cache usage="read-write" collection=""/>--可注释如果配置不成功

 

 

 

    注意大配置的书写位置

      Multiple annotations found at this line:

         - The content of element type "session-factory" must match "(property*,mapping*,(class-cache|collection-

          cache)*,event*,listener*)".

         - Start tag of element <session-factory>

 

    *5.在src下添加ehcache.

      

<ehcache>  <!-- Sets the path to the directory where cache .data files are created.     If the path is a Java System Property it is replaced by     its value in the running VM.     The following properties are translated:     user.home - User's home directory     user.dir - User's current working directory     java.io.tmpdir - Default temp file path -->  <diskStore path="d:/tmp"/>  <!--Default Cache configuration. These will applied to caches programmatically created through    the CacheManager.    The following attributes are required for defaultCache:    maxInMemory    - Sets the maximum number of objects that will be created in memory    eternal      - Sets whether elements are eternal. If eternal, timeouts are ignored and the element              is never expired.    timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used              if the element is not eternal. Idle time is now - last accessed time    timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used              if the element is not eternal. TTL is now - creation time    overflowToDisk  - Sets whether elements can overflow to disk when the in-memory cache              has reached the maxInMemory limit.    -->  <defaultCache    maxElementsInMemory="10000"    eternal="false"    timeToIdleSeconds="120"    timeToLiveSeconds="120"    overflowToDisk="true"    />  <!--Predefined caches. Add your cache configuration settings here.    If you do not have a configuration for your cache a WARNING will be issued when the    CacheManager starts    The following attributes are required for defaultCache:    name       - Sets the name of the cache. This is used to identify the cache. It must be unique.    maxInMemory    - Sets the maximum number of objects that will be created in memory    eternal      - Sets whether elements are eternal. If eternal, timeouts are ignored and the element              is never expired.    timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used              if the element is not eternal. Idle time is now - last accessed time    timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used              if the element is not eternal. TTL is now - creation time    overflowToDisk  - Sets whether elements can overflow to disk when the in-memory cache              has reached the maxInMemory limit.    -->  <!-- Sample cache named sampleCache1    This cache contains a maximum in memory of 10000 elements, and will expire    an element if it is idle for more than 5 minutes and lives for more than    10 minutes.    If there are more than 10000 elements it will overflow to the    disk cache, which in this configuration will go to wherever java.io.tmp is    defined on your system. On a standard Linux system this will be /tmp"    -->  <cache name="sampleCache1"    maxElementsInMemory="10000"    eternal="false"    timeToIdleSeconds="300"    timeToLiveSeconds="600"    overflowToDisk="true"    />  <!-- Sample cache named sampleCache2    This cache contains 1000 elements. Elements will always be held in memory.    They are not expired. -->  <cache name="sampleCache2"    maxElementsInMemory="1000"    eternal="true"    timeToIdleSeconds="0"    timeToLiveSeconds="0"    overflowToDisk="false"    /> -->  <!-- Place configuration for your caches following --></ehcache>

 

(二二级缓存散装数据原理图 

解析:每次从二级缓存中取出的对象,都是一个新的对象。

初识Hibernate 缓存

(三)什么样的数据适合放入二级缓存中?

  1很少被修改的数据

  2不是很重要的数据,允许出现偶尔并发的数据

  3不会被并发访问的数据

  4参考数据,指的是供应用参考的常量数据,它的实列数目有限,它的实列被许多其他类的实列引用,实列极少或者从来不会被修改。

 

 

讲述几乎结束了!!!

 




原标题:初识Hibernate 缓存

关键词:Hibernate

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Google Ads如何开户?_Google Ads开户必备资料:https://www.ikjzd.com/articles/136168
深度剖析Google Shopping与Bing Shopping:https://www.ikjzd.com/articles/136169
独家对话IPO大卖员工,探其“亚马逊封神”之路!:https://www.ikjzd.com/articles/136170
泰格豪雅、鬼爪和间谍忍者侵权预警!:https://www.ikjzd.com/articles/136171
最新!亚马逊品牌备案流程3.0版本:https://www.ikjzd.com/articles/136172
加州大火再度肆虐,橘郡民众紧急大撤离!:https://www.ikjzd.com/articles/136173
探讨内地人开设香港账户的可行性 :https://www.kjdsnews.com/a/1836442.html
在古巴做游轮 古巴旅游项目:https://www.vstour.cn/a/363194.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流