一級緩存與二級緩存
MyBatis將數(shù)據(jù)緩存設(shè)計成兩級結(jié)構(gòu),分為一級緩存、二級緩存:
一級緩存是Session會話級別的緩存,位于表示一次數(shù)據(jù)庫會話的SqlSession對象之中,又被稱之為本地緩存。一級緩存是MyBatis內(nèi)部實現(xiàn)的一個特性,用戶不能配置,默認情況下自動支持的緩存,用戶沒有定制它的權(quán)利(不過這也不是絕對的,可以通過開發(fā)插件對它進行修改);
二級緩存是Application應(yīng)用級別的緩存,它的是生命周期很長,跟Application的聲明周期一樣,也就是說它的作用范圍是整個Application應(yīng)用。
MyBatis中一級緩存和二級緩存的組織如下圖所示:
一級緩存的工作機制:
一級緩存是Session會話級別的,一般而言,一個SqlSession對象會使用一個Executor對象來完成會話操作,Executor對象會維護一個Cache緩存,以提高查詢性能。
二級緩存的工作機制:
如上所言,一個SqlSession對象會使用一個Executor對象來完成會話操作,MyBatis的二級緩存機制的關(guān)鍵就是對這個Executor對象做文章。如果用戶配置了"cacheEnabled=true",那么MyBatis在為SqlSession對象創(chuàng)建Executor對象時,會對Executor對象加上一個裝飾者:CachingExecutor,這時SqlSession使用CachingExecutor對象來完成操作請求。CachingExecutor對于查詢請求,會先判斷該查詢請求在Application級別的二級緩存中是否有緩存結(jié)果,如果有查詢結(jié)果,則直接返回緩存結(jié)果;如果緩存中沒有,再交給真正的Executor對象來完成查詢操作,之后CachingExecutor會將真正Executor返回的查詢結(jié)果放置到緩存中,然后在返回給用戶。
MyBatis的二級緩存設(shè)計得比較靈活,你可以使用MyBatis自己定義的二級緩存實現(xiàn);你也可以通過實現(xiàn)org.apache.ibatis.cache.Cache接口自定義緩存;也可以使用第三方內(nèi)存緩存庫,如Memcached等。
緩存的改造
問題:
最容易出現(xiàn)的問題是開啟cache后,分頁查詢時無論查詢哪一頁都返回第一頁的數(shù)據(jù)。另外,使用sql自動生成插件生成get方法的sql時,傳入的參數(shù)不起作用,無論傳入的參數(shù)是多少,都返回第一個參數(shù)的查詢結(jié)果。
為什么出現(xiàn)這些問題:
在之前講解Mybatis的執(zhí)行流程的時候提到,在開啟cache的前提下,Mybatis的executor會先從緩存里讀取數(shù)據(jù),讀取不到才去數(shù)據(jù)庫查詢。問題就出在這里,sql自動生成插件和分頁插件執(zhí)行的時機是在statementhandler里,而statementhandler是在executor之后執(zhí)行的,無論sql自動生成插件和分頁插件都是通過改寫sql來實現(xiàn)的,executor在生成讀取cache的key(key由sql以及對應(yīng)的參數(shù)值構(gòu)成)時使用都是原始的sql,這樣當然就出問題了。
解決問題:
找到問題的原因后,解決起來就方便了。只要通過攔截器改寫executor里生成key的方法,在生成可以時使用自動生成的sql(對應(yīng)sql自動生成插件)或加入分頁信息(對應(yīng)分頁插件)就可以了。
攔截器簽名:
1
2
3
4
|
@Intercepts ({ @Signature (type = Executor. class , method = "query" , args = {MappedStatement. class , Object. class , RowBounds. class , ResultHandler. class })}) public class CacheInterceptor implements Interceptor { ... } |
從簽名里可以看出,要攔截的目標類型是Executor(注意:type只能配置成接口類型),攔截的方法是名稱為query的方法。
intercept的實現(xiàn):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
public Object intercept(Invocation invocation) throws Throwable { Executor executorProxy = (Executor) invocation.getTarget(); MetaObject metaExecutor = MetaObject.forObject(executorProxy, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY); // 分離代理對象鏈 while (metaExecutor.hasGetter( "h" )) { Object object = metaExecutor.getValue( "h" ); metaExecutor = MetaObject.forObject(object, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY); } // 分離最后一個代理對象的目標類 while (metaExecutor.hasGetter( "target" )) { Object object = metaExecutor.getValue( "target" ); metaExecutor = MetaObject.forObject(object, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY); } Object[] args = invocation.getArgs(); return this .query(metaExecutor, args); } public <E> List<E> query(MetaObject metaExecutor, Object[] args) throws SQLException { MappedStatement ms = (MappedStatement) args[ 0 ]; Object parameterObject = args[ 1 ]; RowBounds rowBounds = (RowBounds) args[ 2 ]; ResultHandler resultHandler = (ResultHandler) args[ 3 ]; BoundSql boundSql = ms.getBoundSql(parameterObject); // 改寫key的生成 CacheKey cacheKey = createCacheKey(ms, parameterObject, rowBounds, boundSql); Executor executor = (Executor) metaExecutor.getOriginalObject(); return executor.query(ms, parameterObject, rowBounds, resultHandler, cacheKey, boundSql); } private CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) { Configuration configuration = ms.getConfiguration(); pageSqlId = configuration.getVariables().getProperty( "pageSqlId" ); if ( null == pageSqlId || "" .equals(pageSqlId)) { logger.warn( "Property pageSqlId is not setted,use default '.*Page$' " ); pageSqlId = defaultPageSqlId; } CacheKey cacheKey = new CacheKey(); cacheKey.update(ms.getId()); cacheKey.update(rowBounds.getOffset()); cacheKey.update(rowBounds.getLimit()); List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); // 解決自動生成SQL,SQL語句為空導(dǎo)致key生成錯誤的bug if ( null == boundSql.getSql() || "" .equals(boundSql.getSql())) { String id = ms.getId(); id = id.substring(id.lastIndexOf( "." ) + 1 ); String newSql = null ; try { if ( "select" .equals(id)) { newSql = SqlBuilder.buildSelectSql(parameterObject); } SqlSource sqlSource = buildSqlSource(configuration, newSql, parameterObject.getClass()); parameterMappings = sqlSource.getBoundSql(parameterObject).getParameterMappings(); cacheKey.update(newSql); } catch (Exception e) { logger.error( "Update cacheKey error." , e); } } else { cacheKey.update(boundSql.getSql()); } MetaObject metaObject = MetaObject.forObject(parameterObject, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY); if (parameterMappings.size() > 0 && parameterObject != null ) { TypeHandlerRegistry typeHandlerRegistry = ms.getConfiguration().getTypeHandlerRegistry(); if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { cacheKey.update(parameterObject); } else { for (ParameterMapping parameterMapping : parameterMappings) { String propertyName = parameterMapping.getProperty(); if (metaObject.hasGetter(propertyName)) { cacheKey.update(metaObject.getValue(propertyName)); } else if (boundSql.hasAdditionalParameter(propertyName)) { cacheKey.update(boundSql.getAdditionalParameter(propertyName)); } } } } // 當需要分頁查詢時,將page參數(shù)里的當前頁和每頁數(shù)加到cachekey里 if (ms.getId().matches(pageSqlId) && metaObject.hasGetter( "page" )) { PageParameter page = (PageParameter) metaObject.getValue( "page" ); if ( null != page) { cacheKey.update(page.getCurrentPage()); cacheKey.update(page.getPageSize()); } } return cacheKey; } |
plugin的實現(xiàn):
1
2
3
4
5
6
7
8
9
|
public Object plugin(Object target) { // 當目標類是CachingExecutor類型時,才包裝目標類,否者直接返回目標本身,減少目標被代理的 // 次數(shù) if (target instanceof CachingExecutor) { return Plugin.wrap(target, this ); } else { return target; } } |