Hibernate中二級快取(sessionFactory)的配置及其使用。
二級快取的配置,相當於給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類做出操作均是在二級快取中進行的了
相關文章
- Hibernate一級快取(session)與二級快取(sessionFactory)的知識點。快取Session
- 快取架構中的服務詳解!SpringBoot中二級快取服務的實現快取架構Spring Boot
- Mybatis的二級快取、使用Redis做二級快取MyBatis快取Redis
- Hibernate【快取】知識要點快取
- Mybatis二級快取使用MyBatis快取
- nginx快取使用詳解,nginx快取使用及配置步驟Nginx快取
- MyBatis快取機制(一級快取,二級快取)MyBatis快取
- Django(39)使用redis配置快取DjangoRedis快取
- Hibernate 基本操作、懶載入以及快取快取
- mybatis快取-二級快取MyBatis快取
- JAVA快取-Redis入門級使用Java快取Redis
- http快取機制及其原理HTTP快取
- Mybatis的二級快取MyBatis快取
- Mybatis整合二級快取與同時使用快取與事務存在的坑MyBatis快取
- http快取與cdn快取配置指南HTTP快取
- 一文讀懂快取穿透、快取擊穿、快取雪崩及其解決方案快取穿透
- 配置session——快取Session快取
- Mybatis的快取——一級快取和原始碼分析MyBatis快取原始碼
- 探討下如何更好的使用快取 —— Redis快取的特殊用法以及與本地快取一起構建多級快取的實現快取Redis
- 瀏覽器快取和webpack快取配置瀏覽器快取Web
- 簡述 HTTP 快取首部及其行為HTTP快取
- MybatisPlus二級快取MyBatis快取
- mybatis原始碼學習:一級快取和二級快取分析MyBatis原始碼快取
- Hibernate配置OracleOracle
- Laravel 實現二級快取 提高快取的命中率和細粒化快取 keyLaravel快取
- Apache靜態快取配置Apache快取
- Nuxt頁面級快取UX快取
- mybatis快取之一級快取(一)MyBatis快取
- mybatis快取之一級快取(二)MyBatis快取
- Mybatis 一級快取和二級快取原理區別 (圖文詳解)MyBatis快取
- 電腦CPU二級快取的開啟方法和如何檢視二級快取的引數快取
- jetcache快取使用快取
- SpringBoot使用快取Spring Boot快取
- Android 的二級快取如斯簡單Android快取
- 009.Nginx快取及配置Nginx快取
- Nginx配置瀏覽器快取Nginx瀏覽器快取
- Nginx快取伺服器配置Nginx快取伺服器
- spring配置redis註解快取SpringRedis快取