最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Mybatis的緩存

 更新時(shí)間:2021年01月16日 08:56:28   作者:Narule  
這篇文章主要介紹了Mybatis緩存的相關(guān)資料,幫助大家更好的理解和使用Mybatis框架,感興趣的朋友可以了解下

Mybatis的緩存

mybatis是一個(gè)查詢數(shù)據(jù)庫(kù)的封裝框架,主要是封裝提供靈活的增刪改sql,開發(fā)中,service層能夠通過mybatis組件查詢和修改數(shù)據(jù)庫(kù)中表的數(shù)據(jù);作為查詢工具,mybatis有使用緩存,這里講一下mybatis的緩存相關(guān)源碼。

緩存

在計(jì)算機(jī)里面,任何信息都有源頭,緩存一般指源頭信息讀取后,放在內(nèi)存或者其他讀取較快的地方,下次讀取相同信息不去源頭查詢而是直接從內(nèi)存(或者能快速存取的硬件)讀取。這樣可以減少硬件使用,提高讀取速度。

mybatis也是這樣,查詢數(shù)據(jù)庫(kù)的數(shù)據(jù)之后,mybatis可以把查詢結(jié)果緩存到內(nèi)存,下次查詢?nèi)绻樵冋Z句相同,并且查詢相關(guān)的表的數(shù)據(jù)沒被修改過,就可以直接返回緩存中的結(jié)果,而不用去查詢數(shù)據(jù)庫(kù)的語句,有效節(jié)省了時(shí)間。

簡(jiǎn)單看一下mybatis一級(jí)緩存和二級(jí)緩存相關(guān)源碼,學(xué)習(xí)使用

一級(jí)緩存

通過查看源碼可知,一級(jí)緩存是綁定sqSsession中的,所以每次查詢sqlSession不同就失效,相同的sqlSession可以使用一級(jí)緩存。

mybatis默認(rèn)sqlsession:org.apache.ibatis.session.defaults.DefaultSqlSession

構(gòu)造方法中傳入executor(查詢執(zhí)行對(duì)象)

 public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
  this.configuration = configuration;
  this.executor = executor;
  this.dirty = false;
  this.autoCommit = autoCommit;
 }

executor中攜帶一級(jí)緩存成員:

 protected BaseExecutor(Configuration configuration, Transaction transaction) {
  this.transaction = transaction;
  this.deferredLoads = new ConcurrentLinkedQueue<>();
  this.localCache = new PerpetualCache("LocalCache"); //默認(rèn)一級(jí)緩存
  this.localOutputParameterCache = new PerpetualCache("LocalOutputParameterCache");
  this.closed = false;
  this.configuration = configuration;
  this.wrapper = this;
 }

查詢使用一級(jí)緩存邏輯

org.apache.ibatis.executor.BaseExecutor.query()

 public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
  ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());
  
  List<E> list;
  try {
   queryStack++;
   	//localCache 一級(jí)緩存
   list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;
    //先從一級(jí)緩存中獲取,key是通過sql語句生成
   if (list != null) {
    handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);
   } else {
    // 如果緩存中沒有 才從數(shù)據(jù)庫(kù)查詢
    list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
   }
  } finally {
   queryStack--;
  }
  return list;
 }

 //從數(shù)據(jù)庫(kù)讀取數(shù)據(jù)
 private <E> List<E> queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
  List<E> list;
  localCache.putObject(key, EXECUTION_PLACEHOLDER);
  try {
   list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);
  } finally {
   localCache.removeObject(key);//將一級(jí)緩存清除
  }
  localCache.putObject(key, list);//返回查詢結(jié)果之前,先放入一級(jí)緩存 刷新
  if (ms.getStatementType() == StatementType.CALLABLE) {
   localOutputParameterCache.putObject(key, parameter);
  }
  return list;
 }

二級(jí)緩存

二級(jí)緩存mapper中的,默認(rèn)是開啟的,但需要在映射文件mapper.xml中添加<cache/>標(biāo)簽

<mapper namespace="userMapper">
	<cache/><!-- 添加cache標(biāo)簽表示此mapper使用二級(jí)緩存 -->
</mapper>

配置false可以關(guān)閉二級(jí)緩存

