(轉)MyBatis 一、二級緩存和自定義緩存
1、一級緩存
MyBatis 默認開啟了一級緩存,一級緩存是在SqlSession 層面進行緩存的。即,同一個SqlSession ,多次調用同一個Mapper和同一個方法的同一個參數,
只會進行一次數據庫查詢,然後把數據緩存到緩沖中,以後直接先從緩存中取出數據,不會直接去查數據庫。
? 但是不同的SqlSession對象,因為用的SqlSession都是相互隔離的,所以相同的Mapper、參數和方法,他還是會再次發送到SQL到數據庫去執行,返回結果;
1 public static void main(String[] args) { 2 //View Code自定義的單例SqlSessionFactory模式 3 SqlSessionFactory factory = SqlSessionFactoryUtil.openSqlSession(); 4 5 // 獲得SqlSession對象 6 SqlSession sqlSession = factory.openSession(); 7 // 獲得dao實體 8 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 9 //進行兩次相同的查詢操作 10 userMapper.selectByPrimaryKey(1); 11 userMapper.selectByPrimaryKey(1); 12 // 註意,當我們使用二級緩存時候,sqlSession需要使用commit時候才會生效 13 sqlSession.commit(); 14 15 System.out.println("\n\n============================================================="); 16 //獲得一個新的SqlSession 對象 17 SqlSession sqlSession1 = factory.openSession(); 18 // 進行相同的查詢操作 19 sqlSession1.getMapper(UserMapper.class).selectByPrimaryKey(1); 20 // 註意,當我們使用二級緩存時候,sqlSession需要使用commit時候才會生效 21 sqlSession.commit(); 22 }
日誌輸出
DEBUG [main] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@77caeb3e] DEBUG [main] - ==> Preparing: select user_ID, login_name,user_name, user_code, user_type, user_active, organization_ID,user_position,password from user where user_ID = ? DEBUG [main] - ==> Parameters: 1(Integer) TRACE [main] - <== Columns: user_ID, login_name, user_name, user_code, user_type, user_active, organization_ID, user_position, password TRACE [main] - <== Row: 1, ASH-001, 小明, JIKF-001, ADMIN, 1, 0, 銷售員, 1212121212121 DEBUG [main] - <== Total: 1 ============================================================= DEBUG [main] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@553f17c] DEBUG [main] - ==> Preparing: select user_ID, login_name,user_name, user_code, user_type, user_active, organization_ID,user_position,password from user where user_ID = ? DEBUG [main] - ==> Parameters: 1(Integer) TRACE [main] - <== Columns: user_ID, login_name, user_name, user_code, user_type, user_active, organization_ID, user_position, password TRACE [main] - <== Row: 1, ASH-001, 小明, JIKF-001, ADMIN, 1, 0, 銷售員, 1212121212121 DEBUG [main] - <== Total: 1
可以發現,第一次的兩個相同操作,只執行了一次數據庫。後來的那個操作又進行了數據庫查詢;
2、二級緩存
為了克服這個問題,需要開啟二級緩存,是的緩存在SqlSessionFactory層面給各個SqlSession 對象共享。默認二級緩存是不開啟的,需要手動進行配置;
如果這樣配置的話,很多其他的配置就會被默認進行,如:
- 映射文件所有的select 語句會被緩存
- 映射文件的所有的insert、update和delete語句會刷新緩存
- 緩存會使用默認的Least Recently Used(LRU,最近最少使用原則)的算法來回收緩存空間
- 根據時間表,比如No Flush Interval,(CNFI,沒有刷新間隔),緩存不會以任何時間順序來刷新
- 緩存會存儲列表集合或對象(無論查詢方法返回什麽)的1024個引用
- 緩存會被視為是read/write(可讀/可寫)的緩存,意味著對象檢索不是共享的,而且可以很安全的被調用者修改,不幹擾其他調用者或縣城所作的潛在修改
添加後日誌打印如下,可以發現所有過程只使用了一次數據庫查詢
EBUG [main] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@5622fdf] DEBUG [main] - ==> Preparing: select user_ID, login_name,user_name, user_code, user_type, user_active, organization_ID,user_position,password from user where user_ID = ? DEBUG [main] - ==> Parameters: 1(Integer) TRACE [main] - <== Columns: user_ID, login_name, user_name, user_code, user_type, user_active, organization_ID, user_position, password TRACE [main] - <== Row: 1, AS-01, 小明, HJ-009, ADMIN, 1, 0, 銷售員, dasfasdfasdfsdf DEBUG [main] - <== Total: 1 =============================================================
可以在開啟二級緩存時候,手動配置一些屬性
<cache eviction="LRU" flushInterval="100000" size="1024" readOnly="true"/>
各個屬性意義如下:
- eviction:緩存回收策略
- LRU:最少使用原則,移除最長時間不使用的對象
- FIFO:先進先出原則,按照對象進入緩存順序進行回收
- SOFT:軟引用,移除基於垃圾回收器狀態和軟引用規則的對象
- WEAK:弱引用,更積極的移除移除基於垃圾回收器狀態和弱引用規則的對象 - flushInterval:刷新時間間隔,單位為毫秒,這裏配置的100毫秒。如果不配置,那麽只有在進行數據庫修改操作才會被動刷新緩存區
- size:引用額數目,代表緩存最多可以存儲的對象個數
- readOnly:是否只讀,如果為true,則所有相同的sql語句返回的是同一個對象(有助於提高性能,但並發操作同一條數據時,可能不安全),如果設置為false,則相同的sql,後面訪問的是cache的clone副本。
可以在Mapper的具體方法下設置對二級緩存的訪問意願:
useCache配置
如果一條語句每次都需要最新的數據,就意味著每次都需要從數據庫中查詢數據,可以把這個屬性設置為false,如:
<select id="selectAll" resultMap="BaseResultMap" useCache="false">
刷新緩存(就是清空緩存)
? 二級緩存默認會在insert、update、delete操作後刷新緩存,可以手動配置不更新緩存,如下:
<update id="updateById" parameterType="User" flushCache="false" />
3、自定義緩存
自定義緩存對象,該對象必須實現 org.apache.ibatis.cache.Cache 接口,如下:
1 import org.apache.ibatis.cache.Cache; 2 3 import java.util.concurrent.ConcurrentHashMap; 4 import java.util.concurrent.locks.ReadWriteLock; 5 import java.util.concurrent.locks.ReentrantReadWriteLock; 6 7 /** 8 * Created by Luky on 2017/10/14. 9 */ 10 public class BatisCache implements Cache { 11 private ReadWriteLock lock = new ReentrantReadWriteLock(); 12 private ConcurrentHashMap<Object,Object> cache = new ConcurrentHashMap<Object, Object>(); 13 private String id; 14 15 public BatisCache(){ 16 System.out.println("初始化-1!"); 17 } 18 19 //必須有該構造函數 20 public BatisCache(String id){ 21 System.out.println("初始化-2!"); 22 this.id = id; 23 } 24 25 // 獲取緩存編號 26 public String getId() { 27 System.out.println("得到ID:" + id); 28 return id; 29 } 30 31 //獲取緩存對象的大小 32 public int getSize() { 33 System.out.println("獲取緩存大小!"); 34 return 0; 35 } 36 // 保存key值緩存對象 37 public void putObject(Object key, Object value) { 38 System.out.println("往緩存中添加元素:key=" + key+",value=" + value); 39 cache.put(key,value); 40 } 41 42 //通過KEY 43 public Object getObject(Object key) { 44 System.out.println("通過kEY獲取值:" + key); 45 System.out.println("OVER"); 46 System.out.println("======================================================="); 47 System.out.println("值為:" + cache.get(key)); 48 System.out.println("=====================OVER=============================="); 49 return cache.get(key); 50 } 51 52 // 通過key刪除緩存對象 53 public Object removeObject(Object key) { 54 System.out.println("移除緩存對象:" + key); 55 return null; 56 } 57 58 // 清空緩存 59 public void clear() { 60 System.out.println("清除緩存!"); 61 cache.clear(); 62 } 63 64 // 獲取緩存的讀寫鎖 65 public ReadWriteLock getReadWriteLock() { 66 System.out.println("獲取鎖對象!!!"); 67 return lock; 68 } 69 }View Code
? 在Mapper文件裏配置使用該自定義的緩存對象,如:
<cache type="com.sanyue.utils.BatisCache"/>
1 public static void main(String[] args) { 2 3 SqlSessionFactory factory = SqlSessionFactoryUtil.openSqlSession(); 4 5 // 獲得SqlSession對象 6 SqlSession sqlSession = factory.openSession(); 7 // 獲得dao實體 8 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 9 // 進行兩次相同的查詢操作 10 userMapper.selectByPrimaryKey(1); 11 userMapper.selectByPrimaryKey(1); 12 // 註意,當我們使用二級緩存時候,sqlSession需要使用commit時候才會生效 13 sqlSession.commit(); 14 15 System.out.println("\n\n============================================================="); 16 // 獲得一個新的SqlSession 對象 17 SqlSession sqlSession1 = factory.openSession(); 18 // 進行相同的查詢操作 19 sqlSession1.getMapper(UserMapper.class).selectByPrimaryKey(1); 20 sqlSession1.commit(); 21 }
? 日誌輸出如下:
初始化-2! 得到ID:com.sanyue.dao.UserMapper 獲取鎖對象!!! 通過kEY獲取值:151355725:1423317450:com.sanyue.dao.UserMapper.selectByPrimaryKey:0:2147483647: select user_ID, login_name,user_name, user_code, user_type, user_active, organization_ID,user_position,password from user where user_ID = ? :1 OVER ======================================================= 值為:null =====================OVER============================== 獲取鎖對象!!! 獲取鎖對象!!! 通過kEY獲取值:151355725:1423317450:com.sanyue.dao.UserMapper.selectByPrimaryKey:0:2147483647: select user_ID, login_name,user_name, user_code, user_type, user_active, organization_ID,user_position,password from user where user_ID = ? :1 OVER ======================================================= 值為:null =====================OVER============================== 獲取鎖對象!!! 獲取鎖對象!!! 往緩存中添加元素:key=151355725:1423317450:com.sanyue.dao.UserMapper.selectByPrimaryKey:0:2147483647: select user_ID, login_name,user_name, user_code, user_type, user_active, organization_ID,user_position,password from user where user_ID = ? :1,value=[User{userId=1, loginName=‘AS-01‘, password=‘12121212121‘, userName=‘小明‘, userCode=‘JSD-009‘, userType=‘ADMIN‘, userActive=true, userPosition=‘銷售員‘}] 獲取鎖對象!!! ============================================================= 獲取鎖對象!!! 通過kEY獲取值:151355725:1423317450:com.sanyue.dao.UserMapper.selectByPrimaryKey:0:2147483647: select user_ID, login_name,user_name, user_code, user_type, user_active, organization_ID,user_position,password from user where user_ID = ? :1 OVER ======================================================= 值為:[User{userId=1, loginName=‘AS-01‘, password=‘12121212121‘, userName=‘小明‘, userCode=‘JSD-009‘, userType=‘ADMIN‘, userActive=true, userPosition=‘銷售員‘}] =====================OVER============================== 獲取鎖對象!!!View Code
可以看出,每次查詢數據庫前,MyBatis都會先在緩存中查找是否有該緩存對象。只有當調用了commit() 方法,MyBatis才會往緩存中寫入數據,數據記錄的鍵為 數字編號+Mapper名+方法名+SQL語句+參數
格式,值為返回的對象值。
(轉)MyBatis 一、二級緩存和自定義緩存