【Java面试实战篇】你说说mybatis怎么实现Redis分布式缓存?

1. Mybatis的缓存机制

一级缓存(默认开启)

SqlSession级的缓存:在同一个SqlSession 查询同一个数据,不需要再通过数据库查询

二级缓存

SqlSessionFactory级的缓存:在所有的SqlSession 查询同一个数据,不需要再通过数据库查询 在mybatis的mapper.xml文件中加入标签:

<!-- eviction LRU 、flushInterval缓存时间,以毫秒为单位、size缓存大小 、readOnly如果为false的话,缓存对象必须是可序列化的-->
<cache type="org.apache.ibatis.cache.impl.PerpetualCache"
  eviction="LRU" 
   flushInterval="120000" 
   size="1024" 
   readOnly="true"/>  <!- 默认本地缓存 ->

二级缓存实现原理:

  • mybatis的二级缓存主要在'Executor'对象上进行操作,当mybatis发现在mybatis.xml配置文件中设置了cacheEnabled=true时,mybatis在创建 sqlsession时创建Executor对象,同时会对Executor加上装饰者【CacheExecutor】。

  • CacheExecutor对于查询请求,会判断application级别的二级缓存是否有缓存结果,如果有查询结果则直接返回,如果没有

2. 利用mybatis自身本地缓存结合redis实现分布式缓存

  1. mybatis中应用二级缓存默认PepreCache SqlSessionFactory级别的缓存 所有SqlSession会话共享

  2. 如何开启(二级缓存) 本地缓存 默认是使用了org.apache.ibatis.cache.impl.PerpetualCache实现

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="...">
 <!-- 默认使用 PerpetualCache -->
 <!--<cache type="....PerpetualCache "/>-->
 
 <!--开启mybatis二级缓存-->
    <cache  type="....RedisCache"/>
    
 <!--关联关系缓存处理-->
    <cache-ref namespace="...UserDAO"/>

</mapper>
public class PerpetualCache implements Cache {
 private final String id; // 【必须】当前放入缓存的Mapper的 namespace 名称空间
 private final Map<Object, Object> cache = new HashMap<>();
 public PerpetualCache(String id) {
  this.id = id;
 }
 // 返回cache的唯一标识
 @Override
 public String getId() {
  return id;
 }

 @Override
 public int getSize() {
  return cache.size();
 }
 // 缓存放入值
 // redis --- RedisTemplate  StringRedisTemplate
 // 
 @Override
 public void putObject(Object key, Object value) {
  cache.put(key, value);
 }
 // 从缓存中获取值
 @Override
 public Object getObject(Object key) {
  return cache.get(key);
 }
 ... 
}

3. 自定义RedisCache实现

通过mybatis默认的 cachery源码得知,可以使用自定义Cache类 实现Cache接口,并对里面的方法进行实现

public class RedisCache implements Cache{
 ...
}

使用RedisCache实现

<mapper ...>
 <cache type="...RedisCache"/>
</mapper>

细节注意:

  1. RedisTemplate对象是自动注入到IOC容器中,然后通过ApplicationContext对象回去容器对象。

  2. 在Spring Boot 内部 提供接口 ApplicationContextAware 获取IOC容器ApplicationContext对象。然后通过applicationContext对象获取Redis操作对象 RedisTemplate 对象。

//用来获取springboot创建好的工厂
@Configuration
public class ApplicationContextUtils implements ApplicationContextAware {

    //保留下来工厂
    private static ApplicationContext applicationContext;

    //将创建好工厂以参数形式传递给这个类
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
    
    //提供在工厂中获取对象的方法 //RedisTemplate  redisTemplate
    public static Object getBean(String beanName){
        return applicationContext.getBean(beanName);
    }

}

实现Cache接口,实现Redis分布式缓存

import com.baizhi.util.ApplicationContextUtils;
import org.apache.ibatis.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.DigestUtils;
import java.util.concurrent.TimeUnit;
//自定义Redis缓存实现
public class RedisCache implements Cache {

    //当前放入缓存的mapper的namespace
    private final String id;

    //必须存在构造方法
    public RedisCache(String id) {
        System.out.println("id:=====================> " + id);
        this.id = id;
    }
    //返回cache唯一标识
    @Override
    public String getId() {
        return this.id;
    }
    //缓存放入值  redis RedisTemplate   StringRedisTemplate
    @Override
    public void putObject(Object key, Object value) {
        System.out.println("key:" + key.toString());
        System.out.println("value:" + value);
//        //通过application工具类获取redisTemplate
//        RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtils.getBean("redisTemplate");
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
//        redisTemplate.setHashKeySerializer(new StringRedisSerializer());

        //使用redishash类型作为缓存存储模型  key   hashkey  value
        getRedisTemplate().opsForHash().put(id.toString(),getKeyToMD5(key.toString()),value);



        if(id.equals("com.baizhi.dao.UserDAO")){
            //缓存超时  client  用户   client  员工
            getRedisTemplate().expire(id.toString(),1, TimeUnit.HOURS);
        }


        if(id.equals("com.baizhi.dao.CityDAO")){
            //缓存超时  client  用户   client  员工
            getRedisTemplate().expire(id.toString(),30, TimeUnit.MINUTES);
        }
        //.....指定不同业务模块设置不同缓存超时时间
    }

    //获取中获取数据
    @Override
    public Object getObject(Object key) {
        System.out.println("key:" + key.toString());
//        //通过application工具类获取redisTemplate
//        RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtils.getBean("redisTemplate");
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
//        redisTemplate.setHashKeySerializer(new StringRedisSerializer());

        //根据key 从redis的hash类型中获取数据
        return getRedisTemplate().opsForHash().get(id.toString(), getKeyToMD5(key.toString()));
    }


    //注意:这个方法为mybatis保留方法 默认没有实现 后续版本可能会实现
    @Override
    public Object removeObject(Object key) {
        System.out.println("根据指定key删除缓存");
        return null;
    }

    @Override
    public void clear() {
        System.out.println("清空缓存~~~");
        //清空namespace
        getRedisTemplate().delete(id.toString());//清空缓存
    }

    //用来计算缓存数量
    @Override
    public int getSize() {
        //获取hash中key value数量
        return getRedisTemplate().opsForHash().size(id.toString()).intValue();
    }


    //封装redisTemplate
    private RedisTemplate getRedisTemplate(){
        //通过application工具类获取redisTemplate
        RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtils.getBean("redisTemplate");
        return redisTemplate;
    }


    //封装一个对key进行md5处理方法
    private String getKeyToMD5(String key){
        return DigestUtils.md5DigestAsHex(key.getBytes());
    }

}

多表查询

关联查询的Mapper.xml文件

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="...">
 <!--<cache type="....PerpetualCache "/>--><!-- 默认使用 PerpetualCache -->
 <!--开启mybatis二级缓存-->
    <cache  type="....RedisCache"/>
   
   ...

</mapper>

被关联表的Mapper.xml文件:

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="...">
 <!--关联关系缓存处理-->
    <cache-ref namespace="...UserDAO"/>
</mapper>

觉得这篇文章对你有用,就请您点赞分享收藏,顺路点"在看"呗!

我们下期再会!


作者:神域的思念
链接:

https://blog.csdn.net/First_Bal/article/details/107573596
来源:CSDN

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值