二級(jí)緩存的解析

org.apache.ibatis.builder.xml.XMLMapperBuilder

 private void configurationElement(XNode context) {
  try {
   //...
   cacheElement(context.evalNode("cache")); //解析cache標(biāo)簽
  } catch (Exception e) {
   throw new BuilderException("Error parsing Mapper XML. The XML location is '" + resource + "'. Cause: " + e, e);
  }
 }

 private void cacheElement(XNode context) {
  if (context != null) { // if hava cache tag 如果有cache標(biāo)簽才執(zhí)行下面的邏輯
   String type = context.getStringAttribute("type", "PERPETUAL");
   Class<? extends Cache> typeClass = typeAliasRegistry.resolveAlias(type);
   String eviction = context.getStringAttribute("eviction", "LRU");
   Class<? extends Cache> evictionClass = typeAliasRegistry.resolveAlias(eviction);
   Long flushInterval = context.getLongAttribute("flushInterval");
   Integer size = context.getIntAttribute("size");
   boolean readWrite = !context.getBooleanAttribute("readOnly", false);
   boolean blocking = context.getBooleanAttribute("blocking", false);
   Properties props = context.getChildrenAsProperties();
   builderAssistant.useNewCache(typeClass, evictionClass, flushInterval, size, readWrite, blocking, props);//建立二級(jí)緩存
  }
 }

org.apache.ibatis.builder.MapperBuilderAssistant.useNewCache():

 public Cache useNewCache(Class<? extends Cache> typeClass,
   Class<? extends Cache> evictionClass,
   Long flushInterval,
   Integer size,
   boolean readWrite,
   boolean blocking,
   Properties props) {
  Cache cache = new CacheBuilder(currentNamespace)
    .implementation(valueOrDefault(typeClass, PerpetualCache.class))
    .addDecorator(valueOrDefault(evictionClass, LruCache.class))
    .clearInterval(flushInterval)
    .size(size)
    .readWrite(readWrite)
    .blocking(blocking)
    .properties(props)
    .build();
  configuration.addCache(cache);//二級(jí)緩存賦值,如果cache標(biāo)簽為空,不會(huì)執(zhí)行此方法,currentCache為空
  currentCache = cache; 
  return cache;
 }

 在映射文件mapper中如果沒有cache標(biāo)簽,不會(huì)執(zhí)行上面的useNewCache方法,cache為null,就不會(huì)使用二級(jí)緩存(相當(dāng)于失效)。

查詢使用二級(jí)緩存邏輯

org.apache.ibatis.executor.CachingExecutor :

 @Override
 public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)
   throws SQLException {
  Cache cache = ms.getCache(); 
  if (cache != null) {//如果二級(jí)緩存對(duì)象不為空 嘗試在二級(jí)緩存中獲?。]有cache標(biāo)簽此對(duì)象就是空)
   flushCacheIfRequired(ms);
   if (ms.isUseCache() && resultHandler == null) {
    ensureNoOutParams(ms, boundSql);
    @SuppressWarnings("unchecked")
    List<E> list = (List<E>) tcm.getObject(cache, key); //從二級(jí)緩存中獲取數(shù)據(jù)
    if (list == null) {
     list = delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql); //如果為空,使用delegate查詢(BaseExecutor)
     tcm.putObject(cache, key, list); // 查詢結(jié)果保存到二級(jí)緩存
    }
    return list;
   }
  }
  return delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
 }

二級(jí)緩存和一級(jí)緩存不用想,數(shù)據(jù)庫(kù)的數(shù)據(jù)被修改是要清空緩存的,不然數(shù)據(jù)有誤,至于怎么清空,是另一套邏輯了,mapper中的cache標(biāo)簽可以配置一些參數(shù),比如緩存定期清空。

一級(jí)二級(jí)緩存先后順序

mybatis默認(rèn)是先查詢二級(jí)緩存,沒有,再查看一級(jí)緩存,都為空,最后查詢數(shù)據(jù)庫(kù)

