缓存
简介
什么是缓存?
- 存在内存中的临时数据
- 将用户经常查询的数据放在缓存中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高查询效率,解决了高并发系统的性能问题。
为什么使用缓存?
- 减少和数据库的交互次数,减少系统开销,提高系统效率
什么样的数据能使用缓存?
- 经常查询并且不经常改变的数据
Mybatis缓存
- Mybatis包含一个非常强大的查询缓存特性,它可以非常方便地定制和配置缓存。缓存可以极大的提升查询效率
- Mybatis系统中默认定义了两级缓存:一级缓存和二级缓存
默认情况下,只开启一级缓存(SqlSession级别的缓存,也称本地缓存)
二级缓存需要手动开启和配置,他是基于namespace级别的缓存
为了提高扩展性,Mybatis定义了缓存接口Cache。可以通过实现Cache接口来定义二级缓存
一级缓存
一级缓存也叫本地缓存:
- 与数据库同一次会话期间查询到的数据会放到本地缓存中
- 以后如果需要获取相同的数据,直接从缓存中拿,没必要再去查询数据库
测试步骤:
- 开启日志
- 测试在一个Session中查询两次相同记录
- 查看日志输出
缓存失效情况:
- 查询不同的数据
- 增删改操作,可能会改变原来的数据,所以必定会刷新缓存
- 查询不同的Mapper.xml
- 手动清理缓存
一句缓存默认是开启的,只在一次SqlSession中游戏走,也就是拿到连接到关闭连接这个区间段。
一级缓存就是一个Map
二级缓存
二级缓存也叫全局缓存,一级缓存作用域太低了,所以诞生了二级缓存
基于namespace级别的缓存,一个名称空间,对应一个二级缓存
工作机制:
- 一个绘画查询一条数据,这个数据就会被放再当前会话的一级缓存中
- 如当前会话关闭了,这个会话对应的一级缓存就没了;但是想要的情况是,会话关闭了,一级缓存的数据被保存到二级缓存
- 新的会话查询信息,就可以从二级缓存中获取内容
- 不同的mapper查出的数据会放在自己对应的缓存(map)中
步骤:
- 开启全局缓存
<!-- 显示的开启全局缓存 -->
<setting name="cacheEnabled" value="true"/>
- 在要使用的二级缓存的Mapper.xml中开启
<!--在当前Mapper.xml中使用二级缓存-->
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
- 测试
1.问题:我么需要将实体类序列化!
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private int id;
private String name;
private String pwd;
}
- 测试
public class MyTest {
@Test
public void test() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.queryUserById(2);
System.out.println(user);
sqlSession.close();
SqlSession sqlSession2 = MybatisUtils.getSqlSession();
UserMapper mapper2 = sqlSession2.getMapper(UserMapper.class);
User user2 = mapper2.queryUserById(3);
System.out.println(user2);
sqlSession2.close();
}
}
- 只要开启了二级缓存,在同一Mapper下就有效
- 所有数据都会先放到一级缓存中
- 只有当会话提交,或者关闭时,才会提交到二级缓存中
缓存原理

自定义缓存--Ehcache
一种广泛使用的开源Java分布式缓存。主要面向通用缓存
要使用,先导包
<!-- Source: https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache -->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.2.1</version>
<scope>compile</scope>
</dependency>
在mapper中指定使用我们的ehcache缓存实现
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.god.dao.UserMapper">
<!--在当前Mapper.xml中使用二级缓存-->
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
<select id="queryUserById" resultType="user">
select * from user where id = #{id}
</select>
</mapper>
<ehcache.xml>
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="./tmpdir/Tmp_EhCache"/>
<defaultCache
eternal="false"
maxElementsInMemory="10000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="259200"
memoryStoreEvictionPolicy="LRU"/>
<cache
name="cloud_user"
eternal="false"
maxElementsInMemory="5000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="1800"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
Redis数据库用来做缓存
浙公网安备 33010602011771号