Mybatis Cache 缓存策略
Mybatis Cache 缓存策略
正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持
- 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Session,当 Session flush 或 close 之后,该Session中的所有 Cache 就将清空。
- 二级缓存与一级缓存其机制相同,默认也是采用 PerpetualCache,HashMap存储,不同在于其存储作用域为 Mapper(Namespace),并且可自定义存储源,如 Ehcache。
- 对于缓存数据更新机制,当某一个作用域(一级缓存Session/二级缓存Namespaces)的进行了 C/U/D 操作后,默认该作用域下所有 select 中的缓存将被clear。
一级缓存的验证:
@Test
public void testLevel1Cache()
{
SqlSession sqlSession = MybatisUtil.getSqlSession();
String statement = "src.lyb.resources.mapper.studentMapper.getStudent";
Student student1 = sqlSession.selectOne(statement,1);
System.out.println(student1);
student1 = sqlSession.selectOne(statement, 1);
System.out.println(student1);
sqlSession.close();
sqlSession = MybatisUtil.getSqlSession();
student1 = sqlSession.selectOne(statement, 1);
System.out.println(student1);
student1 = sqlSession.selectOne(statement, 4);
System.out.println(student1);
student1 = sqlSession.selectOne(statement, 4);
System.out.println(student1);
sqlSession.update("src.lyb.resources.mapper.studentMapper.updateStudent",
new Student(4,"tmd", 50, "angry"));
student1 = sqlSession.selectOne(statement, 4);
System.out.println(student1);
}
这里得到的执行结果是:
Student{student_id=1, student_name='lyb', student_grade=100, student_issue_name='lazy'}
Student{student_id=1, student_name='lyb', student_grade=100, student_issue_name='lazy'}
Student{student_id=1, student_name='lyb', student_grade=100, student_issue_name='lazy'}
Student{student_id=4, student_name='lll', student_grade=22, student_issue_name='angry'}
Student{student_id=4, student_name='lll', student_grade=22, student_issue_name='angry'}
Student{student_id=4, student_name='tmd', student_grade=50, student_issue_name='angry'}
而真实数据库的内容是:
1 lyb 100 lazy
4 lll 22 angry
可以看出数据库和查询的结果不一致了, 如果做了sqlSession的commit()或者是close(), 数据库才会和返回值一致.
二级缓存的验证
开启二级缓存,在studentMapper.xml文件中添加如下配置
<mapper namespace="src.lyb.resources.mapper.studentMapper">
<cache/>
测试文件为:
@Test
public void testLevel2Cache()
{
String statement = "src.lyb.resources.mapper.studentMapper.getStudent";
SqlSessionFactory factory = MybatisUtil.getSqlSessionFactoy();
//开启两个不同的SqlSession
SqlSession sqlSession1 = factory.openSession();
SqlSession sqlSession2 = factory.openSession();
Student student = sqlSession1.selectOne(statement, 1);
sqlSession1.commit(); //不懂为啥,这个地方一定要提交事务之后二级缓存才会起作用
System.out.println("user="+student);
//由于使用的是两个不同的SqlSession对象,所以即使查询条件相同,一级缓存也不会开启使用
student = sqlSession2.selectOne(statement, 1);
//session2.commit();
System.out.println("user2="+student);
}
这里比较奇怪, 如果查询过马上commit(), 其实只有一个sqlSession真正在工作, 但是如果不commit(), 就是两个session.
<cache eviction="FIFO" <!--回收策略为先进先出-->
flushInterval="60000" <!--自动刷新时间60s-->
size="512" <!--最多缓存512个引用对象-->
readOnly="true"/> <!--只读-->
常用的cache属性.
映射语句文件中的所有select语句将会被缓存。
映射语句文件中的所有insert,update和delete语句会刷新缓存。
缓存会使用Least Recently Used(LRU,最近最少使用的)算法来收回。
缓存会根据指定的时间间隔来刷新。
缓存会存储1024个对象
Mybatis Cache 缓存策略的更多相关文章
- mybatis 使用缓存策略
mybatis中默认开启缓存 1.mybatis中,默认是开启缓存的,缓存的是一个statement对象. 不同情况下是否会使用缓存 同一个SqlSession对象,重复调用同一个id的<sel ...
- MyBatis缓存策略
MyBatis 提供了一级缓存和二级缓存策略,一级缓存是作用在SqlSession级别上的,而二级缓存则是作用在Mapper级别上的( 即作用在 namespace上),MyBatis 默认是开启的一 ...
- ASIHTTPRequest缓存策略download cache
本文为大家介绍了iOS开发ASIHTTPRequest使用download cache的内容,其中包括cache策略,存储策略,其他cache相关的特性,编写自己的cache等等内容. 从1.8版本开 ...
- Mybatis 缓存策略
听极客学院笔记 使用mybatis的缓存需要以下三步 一.在mybatis的config.xml中开启缓存 <settings> <setting name="cacheE ...
- ASP.NET Cache 实现依赖Oracle的缓存策略
ASP.NET 中的缓存提供了对SQL依赖项的支持,也就是说当SQL SERVER数据库中的表或行中的数据被更改后,缓存中的页面就失效,否则,页面输出可一直保留在缓存当中.这确实为程序员提供了方便.但 ...
- mybatis 自定义缓存 cache
缓存不管哪个框架都是显得特别的重要,今天自己测试实现了mybatis自定义缓存,从而理解mybatis缓存的工作原理. 首先缓存类要实现Cache接口:具体实现如下package com.ibatis ...
- MyBatis Cache配置
@(MyBatis)[Cache] MyBatis Cache配置 MyBatis提供了一级缓存和二级缓存 配置 全局配置 配置 说明 默认值 可选值 cacheEnabled 全局缓存的开关 tru ...
- Spring+SpringMVC+MyBatis深入学习及搭建(八)——MyBatis查询缓存
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6956206.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(七)——My ...
- mybatis一级缓存二级缓存
一级缓存 Mybatis对缓存提供支持,但是在没有配置的默认情况下,它只开启一级缓存,一级缓存只是相对于同一个SqlSession而言.所以在参数和SQL完全一样的情况下,我们使用同一个SqlSess ...
随机推荐
- 1.3-1.4 hive环境部署
一. 官网:http://hive.apache.org/ 下载:http://archive.apache.org/dist/hive/ GitHub:https://github.com/apac ...
- 移动端UI资源
1.Flat UI Free http://designmodo.github.io/Flat-UI/ 2.POP 原型工具 http://mobilehub.io/products/pop 3.ei ...
- (水题)HDU - 1077 - Catching Fish - 计算几何
http://acm.hdu.edu.cn/showproblem.php?pid=1077 很明显这样的圆,必定有两个点在边界上.n平方枚举圆,再n立方暴力判断.由于没有给T,所以不知道行不行.
- Easyui TextBox 添加事件的方法
$("#txtPaySideId").textbox('textbox').bind("click", function () { showPlatform() ...
- Unity3D命令行Build
转自:http://www.cnblogs.com/gameprogram/archive/2012/05/11/2496303.html 本来是没想用这个命令行Build方式,可惜电脑不知道怎么的就 ...
- SpringBoot2.0 基础案例(02):配置Log4j2,实现不同环境日志打印
一.Log4j2日志简介 日志打印是了解Web项目运行的最直接方式,所以在项目开发中是需要首先搭建好的环境. 1.Log4j2特点 1)核心特点 相比与其他的日志系统,log4j2丢数据这种情况少:d ...
- Java程序员需要突破的技术要点
一.源码分析 源码分析是一种临界知识,掌握了这种临界知识,能不变应万变,源码分析对于很多人来说很枯燥,生涩难懂. 源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心. 我认为是阅读源码的最核心 ...
- fetch请求数据,后台将cookie一起返回时
请求时,添加以上标记的属性,就可以拿到后台给的cookie,并返回给后台.比如登录后才能有的操作,这样就需要返回给后台cookie从而判断是否登录
- 集合框架Collection<E>接口
- Java - Class版本号和UnsupportedClassVersionError
问题分析 Java是向下兼容的,每一个jdk版本都有对应的class版本号(major + minor version numbers):如果用低版本的jvm去加载高版本jdk编译的类,就会报错:ja ...