Hibernate二級緩存配置

1.文件轉載於:http://hi.baidu.com/xusuofei/blog/item/609468d0bc374e8ba0ec9c88.html

 

 

2.配置二級緩存的主要步驟:

   (1)準備

          把ehcache-1.2.3.jar加入到classpath中

          在hibernate.cfg.xml中加入EHCache緩存插件的提供類

 

 

Category.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">

<hibernate-mapping>
    <class name="org.qiujy.domain.cachedemo.Category" table="categories">
       <!—
             配置緩存,必須緊跟在class元素後面
            對緩存中的Category對象採用讀寫型的併發訪問策略
        -->
       <cache usage="read-write"/>
      
       <id name="id" type="java.lang.Long">
           <column name="id" />
           <generator class="native" />
       </id>
       <!-- 配置版本號,必須緊跟在id元素後面 -->
       <version name="version" column="version" type="java.lang.Long" />
      
       <property name="name" type="java.lang.String">
           <column name="name" length="32" not-null="true"/>
       </property>
      
       <property name="description" type="java.lang.String">
           <column name="description" length="255"/>
       </property>
      
       <set name="products" table="products" cascade="all" inverse="true">
           <!-- Hibernate只會緩存對象的簡單屬性的值,
       要緩存集合屬性,必須在集合元素中也加入<cache>子元素
       而Hibernate僅僅是把與當前持久對象關聯的對象的OID存放到緩存中。
如果希望把整個關聯的對象的所有數據都存入緩存,
則要在相應關聯的對象的映射文件中配置<cache>元素
           -->
           <cache usage="read-write"/>
          
           <key column="categoryId" not-null="true"/>
           <one-to-many class="org.qiujy.domain.cachedemo.Product"/>
       </set>
      
    </class>
</hibernate-mapping>
Product.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">

<hibernate-mapping>
    <class name="org.qiujy.domain.cachedemo.Product" table="products">
      
       <cache usage="read-write"/>
      
       <id name="id" type="java.lang.Long">
           <column name="id" />
           <generator class="native" />
       </id>
       <!-- 配置版本號,必須緊跟在id元素後面 -->
       <version name="version" column="version" type="java.lang.Long" />
      
       <property name="name" type="java.lang.String">
           <column name="name" length="32" not-null="true"/>
       </property>
      
       <property name="description" type="java.lang.String">
           <column name="description" length="255"/>
       </property>
      
       <property name="unitCost" type="java.lang.Double">
           <column name="unitCost" />
       </property>
      
       <property name="pubTime" type="java.util.Date">
           <column name="pubTime" not-null="true" />
       </property>
      
       <many-to-one name="category"
                column="categoryId"
               class="org.qiujy.domain.cachedemo.Category"
               cascade="save-update"
                not-null="true">
        </many-to-one>
      
    </class>
</hibernate-mapping>

2)      編輯ehcache.xml文件:
<ehcache>
    <diskStore path="c:\\ehcache\"/>
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"  
        />
       
    <!-- 設置Category類的緩存的數據過期策略 -->
    <cache name="org.qiujy.domain.cachedemo.Category"
        maxElementsInMemory="100"
        eternal="true"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        overflowToDisk="false"
        />
       
     <!-- 設置Category類的products集合的緩存的數據過期策略 -->
     <cache name="org.qiujy.domain.cachedemo.Category.products"
        maxElementsInMemory="500"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"
        />
       
    <cache name="org.qiujy.domain.cachedemo.Product"
        maxElementsInMemory="500"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"
        />
   
</ehcache>
配置的元素說明:
元素或屬性 描述
<diskStore> 設置緩存數據文件的存放目錄
<defaultCache> 設置緩存的默認數據過期策略
<cache> 設定具體的命名緩存的數據過期策略
每個命名緩存代表一個緩存區域,每個緩存區域有各自的數據過期策略。命名緩存機制使得用戶能夠在每個類以及類的每個集合的粒度上設置數據過期策略。
cache元素的屬性  
name 設置緩存的名字,它的取值爲類的全限定名或類的集合的名字
maxInMemory 設置基於內存的緩存中可存放的對象最大數目
eternal 設置對象是否爲永久的,true表示永不過期,此時將忽略timeToIdleSeconds和timeToLiveSeconds屬性;
默認值是false
timeToIdleSeconds 設置對象空閒最長時間,超過這個時間,對象過期。當對象過期時,EHCache會把它從緩存中清除。
如果此值爲0,表示對象可以無限期地處於空閒狀態。
timeToLiveSeconds 設置對象生存最長時間,超過這個時間,對象過期。
如果此值爲0,表示對象可以無限期地存在於緩存中。
overflowToDisk 設置基於內在的緩存中的對象數目達到上限後,是否把溢出的對象寫到基於硬盤的緩存中
發佈了38 篇原創文章 · 獲贊 0 · 訪問量 1754
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章