博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mybatis 源码学习(七) 缓存的刷新
阅读量:4216 次
发布时间:2019-05-26

本文共 3128 字,大约阅读时间需要 10 分钟。

之前学习了mybatis的一级缓存和二级缓存。

那么当数据库数据有跟新的时候,缓存是如何被刷新的呢?

找到update的实现源码:

public int update(String statement, Object parameter) {    try {      dirty = true;      MappedStatement ms = configuration.getMappedStatement(statement);      return executor.update(ms, wrapCollection(parameter));    } catch (Exception e) {      throw ExceptionFactory.wrapException("Error updating database.  Cause: " + e, e);    } finally {      ErrorContext.instance().reset();    }  }

看来是在executor中清理的。

public class CachingExecutor implements Executor {  public int update(MappedStatement ms, Object parameterObject) throws SQLException {    flushCacheIfRequired(ms); //这里清空了二级缓存    return delegate.update(ms, parameterObject);  }  private void flushCacheIfRequired(MappedStatement ms) {    Cache cache = ms.getCache();    if (cache != null && ms.isFlushCacheRequired()) {      dirty = true; // issue #524. Disable using cached data for this session      tcm.clear(cache);    }  }}
public abstract class BaseExecutor implements Executor {  public int update(MappedStatement ms, Object parameter) throws SQLException {    ErrorContext.instance().resource(ms.getResource()).activity("executing an update").object(ms.getId());    if (closed) throw new ExecutorException("Executor was closed.");    clearLocalCache();//这里清空一级缓存    return doUpdate(ms, parameter);  }}
public void clearLocalCache() {    if (!closed) {      localCache.clear();      localOutputParameterCache.clear();    }  }

一级缓存比较简单清晰,同一个sqlSession执行查询的时候缓存,再执行相同查的时候使用缓存,当执行修改操作的时候,直接刷新整个sqlSession的缓存(在Executer上的localCache)。

二级缓存的更新一直让我有点困惑,缓存是在MappedStatement上的,怎么能在修改的时候把其他的查询缓存清除呢,他们是在不同的MappedStatement上的啊?

先看查询的时候是怎么放到缓存的

@Override  public 
List
query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException { Cache cache = ms.getCache(); if (cache != null) { flushCacheIfRequired(ms); if (ms.isUseCache() && resultHandler == null) { ensureNoOutParams(ms, parameterObject, boundSql); @SuppressWarnings("unchecked") List
list = (List
) tcm.getObject(cache, key); if (list == null) { list = delegate.
query(ms, parameterObject, rowBounds, resultHandler, key, boundSql); tcm.putObject(cache, key, list); // issue #578 and #116 } return list; } } return delegate.
query(ms, parameterObject, rowBounds, resultHandler, key, boundSql); } public void putObject(Cache cache, CacheKey key, Object value) { getTransactionalCache(cache).putObject(key, value); } private TransactionalCache getTransactionalCache(Cache cache) { TransactionalCache txCache = transactionalCaches.get(cache); if (txCache == null) { txCache = new TransactionalCache(cache); transactionalCaches.put(cache, txCache); } return txCache; }

从以上的三个方法可以看到,是通过TransactionalCacheManager来放入缓存的。在放入mappedStatement的缓存同时,还被存入了executror下的tcm属性的transactionalCaches对象内容。这是有点绕。如下图所看到的,通过层层代理,最终看到id是mapper的名字。这样其他的SqlSession在查询的时候先查缓存中是否已经存在。刷新的时候也是按照mapper来刷新的。大功告成!

你可能感兴趣的文章
cocos2dx lua Node节点 私有数据存取
查看>>
lua math.ceil math.ceil
查看>>
cocos2dx CCNode计算node的大小
查看>>
cocos2dx 布局记录(1)
查看>>
lua 多行注释和取消多行注释
查看>>
缩放系数计算
查看>>
cocos2dx --- 按钮点击居中放大
查看>>
cocos2dx menu位置计算
查看>>
cocos2dx资源加载机制(同步/异步)
查看>>
cocos2dx C++调用java -- 字符串传递
查看>>
git学习网站
查看>>
JavaScript 学习网站
查看>>
cocos2dx java调用c++ -- 字符串传递
查看>>
CCScaleTo与CCScaleBy比较
查看>>
cocos2dx CCObject引用计数,内存释放分析(1)
查看>>
cocos2dx2.X 编译时,传递编译选项
查看>>
ccCArray.cpp 文件
查看>>
cocos2dx 屏幕大小
查看>>
libgdx: 2D Particle Editor工具使用
查看>>
eclipse 给jar库添加源码
查看>>