Mybatis Cache 缓存策略

正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持

  1. 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Session,当 Session flush 或 close 之后,该Session中的所有 Cache 就将清空。
  2. 二级缓存与一级缓存其机制相同,默认也是采用 PerpetualCache,HashMap存储,不同在于其存储作用域为 Mapper(Namespace),并且可自定义存储源,如 Ehcache。
  3. 对于缓存数据更新机制,当某一个作用域(一级缓存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属性.

  1. 映射语句文件中的所有select语句将会被缓存。

  2. 映射语句文件中的所有insert,update和delete语句会刷新缓存。

  3. 缓存会使用Least Recently Used(LRU,最近最少使用的)算法来收回。

  4. 缓存会根据指定的时间间隔来刷新。

  5. 缓存会存储1024个对象

Mybatis Cache 缓存策略的更多相关文章

  1. mybatis 使用缓存策略

    mybatis中默认开启缓存 1.mybatis中,默认是开启缓存的,缓存的是一个statement对象. 不同情况下是否会使用缓存 同一个SqlSession对象,重复调用同一个id的<sel ...

  2. MyBatis缓存策略

    MyBatis 提供了一级缓存和二级缓存策略,一级缓存是作用在SqlSession级别上的,而二级缓存则是作用在Mapper级别上的( 即作用在 namespace上),MyBatis 默认是开启的一 ...

  3. ASIHTTPRequest缓存策略download cache

    本文为大家介绍了iOS开发ASIHTTPRequest使用download cache的内容,其中包括cache策略,存储策略,其他cache相关的特性,编写自己的cache等等内容. 从1.8版本开 ...

  4. Mybatis 缓存策略

    听极客学院笔记 使用mybatis的缓存需要以下三步 一.在mybatis的config.xml中开启缓存 <settings> <setting name="cacheE ...

  5. ASP.NET Cache 实现依赖Oracle的缓存策略

    ASP.NET 中的缓存提供了对SQL依赖项的支持,也就是说当SQL SERVER数据库中的表或行中的数据被更改后,缓存中的页面就失效,否则,页面输出可一直保留在缓存当中.这确实为程序员提供了方便.但 ...

  6. mybatis 自定义缓存 cache

    缓存不管哪个框架都是显得特别的重要,今天自己测试实现了mybatis自定义缓存,从而理解mybatis缓存的工作原理. 首先缓存类要实现Cache接口:具体实现如下package com.ibatis ...

  7. MyBatis Cache配置

    @(MyBatis)[Cache] MyBatis Cache配置 MyBatis提供了一级缓存和二级缓存 配置 全局配置 配置 说明 默认值 可选值 cacheEnabled 全局缓存的开关 tru ...

  8. Spring+SpringMVC+MyBatis深入学习及搭建(八)——MyBatis查询缓存

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6956206.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(七)——My ...

  9. mybatis一级缓存二级缓存

    一级缓存 Mybatis对缓存提供支持,但是在没有配置的默认情况下,它只开启一级缓存,一级缓存只是相对于同一个SqlSession而言.所以在参数和SQL完全一样的情况下,我们使用同一个SqlSess ...

随机推荐

  1. ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 27. CICD Azure DevOps

    VSTS做持续集成 后来改名叫做Azure Deveps https://azure.microsoft.com/zh-cn/services/devops/ 这是中文的地址 创建一个项目 名称.描述 ...

  2. AndroidStudio关联GitHub

    1.前提: 1.已有github账号和密码 github官方网站:https://github.com/ 2.下载了git客户端 客户端下载地址:http://pan.baidu.com/s/1slV ...

  3. hdu1848(sg函数打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1848 题意:中文题诶- 思路:直接sg函数打表就好了 代码: #include <iostrea ...

  4. 【BZOJ1226】[SDOI2009] 学校食堂

    题目描述 小F 的学校在城市的一个偏僻角落,所有学生都只好在学校吃饭.学校有一个食堂,虽然简陋,但食堂大厨总能做出让同学们满意的菜肴.当然,不同的人口味也不一定相同,但每个人的口味都可以用一个非负整数 ...

  5. 33、JSONP跨域

    跨域请求数据也是可以的,只不过Ajax这技术不行,而JSONP这种数据格式可以进行跨域.很多年前,浏览器是没有跨域限制的,可以正常跨域,浏览器为了安全和隐私限制了Ajax跨域 JSONP原理就是:将函 ...

  6. Libre OJ P2332「JOI 2017 Final」焚风现象【差分思想】By cellur925

    题目传送门 这道题开始看起来会很晕...\(qwq\).首先我们要明确题目中的海拔&&温度.温度是受海拔影响的,每次改变的是海拔,我们求的是温度. 我们开始读入的时候便可以处理出开始\ ...

  7. LVS-DR VIP和RIP不同网段的配置方法

    http://blog.itpub.net/25723371/viewspace-1446935/

  8. nginx,tomcat,apache三者分别用来做什么,有何区别

    1. Nginx和tomcat的区别 nginx常用做静态内容服务和代理服务器,直接外来请求转发给后面的应用服务器(tomcat,Django等),tomcat更多用来做一个应用容器,让java we ...

  9. POJ-1020-Anniversary Cake

    链接:https://vjudge.net/problem/POJ-1020 题意: 给一个宽为s的正方形,再给n个变长为an的小正方形, 判断是否能将这n个小正方形完全填充到这个大正方形里面. 思路 ...

  10. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) B

    Sherlock has a new girlfriend (so unlike him!). Valentine's day is coming and he wants to gift her s ...