日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

初识Hibernate 缓存

發(fā)布時(shí)間:2023/12/20 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 初识Hibernate 缓存 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

    生活就像一杯咖啡,讓你我慢慢的品嘗,品嘗它的苦澀和甘甜......

一、什么是Hibernate緩存。

   解析:白話來說就是緩存數(shù)據(jù)的容器

      官方標(biāo)準(zhǔn)點(diǎn)緩存:是計(jì)算機(jī)領(lǐng)域的概念,它介于應(yīng)用程序和永久性數(shù)據(jù)存儲(chǔ)源之間。

      作用:降低應(yīng)用程序直接讀寫數(shù)據(jù)庫(kù)的頻率,從而提高程序的運(yùn)行性能。緩存中的數(shù)據(jù)是數(shù)據(jù)存儲(chǔ)源中數(shù)據(jù)的拷貝。緩存的物理介質(zhì)通常是內(nèi)存。

二、緩存一般分為三個(gè)類

  一級(jí)緩存

  二級(jí)緩存

  查看緩存

三、一級(jí)緩存

場(chǎng)景一:使用同一個(gè)session連續(xù)查詢兩次同一個(gè)對(duì)象

/查詢學(xué)生信息public static void select(){//由班級(jí)查詢?cè)摪嗉?jí)學(xué)生信息Session session=HibernateUtil.currentSession();Grade grade=(Grade) session.get(Grade.class, 14);//輸出班級(jí)信息System.out.println(grade.getGname());Grade grade2=(Grade) session.get(Grade.class, 14);//輸出班級(jí)信息System.out.println(grade2.getGname());}

執(zhí)行結(jié)果:

場(chǎng)景一:在第一次查詢完畢后,關(guān)閉session對(duì)象,重新開啟一個(gè)session然后繼續(xù)查詢同一個(gè)對(duì)象

//查詢學(xué)生信息public static void select(){//由班級(jí)查詢?cè)摪嗉?jí)學(xué)生信息Session session=HibernateUtil.currentSession();Grade grade=(Grade) session.get(Grade.class, 14);//輸出班級(jí)信息System.out.println(grade.getGname());//關(guān)閉session HibernateUtil.closeSession();//重新獲取sessionsession=HibernateUtil.currentSession();Grade grade2=(Grade) session.get(Grade.class, 14);//輸出班級(jí)信息System.out.println(grade2.getGname());}

執(zhí)行結(jié)果:

?

解析:

  ?1:當(dāng)我沒有關(guān)閉session時(shí)用的同一個(gè)session兩次訪問同一個(gè)對(duì)象時(shí),只會(huì)向DB端發(fā)送一條sql語句
    * 原因:因?yàn)槲业谝淮卧L問數(shù)據(jù)庫(kù)的時(shí)候Hibernate會(huì)自動(dòng)的將我查詢出來的結(jié)果保留一份查詢出來的對(duì)象到一級(jí)緩存
       ? ?并且這個(gè)額對(duì)象是根據(jù)OID唯一標(biāo)識(shí)的,也可以理解為數(shù)據(jù)庫(kù)中的主鍵值,然后當(dāng)我再一次訪問一個(gè)對(duì)象時(shí),Hibernate
        機(jī)制會(huì)自動(dòng)的先去一級(jí)緩存中查找看有沒有OID與我要查詢的OID相同的對(duì)象,如果有的話,則直接從一級(jí)緩存中 拿數(shù)據(jù)
       ? ?如果沒有相同的OID則說明緩存中沒有我要的記錄,那么就會(huì)直接去訪問DB端了,這樣的話,又會(huì)重新發(fā)送一條sql
  ??2:當(dāng)我第一次查詢完數(shù)據(jù)后立即關(guān)閉session,這時(shí)重新開啟一個(gè)session來訪問同一個(gè)對(duì)象,這時(shí)我們會(huì)發(fā)現(xiàn)它居然向數(shù)據(jù)庫(kù)發(fā)送了兩條Sql語句。這是為什么呢?
    * 原因:其實(shí)原因很簡(jiǎn)單,因?yàn)槲覀冸m然說是訪問的同一個(gè)對(duì)象,但是我們隨即就關(guān)閉了這個(gè)session而重新開啟了一個(gè)session,

        此時(shí)我們?cè)L問時(shí)的session是不一致的也就是說是兩個(gè)不同的session發(fā)出的請(qǐng)求,這樣理解的話,我們就不難理解了。

        ??所以總結(jié)出,一級(jí)緩存是一個(gè)會(huì)話級(jí)別的緩存,當(dāng)一次回話結(jié)束后該會(huì)話里的緩存則會(huì)全部的銷毀,所有我們自然就只能重新發(fā)送一條sql啦。

