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 ...
随机推荐
- JSP页面无法识别EL表达式
昨天一直纠结一个问题,JSP页面无法获取${user}的值,一直显示的是${user},今天解决了,原来是JSP页面无法识别EL表达式. 我的web.xml的声明如下: <!DOCTYPE we ...
- 验证网站SiteMap的工具
验证网站SiteMap的在线工具 http://www.xmlvalidation.com/ 在SiteMap文件中,不能直接这样写url "http://www.obriensplast ...
- jQuery获取table当前所在行
$("div tbody tr").click(function() { var rows = $(this).prevAll().length + 1;//行号 ...
- 细说CSS中的display属性
相信大部分奋战在前端的,尤其在前端攻城的过程中,有一种越陷越深的感觉,不错,一如前端深似海,从此妹子是浮云啊,前端上手容易,深入难啊!下面我就CSS中的display属性讲下我自己所积累的,与大家共享 ...
- C#中的explicit和implicit了解一下吧
今天在研究公司项目框架的时候看到了下面的用法,public static implicit operator JsonData(int data);.貌似很久没用过这种隐式转换的写法了,因此重新温习一 ...
- CodeForces 644B【模拟】
题意: 查询数 和 最大的队列容量+1: 按时间顺序 ti代表,第i个出线的时间: di代表,第i个需要处理的时间: 对于第i个输出他所需要的时间完成,或者拒绝进入输出-1: 思路: 真是MDZZ了, ...
- C 语言实例 - 查找字符在字符串中出现的次数
C 语言实例 - 查找字符在字符串中出现的次数 C 语言实例 C 语言实例 查找字符在字符串中的起始位置(索引值从 开始). 实例 #include <stdio.h> int main( ...
- sequence(2018.10.23)
建出差分序列,可以发现最早出现的回文串就是答案,自己想想就懂了. \(O(N)\)找出回文串就好了,字符串\(hash\)或者\(manacher\)都能在合法时间内得到答案. #include< ...
- E - Multiplication Puzzle
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...
- JS中一个new到底做了哪些事情?
1.https://www.cnblogs.com/faith3/p/6209741.html 2.https://www.cnblogs.com/AaronNotes/p/6529492.html