前置
会演示二级缓存生效/失效的场景
项目地址: https://gitee.com/xmaxm/chaim-code-template/blob/master/chaim-cache/chaim-mybatis-cache/chaim-mybatis-cache-two/README.md
前置配置:
本篇文章是基于上篇文章进行: https://blog.csdn.net/qq_38637558/article/details/127924334
注意点
官网说明: 如清除策略、可读或可读写等,不能应用于自定义缓存。https://mybatis.org/mybatis-3/zh/sqlmap-xml.html#cache
源码部分
感觉要是把源码过一遍, 得从新起一篇文章才行, 后面有需要在写, 偷个懒吧, 哈哈哈哈哈!
源码入口: org.apache.ibatis.mapping.CacheBuilder#build
关键类: org.apache.ibatis.cache.Cache
实现类: org.mybatis.caches.redis.RedisCache
淘汰策略: 没得
相关缓存文章
Mybatis的一级缓存
Mybatis的二级缓存 (默认方式)
Mybatis的二级缓存 (Redis方式)
Mybatis的二级缓存 (ehcache方式)
pom: jar
此包源码很简单, 建议看一下, 也就是实现了Cache接口而已
<!-- 自定义二级缓存存储: redis方式 -->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-redis</artifactId>
<version>1.0.0-beta2</version>
<!-- 不传递依赖 -->
<!-- optional 的值有 true 和 false 可选 -->
<optional>true</optional>
</dependency>
配置文件: redis.properties
# Redis缓存配置
# 源码对应 org.mybatis.caches.redis.RedisCache
host=127.0.0.1
port=6379
connectionTimeout=2000
password=
database=2
配置指定方式
配置:
@CacheNamespace(implementation = RedisCache.class)
或 <cache type=“org.mybatis.caches.redis.RedisCache”/>
备注:
@CacheNamespace(implementation = RedisCache.class)
对应的xml文件:
<cache-ref namespace=“com.chaim.mybatis.cache.two.mappers.SysUserMapper”/>
---------------------------------------
@CacheNamespaceRef(value = SysUserMapper.class)
对应的xml文件:
<cache type=“org.mybatis.caches.redis.RedisCache”/>
@CacheNamespace(implementation = RedisCache.class)
public interface SysUserMapper extends BaseMapper<SysUser> {
}
<cache type="org.mybatis.caches.redis.RedisCache"/>
效果图