3補(bǔ)充以下方法支持一級(jí)緩存

?

?

?四、二級(jí)緩存

(一)配置二級(jí)緩存 

  1.引入如下jar包。

?????   ehcache-1.2.3.jar? 核心庫(kù)

?????   backport-util-concurrent.jar

?????   commons-logging.jar

  2.配置Hibernate.cfg.xml開啟二級(jí)緩存

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

?

?

?

? ? ?3.配置二級(jí)緩存的供應(yīng)商

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

?

?

?

  4.指定使用二級(jí)緩存的類

    方案一:在*.hbm.xml中配置

??????   在<class元素的子元素下添加chche子節(jié)點(diǎn),但該配置僅會(huì)緩存對(duì)象的簡(jiǎn)單屬性,若希望緩存集合屬性中的元素,必須在set元素中添加<cache>子元素

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

        <cache usage="read-write"/>

?

?

?

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

        ?<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.xml文件,從etc獲取文件即可。

      

<ehcache><!-- Sets the path to the directory where cache .data files are created.If the path is a Java System Property it is replaced byits value in the running VM.The following properties are translated:user.home - User's home directoryuser.dir - User's current working directoryjava.io.tmpdir - Default temp file path --><diskStore path="d:/tmp"/><!--Default Cache configuration. These will applied to caches programmatically created throughthe CacheManager.The following attributes are required for defaultCache:maxInMemory - Sets the maximum number of objects that will be created in memoryeternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the elementis never expired.timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only usedif the element is not eternal. Idle time is now - last accessed timetimeToLiveSeconds - Sets the time to live for an element before it expires. Is only usedif the element is not eternal. TTL is now - creation timeoverflowToDisk - Sets whether elements can overflow to disk when the in-memory cachehas reached the maxInMemory limit.--><defaultCachemaxElementsInMemory="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 theCacheManager startsThe 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 memoryeternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the elementis never expired.timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only usedif the element is not eternal. Idle time is now - last accessed timetimeToLiveSeconds - Sets the time to live for an element before it expires. Is only usedif the element is not eternal. TTL is now - creation timeoverflowToDisk - Sets whether elements can overflow to disk when the in-memory cachehas reached the maxInMemory limit.--><!-- Sample cache named sampleCache1This cache contains a maximum in memory of 10000 elements, and will expirean element if it is idle for more than 5 minutes and lives for more than10 minutes.If there are more than 10000 elements it will overflow to thedisk cache, which in this configuration will go to wherever java.io.tmp isdefined 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 sampleCache2This 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>

?

(二)二級(jí)緩存散裝數(shù)據(jù)原理圖?

解析:每次從二級(jí)緩存中取出的對(duì)象,都是一個(gè)新的對(duì)象。

(三)什么樣的數(shù)據(jù)適合放入二級(jí)緩存中?

  1很少被修改的數(shù)據(jù)

  2不是很重要的數(shù)據(jù),允許出現(xiàn)偶爾并發(fā)的數(shù)據(jù)

  3不會(huì)被并發(fā)訪問的數(shù)據(jù)

  4參考數(shù)據(jù),指的是供應(yīng)用參考的常量數(shù)據(jù),它的實(shí)列數(shù)目有限,它的實(shí)列被許多其他類的實(shí)列引用,實(shí)列極少或者從來不會(huì)被修改。

?

?

講述幾乎結(jié)束了!!!

?

轉(zhuǎn)載于:https://www.cnblogs.com/yejiaojiao/p/5798448.html

總結(jié)

以上是生活随笔為你收集整理的初识Hibernate 缓存的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。