MyBatis的二级缓存整合redis

Terwer Java SE 后端开发 MyBatis评论175字数 2035阅读6分47秒阅读模式

MyBatis 自带的二级缓存存在的问题

在前面我们使用 @CacheNamespace​ 实现了 430.MyBatis的二级缓存 ,这个底层使用 HashMap​ 来实现。在 单机环境 下没有问题,但是在 分布式环境 下就不行了。文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-s-secondary-cache-integrated-redis-z9qyjo.html

MyBatis的二级缓存整合redis文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-s-secondary-cache-integrated-redis-z9qyjo.html

MyBatis 二级缓存在分布式环境下的问题解决

为了解决这个问题,可以使用 分布式缓存 保存 MyBatis 二级缓存的数据。文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-s-secondary-cache-integrated-redis-z9qyjo.html

MyBatis的二级缓存整合redis文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-s-secondary-cache-integrated-redis-z9qyjo.html

怎么自定义 MyBatis 的二级缓存

可以在 @CacheNamespace​ 上面加上 implementation , 例如,默认的缓存可以写成:文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-s-secondary-cache-integrated-redis-z9qyjo.html

@CacheNamespace(implementation = PerpetualCache.class)

使用 redis 作为 MyBatis 的二级缓存

使用 redis 作为 MyBatis 二级缓存的步骤如下:文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-s-secondary-cache-integrated-redis-z9qyjo.html

导入 mybatis-redis 的 pom 包文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-s-secondary-cache-integrated-redis-z9qyjo.html

<dependency>
    <groupId>org.mybatis.caches</groupId>
    <artifactId>mybatis-redis</artifactId>
    <version>1.0.0-beta2</version>
</dependency>

修改,IUserMapper ​加上相关注解文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-s-secondary-cache-integrated-redis-z9qyjo.html

请参考:https://github.com/terwer/senior-java-engineer-road/blob/main/p7-skill/framework/mybatis/mybatis-annotation/src/main/java/com/terwergreen/mapper/IUserMapper.java#L25文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-s-secondary-cache-integrated-redis-z9qyjo.html

@CacheNamespace(implementation = RedisCache.class)
public interface IUserMapper {

resource根目录 ​加上 redis.properties​ 配置文件文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-s-secondary-cache-integrated-redis-z9qyjo.html

host=localhost
port=6379
password=
database=0

特别提醒:这里的 配置 不要写错了。文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-s-secondary-cache-integrated-redis-z9qyjo.html

注意: 查询方法 ​也得地加上 @Options(useCache = true)​ 注解文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-s-secondary-cache-integrated-redis-z9qyjo.html

@Options(useCache = true)
@Select("select * from user where id=#{id}")
User findUserById(Integer id);

测试:文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-s-secondary-cache-integrated-redis-z9qyjo.html

请参考:https://github.com/terwer/senior-java-engineer-road/blob/main/p7-skill/framework/mybatis/mybatis-annotation/src/test/java/com/terwergreen/mapper/SecondCacheTest.java#L30文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-s-secondary-cache-integrated-redis-z9qyjo.html

@Test
public void secondLevelCache() {
    SqlSession sqlSession1 = sqlSessionFactory.openSession();
    SqlSession sqlSession2 = sqlSessionFactory.openSession();

    IUserMapper userMapper1 = sqlSession1.getMapper(IUserMapper.class);
    IUserMapper userMapper2 = sqlSession2.getMapper(IUserMapper.class);

    User user1 = userMapper1.findUserById(1);
    // 清空一级缓存
    sqlSession1.close();
    User user2 = userMapper2.findUserById(1);
    System.out.println(user1 == user2);
}

结果:文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-s-secondary-cache-integrated-redis-z9qyjo.html

Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6b81ce95]
==>  Preparing: select * from user where id=?
==> Parameters: 1(Integer)
<==    Columns: id, username, password, birthday
<==        Row: 1, lisi, 123, 2019-12-12
<==      Total: 1
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6b81ce95]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6b81ce95]
Returned connection 1803669141 to pool.
Cache Hit Ratio [com.terwergreen.mapper.IUserMapper]: 0.5
false

查看 redis 缓存文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-s-secondary-cache-integrated-redis-z9qyjo.html

MyBatis的二级缓存整合redis文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-s-secondary-cache-integrated-redis-z9qyjo.html

相关文章
  • 扫码加我微信
  • 验证消息请输入:来自你的博客
  • weinxin
  • 我的微信公众号
  • 微信扫一扫与我交流吧
  • weinxin
Terwer
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: