mybatis 源码分析(四)一二级缓存分析 (3)

mybatis 源码分析(四)一二级缓存分析

LRU – 最近最少使用:移除最长时间不被使用的对象。

FIFO – 先进先出:按对象进入缓存的顺序来移除它们。

SOFT – 软引用:基于垃圾回收器状态和软引用规则移除对象。

WEAK – 弱引用:更积极地基于垃圾收集器状态和弱引用规则移除对象。

但是需要注意的是这里的策略也能用户本地缓存,对于分布式缓存有些策略还是有问题;比如:

<cache eviction="FIFO" flushInterval="60000" size="2" readOnly="true"/>

这里主要定义了缓存大小2,使用 FIFO 策略更新;

@Test public void test04() { SqlSessionFactory sqlSessionFactory = DBUtils.getSessionFactory(); try ( SqlSession sqlSession1 = sqlSessionFactory.openSession(true); SqlSession sqlSession2 = sqlSessionFactory.openSession(true);) { UserMapper userMapper1 = sqlSession1.getMapper(UserMapper.class); UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class); System.out.println("---get user: " + userMapper1.getUser(1L)); sqlSession1.commit(); System.out.println("---get user: " + userMapper1.getUser(2L)); sqlSession1.commit(); System.out.println("---get user: " + userMapper1.getUser(3L)); sqlSession1.commit(); System.out.println("---get user: " + userMapper2.getUser(1L)); System.out.println("---get user: " + userMapper2.getUser(2L)); System.out.println("---get user: " + userMapper1.getUser(1L)); sqlSession2.commit(); System.out.println("------------"); System.out.println("---get user: " + userMapper1.getUser(1L)); System.out.println("---get user: " + userMapper1.getUser(2L)); System.out.println("---get user: " + userMapper1.getUser(3L)); } }

打印:

DEBUG [main] - Cache Hit Ratio [sanzao.db.UserMapper]: 0.0 DEBUG [main] - ==> Preparing: select * from user where id = ? DEBUG [main] - ==> Parameters: 1(Long) DEBUG [main] - <== Total: 1 ---get user: User{id=1, user_name='s1', password='123456', address='TT'} DEBUG [main] - Cache Hit Ratio [sanzao.db.UserMapper]: 0.0 DEBUG [main] - ==> Preparing: select * from user where id = ? DEBUG [main] - ==> Parameters: 2(Long) DEBUG [main] - <== Total: 1 ---get user: User{id=2, user_name='s2', password='123456', address='TT'} DEBUG [main] - Cache Hit Ratio [sanzao.db.UserMapper]: 0.0 DEBUG [main] - ==> Preparing: select * from user where id = ? DEBUG [main] - ==> Parameters: 3(Long) DEBUG [main] - <== Total: 1 ---get user: User{id=3, user_name='s3', password='123456', address='TT'} DEBUG [main] - Cache Hit Ratio [sanzao.db.UserMapper]: 0.0 DEBUG [main] - ==> Preparing: select * from user where id = ? DEBUG [main] - ==> Parameters: 1(Long) DEBUG [main] - <== Total: 1 ---get user: User{id=1, user_name='s1', password='123456', address='TT'} DEBUG [main] - Cache Hit Ratio [sanzao.db.UserMapper]: 0.2 ---get user: User{id=2, user_name='s2', password='123456', address='TT'} DEBUG [main] - Cache Hit Ratio [sanzao.db.UserMapper]: 0.16666666666666666 DEBUG [main] - ==> Preparing: select * from user where id = ? DEBUG [main] - ==> Parameters: 1(Long) DEBUG [main] - <== Total: 1 ---get user: User{id=1, user_name='s1', password='123456', address='TT'} ------------ DEBUG [main] - Cache Hit Ratio [sanzao.db.UserMapper]: 0.2857142857142857 ---get user: User{id=1, user_name='s1', password='123456', address='TT'} DEBUG [main] - Cache Hit Ratio [sanzao.db.UserMapper]: 0.25 DEBUG [main] - ==> Parameters: 2(Long) DEBUG [main] - <== Total: 1 ---get user: User{id=2, user_name='s2', password='123456', address='TT'} DEBUG [main] - Cache Hit Ratio [sanzao.db.UserMapper]: 0.3333333333333333 ---get user: User{id=3, user_name='s3', password='123456', address='TT'}

从日志中可以看到对于 SqlSession1,大小2,FIFO 是生效的,但是 SqlSession2 提交了之后,就发现缓存 s1,s2,s3 都命中了;

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

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