在线电影项目介绍 (5)

img

我们可以看到,在修改操作后执行的相同查询,查询了数据库,一级缓存失效

实验3

开启两个 SqlSession,在 sqlSession1 中查询数据,使一级缓存生效,在sqlSession2 中更新数据库,验证一级缓存只在数据库会话内部共享。

@Test public void testLocalCacheScope() throws Exception { SqlSession sqlSession1 = factory.openSession(true); SqlSession sqlSession2 = factory.openSession(true); StudentMapper studentMapper = sqlSession1.getMapper(StudentMapper.class); StudentMapper studentMapper2 = sqlSession2.getMapper(StudentMapper.class); System.out.println("studentMapper读取数据: " + studentMapper.getStudentById(1)); System.out.println("studentMapper读取数据: " + studentMapper.getStudentById(1)); System.out.println("studentMapper2更新了" + studentMapper2.updateStudentName("小岑",1) + "个学生的数据"); System.out.println("studentMapper读取数据: " + studentMapper.getStudentById(1)); System.out.println("studentMapper2读取数据: " + studentMapper2.getStudentById(1)); }

img

sqlSession2 更新了id为1的学生的姓名,从凯伦改为了小岑,但 session1 之后的查询中,id为1的学生的名字还是凯伦,出现了脏数据,也证明了之前的设想,一级缓存只在数据库会话内部共享。

二级缓存 二级缓存介绍

在上文中提到的一级缓存中,其最大的共享范围就是一个 SqlSession 内部,如果多个 SqlSession之间需要共享缓存,则需要使用到二级缓存。开启二级缓存后,会使用 CachingExecutor 装饰Executor,进入一级缓存的查询流程前,先在 CachingExecutor 进行二级缓存的查询,具体的工作流程如下所示。

img

二级缓存开启后,同一个 namespace 下的所有操作语句,都影响着同一个 Cache,即二级缓存被多个 SqlSession 共享,是一个全局的变量。

当开启缓存后,数据的查询执行的流程就是 二级缓存 -> 一级缓存 -> 数据库。

二级缓存配置

要正确的使用二级缓存,需完成如下配置的。

在MyBatis的配置文件中开启二级缓存。

<setting value="true"/>

在 MyBatis 的映射 XML 中配置 cache 或者 cache-ref 。

cache标签用于声明这个 namespace 使用二级缓存,并且可以自定义配置。

<cache/>

type:cache使用的类型,默认是 PerpetualCache,这在一级缓存中提到过。

eviction: 定义回收的策略,常见的有 FIFO,LRU。

flushInterval: 配置一定时间自动刷新缓存,单位是毫秒。

size: 最多缓存对象的个数。

readOnly: 是否只读,若配置可读写,则需要对应的实体类能够序列化。

blocking: 若缓存中找不到对应的key,是否会一直blocking,直到有对应的数据进入缓存。

cache-ref 代表引用别的命名空间的Cache配置,两个命名空间的操作使用的是同一个 Cache。

<cache-ref namespace="mapper.StudentMapper"/> 二级缓存实验

接下来我们通过实验,了解 MyBatis 二级缓存在使用上的一些特点。

在本实验中,id 为1的学生名称初始化为点点。

实验1

测试二级缓存效果,不提交事务,sqlSession1 查询完数据后,sqlSession2 相同的查询是否会从缓存中获取数据。

@Test public void testCacheWithoutCommitOrClose() throws Exception { SqlSession sqlSession1 = factory.openSession(true); SqlSession sqlSession2 = factory.openSession(true); StudentMapper studentMapper = sqlSession1.getMapper(StudentMapper.class); StudentMapper studentMapper2 = sqlSession2.getMapper(StudentMapper.class); System.out.println("studentMapper读取数据: " + studentMapper.getStudentById(1)); System.out.println("studentMapper2读取数据: " + studentMapper2.getStudentById(1)); }

执行结果:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsfxdx.html