缓存
1. 简介
查询要连接数据库,耗资源
一次查询的结果,可以给他暂存在一个可以直接取到的地方! --> 内存 : 缓存
我们再次查询相同数据的时候,直接走缓存,就不用走数据库了
-
什么是缓存 [ Cache ]?
- 存在内存中的临时数据。
- 将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高查询效率,解决了高并发系统的性能问题。
-
为什么使用缓存?
- 减少和数据库的交互次数,减少系统开销,提高系统效率。
-
什么样的数据能使用缓存?
- 经常查询并且不经常改变的数据。【可以使用缓存】
2. Mybatis缓存
- MyBatis包含一个非常强大的查询缓存特性,它可以非常方便地定制和配置缓存。缓存可以极大的提升查询效率。
- MyBatis系统中默认定义了两级缓存:一级缓存和二级缓存
-
默认情况下,只有一级缓存开启。(SqlSession级别的缓存,也称为本地缓存)
-
二级缓存需要手动开启和配置,他是基于namespace级别的缓存。
-
为了提高扩展性,MyBatis定义了缓存接口Cache。我们可以通过实现Cache接口来自定义二级缓存
-
3. 一级缓存
测试步骤:
- 开启日志
<!--标准的日志工厂实现-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
- 测试在一个Sesion中查询两次相同记录
package com.wang.dao;
import com.wang.pojo.User;
import com.wang.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class MyTest {
@Test
public void TestQueryUserById() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.queryUserById(1);
System.out.println(user);
System.out.println("==============================");
User user1 = mapper.queryUserById(1);
System.out.println(user1);
sqlSession.close();
}
}
-
查看日志输出
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter. PooledDataSource forcefully closed/removed all connections. PooledDataSource forcefully closed/removed all connections. PooledDataSource forcefully closed/removed all connections. PooledDataSource forcefully closed/removed all connections. Opening JDBC Connection Created connection 130668770. ==> Preparing: select * from user where id = ? ==> Parameters: 1(Integer) <== Columns: id, name, pwd <== Row: 1, 张三, 123456 <== Total: 1 User(id=1, name=张三, pwd=123456) ============================== User(id=1, name=张三, pwd=123456) Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@7c9d8e2] Returned connection 130668770 to pool. Process finished with exit code 0
缓存失效的情况:
-
查询不同的东西
-
增删改操作,可能会改变原来的数据,所以必定会刷新缓存!
-
查询不同的Mapper.xml
-
手动清理缓存!
sqlSession.clearCache();
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter. PooledDataSource forcefully closed/removed all connections. PooledDataSource forcefully closed/removed all connections. PooledDataSource forcefully closed/removed all connections. PooledDataSource forcefully closed/removed all connections. Opening JDBC Connection Created connection 20049680. ==> Preparing: select * from user where id = ? ==> Parameters: 1(Integer) <== Columns: id, name, pwd <== Row: 1, 张三, 123456 <== Total: 1 User(id=1, name=张三, pwd=123456) ============================== ==> Preparing: select * from user where id = ? ==> Parameters: 1(Integer) <== Columns: id, name, pwd <== Row: 1, 张三, 123456 <== Total: 1 User(id=1, name=张三, pwd=123456) Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@131ef10] Returned connection 20049680 to pool.
小结:
-
一级缓存默认是开启的,只在一次SqlSession中有效,也就是拿到连接到关闭连接这个区间段
-
一级缓存就是一个Map
4. 二级缓存
- 二级缓存也叫全局缓存,一级缓存作用域太低了,所以诞生了二级缓存
- 基于namespace级别的缓存,一个名称空间,对应一个二级缓存;
- 工作机制
- 一个会话查询一条数据,这个数据就会被放在当前会话的一级缓存中;
- 如果当前会话关闭了,这个会话对应的一级缓存就没了;但是我们想要的是,会话关闭了,一级缓存中的数据被保存到二级缓存中;
- 新的会话查询信息,就可以从二级缓存中获取内容;
- 不同的mapper查出的数据会放在自己对应的缓存(map)中;
步骤:
-
开启全局缓存
<!--开启全局缓存--> <setting name="cacheEnabled" value="true"/>
-
在要使用二级缓存的Mapper中开启
<!--在当前Mapper.xml中使用二级缓存--> <cache/>
也可以自定义参数
<!--在当前Mapper.xml中使用二级缓存--> <cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
-
测试
问题:
- 我们需要将实体类序列化!否则就会报错
小结:
- 只要开启了二级缓存,在同一个Mapper下就有效
- 所有的数据都会先放在一级缓存中
- 只有当会话提交,或者会话关闭的时候,才会提交到二级缓存中!
5. 缓存原理
缓存顺序:
- 先看二级缓存中有没有
- 再看一级缓存中有没有
- 一级缓存中也没有,则查询数据库
6. 自定义缓存-ehcache
Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存
要在程序中使用Ehcache,首先要导包(Mybatis Ehcache)
<!-- https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache -->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.2.1</version>
</dependency>