以上就是詳解Mybatis的緩存的詳細(xì)內(nèi)容,更多關(guān)于Mybatis的緩存的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 舉例講解Java設(shè)計(jì)模式編程中Decorator裝飾者模式的運(yùn)用

    舉例講解Java設(shè)計(jì)模式編程中Decorator裝飾者模式的運(yùn)用

    這篇文章主要介紹了Java設(shè)計(jì)模式編程中Decorator裝飾者模式的運(yùn)用,裝飾者模式就是給一個(gè)對(duì)象動(dòng)態(tài)的添加新的功能,裝飾者和被裝飾者實(shí)現(xiàn)同一個(gè)接口,裝飾者持有被裝飾者的實(shí)例,需要的朋友可以參考下
    2016-05-05
  • MyBatis 核心配置文件及映射文件詳解

    MyBatis 核心配置文件及映射文件詳解

    MyBatis是支持定制化SQL、存儲(chǔ)過程以及高級(jí)映射的優(yōu)秀的持久層框架,本文重點(diǎn)介紹MyBatis 核心配置文件及映射文件,需要的朋友可以參考下
    2023-01-01
  • Java實(shí)習(xí)打卡8道面試題

    Java實(shí)習(xí)打卡8道面試題

    臨近秋招,備戰(zhàn)暑期實(shí)習(xí),祝大家每天進(jìn)步億點(diǎn)點(diǎn)!本篇文章準(zhǔn)備了十道java的常用面試題,希望能夠給大家提供幫助,最后祝大家面試成功,進(jìn)入自己心儀的大廠
    2021-06-06
  • 解決spring data jpa 批量保存更新的問題

    解決spring data jpa 批量保存更新的問題

    這篇文章主要介紹了解決spring data jpa 批量保存更新的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • SpringBoot程序打包失敗(.jar中沒有主清單屬性)

    SpringBoot程序打包失敗(.jar中沒有主清單屬性)

    在學(xué)習(xí)SpringBoot,打包SpringBoot程序后,在cmd運(yùn)行出現(xiàn)了 某某某.jar中沒有注清單屬性,本文就來介紹一下原因以及解決方法,感興趣的可以了解一下
    2023-06-06
  • 通過xml配置SpringMVC注解DispatcherServlet初始化過程解析

    通過xml配置SpringMVC注解DispatcherServlet初始化過程解析

    這篇文章主要為大家介紹了通過xml配置SpringMVC注解DispatcherServlet初始化過程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • springboot實(shí)現(xiàn)增加黑名單和白名單功能

    springboot實(shí)現(xiàn)增加黑名單和白名單功能

    本文主要介紹了springboot實(shí)現(xiàn)增加黑名單和白名單功能,就是單純的實(shí)現(xiàn)filter,然后注冊(cè)到springboot里面,在filter里面進(jìn)行黑白名單的篩選,感興趣的可以了解一下
    2024-05-05
  • k8s部署java項(xiàng)目的實(shí)現(xiàn)

    k8s部署java項(xiàng)目的實(shí)現(xiàn)

    本文主要介紹了k8s部署java項(xiàng)目的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • Java+element實(shí)現(xiàn)excel的導(dǎo)入和導(dǎo)出

    Java+element實(shí)現(xiàn)excel的導(dǎo)入和導(dǎo)出

    本文主要介紹了Java+element實(shí)現(xiàn)excel的導(dǎo)入和導(dǎo)出,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • SpringMVC如何自定義響應(yīng)的HTTP狀態(tài)碼

    SpringMVC如何自定義響應(yīng)的HTTP狀態(tài)碼

    這篇文章主要介紹了SpringMVC如何自定義響應(yīng)的HTTP狀態(tài)碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11

最新評(píng)論

镇坪县| 阳江市| 莱芜市| 大方县| 湟中县| 浦江县| 虞城县| 乐平市| 遂溪县| 罗定市| 婺源县| 原阳县| 集贤县| 宿松县| 大庆市| 疏勒县| 丁青县| 眉山市| 云梦县| 馆陶县| 三亚市| 武城县| 中西区| 芦山县| 瑞安市| 禄劝| 彭阳县| 兴仁县| 柳河县| 定西市| 遂溪县| 图木舒克市| 江源县| 老河口市| 清水河县| 宁乡县| 永靖县| 土默特右旗| 芦山县| 龙胜| 即墨市|