一级缓存
Mybatis对缓存提供支持,但是在没有配置的默认情况下,它只开启一级缓存,一级缓存只是相对于同一个SqlSession而言。所以在参数和SQL完全一样的情况下,我们使用同一个SqlSession对象调用一个Mapper方法,往往只执行一次SQL,因为使用SelSession第一次查询后,MyBatis会将其放在缓存中,以后再查询的时候,如果没有声明需要刷新,并且缓存没有超时的情况下,SqlSession都会取出当前缓存的数据,而不会再次发送SQL到数据库。
一级缓存的使用条件:
- 必须是相同的SQL和参数
- 必须是相同的会话
- 必须是相同的namespace 即同一个mapper
- 必须是相同的statement 即同一个mapper 接口中的同一个方法
- 查询语句中间没有执行session.clearCache() 方法
- 查询语句中间没有执行 insert update delete 方法(无论变动记录是否与 缓存数据有无关系)
一级缓存源码解析:
缓存获取 :
>mapper.mapper.selectById()
>org.apache.ibatis.session.defaults.DefaultSqlSession#selectList()
>org.apache.ibatis.executor.CachingExecutor#query()
>org.apache.ibatis.executor.BaseExecutor#query()
>org.apache.ibatis.cache.impl.PerpetualCache#getObject
缓存的存储:
>mapper.mapper.selectById()
>org.apache.ibatis.session.defaults.DefaultSqlSession#selectList()
>org.apache.ibatis.executor.CachingExecutor#query()
>org.apache.ibatis.executor.BaseExecutor#query()
>org.apache.ibatis.executor.BaseExecutor#queryFromDatabase
>org.apache.ibatis.cache.impl.PerpetualCache#putObject
通过对clearCache 作为入口我们可能追踪到 一级缓存的实现 PerpetualCache
>org.apache.ibatis.session.defaults.DefaultSqlSession#clearCache
>org.apache.ibatis.executor.CachingExecutor#clearLocalCache
>org.apache.ibatis.executor.BaseExecutor#clearLocalCache
>org.apache.ibatis.cache.impl.PerpetualCache#clear
提问:
在查询时另一个会话并发去修改查询的数据,一级缓存是否会生效?如果生效是否就会导致数据不正确?
一级缓存存储的是对象,修改会导致数据不一致
二级缓存
使用场景:
业务系统中存在很多的静态数据如,字典表、菜单表、权限表等,这些数据的特性是不会轻易修改但又是查询的热点数据。一级缓存针对的是同一个会话当中相同SQL,并不适合这情热点数据的缓存场景。为了解决这个问题引入了二级缓存,它脱离于会话之外。
示例:
@CacheNamespace()
public interface LabelMapper {
@Select("select * from t_label where id =#{id}")
Label getById(Integer id);
}
// 属性说明:
@CacheNamespace(
implementation = PerpetualCache.class, // 缓存实现 Cache接口 实现类
eviction = LruCache.class,// 缓存算法
flushInterval = 0, // 刷新间隔时间 毫秒
size = 1024, // 最大缓存引用对象
readWrite = true, // 是否可写
blocking = false // 是否阻塞
)
使用条件:
- 当会话提交或关闭之后才会填充二级缓存
- 必须是在同一个命名空间之下
- 必须是相同的statement 即同一个mapper 接口中的同一个方法
- 必须是相同的SQL语句和参数
- 如果readWrite=true ,实体对像必须实现Serializable 接口
清除条件:
- xml中配置的update 不能清空 @CacheNamespace 中的缓存数据
- 只有修改会话提交之后 才会执行清空操作
- 任何一种增删改操作 都会清空整个namespace 中的缓存
源码解析:
获取缓存关键源码:
>org.apache.ibatis.cache.TransactionalCacheManager#getObject
>org.apache.ibatis.cache.decorators.TransactionalCache#getObject
>org.apache.ibatis.cache.decorators.SynchronizedCache#getObject
>org.apache.ibatis.cache.decorators.LoggingCache#getObject
>org.apache.ibatis.cache.decorators.SerializedCache#getObject
>org.apache.ibatis.cache.decorators.ScheduledCache#getObject
>org.apache.ibatis.cache.decorators.LruCache#getObject
>org.apache.ibatis.cache.impl.PerpetualCache#getObject
保存二级缓存:
org.apache.ibatis.executor.CachingExecutor#close
>org.apache.ibatis.cache.TransactionalCacheManager#commit
>org.apache.ibatis.cache.decorators.TransactionalCache#flushPendingEntries
>org.apache.ibatis.cache.decorators.SynchronizedCache#putObject
>org.apache.ibatis.cache.decorators.LoggingCache#putObject
>org.apache.ibatis.cache.decorators.SerializedCache#putObject
>org.apache.ibatis.cache.decorators.ScheduledCache#putObject
>org.apache.ibatis.cache.decorators.LruCache#putObject
>org.apache.ibatis.cache.impl.PerpetualCache#putObject
清除缓存:
>org.apache.ibatis.session.defaults.DefaultSqlSession#selectList()
>org.apache.ibatis.executor.CachingExecutor#query()
>org.apache.ibatis.executor.CachingExecutor#query()
>org.apache.ibatis.executor.CachingExecutor#flushCacheIfRequired()