Hibernate中二級快取(sessionFactory)的配置及其使用。

渴睡人的眼發表於2018-07-17

二級快取的配置,相當於給model類做配置。隨後對該類做出的操作均是在二級快取中。廢話不多說,直接上程式碼。

一、建立model類及其hbm檔案。這裡不多說。就進行常規的配置即可。

二、在hibernate.cfg.xml中開啟二級快取及Query查詢快取。

<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<!-- 對hibernate全域性設定 -->
<hibernate-configuration>
	<!-- 對session工廠的設定 -->
	<session-factory>
		
		<!-- 資料庫連線的基本設定 四本一言基本設定-->
		<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/hibernate?characterEncoding=utf8</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123456</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
		
		<!-- 預設是不使用二級快取 。設定開啟二級快取 -->
		<property name="hibernate.cache.use_second_level_cache">true</property>
		
		<!-- 
			開啟查詢快取
			如果沒有開啟二級快取,只開啟了查詢快取,此時,
			快取中只儲存查詢出來的資料的id,如果後續再查詢相同
			的資料,根據快取中的id分條查詢資料,即所謂的N+1。
		 -->
		<property name="hibernate.cache.use_query_cache">true</property>
		
		<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

		
		<!-- 控制檯是否顯示sql語句 -->
		<property name="show_sql">true</property>
		
		<!-- 格式sql -->
		<property name="format_sql">true</property>
		
		<!-- 自動建表 -->
		<property name="hbm2ddl.auto">update</property>  
		
		<!-- 引入對映檔案 -->
		<mapping resource="com/xalo/model/ClassRoom.hbm.xml"/>
				
		<!-- 指定使用快取物件的策略 並指定併發策略(usage) -->
		<class-cache usage="read-only" class="com.xalo.model.ClassRoom"/>
		
	
	</session-factory>
</hibernate-configuration>

需要注意的是class-cache標籤必須在mapping標籤之下。

三、配置ehcache.xml檔案

<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<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="java.io.tmpdir"/>


    <!--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.

        -->
        
        
        <!-- 
        		所有使用到二級快取的物件的預設設定 
        		maxElementsInMemory:允許在記憶體中快取的物件個數
        		eternal:快取是否永久有效
        		timeToIdleSeconds:設定 快取物件 最大空閒的時間 超過該時間銷燬 
        		timeToLiveSeconds:設定 快取物件 最大存活時間 超過該時間銷燬
        		overflowToDisk:超出允許快取個數後 是否儲存到指定位置硬碟
        -->
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />
    <!-- 單獨配置(個性化)某個二級快取物件 -->
    <cache 
    		name="com.xalo.model.ClassRoom"
    		maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        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>

隨後對model類做出操作均是在二級快取中進行的了

相關文章