redis实现mybatis的二级缓存

步骤一:开启mybatis的二级缓存

#开启mybatis的二级缓存
mybatis.configuration.cache-enabled=true

步骤二:实现mybatis的cache接口

package com.aaa.qy156.utils;
import lombok.extern.java.Log;
import org.apache.ibatis.cache.Cache;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Set;
/**
 * @author :Teacher陈(86521760@qq.com)
 * @date :Created in 2022/10/22 16:20
 * @description:我自己的myabtis二级缓存实现类
 * 将我们的数据缓存到redis中,MyMybatisCacheImpl 不被spring所管理
 * @modified By:
 * @version:
 */
@Log
public class MyMybatisCacheImpl   implements Cache {
    private  String id;
    private RedisTemplate<String, Object> redisTemplate;
    //构造的时候id必须传
    public MyMybatisCacheImpl(String id) {
        this.id = id;
    }
    @Override
    public String getId() {
        return this.id;
    }
    @Override
    public void putObject(Object key, Object value) {
        log.info("设置缓存");
        if(null==redisTemplate){
            redisTemplate = (RedisTemplate<String, Object>) MySpringTool.getBean("redisTemplate");
        }
        redisTemplate.opsForValue().set(key.toString(),value);
    }
    @Override
    public Object getObject(Object key) {
        log.info("查询缓存");
        if(null==redisTemplate){
              redisTemplate = (RedisTemplate<String, Object>) MySpringTool.getBean("redisTemplate");
        }
        Object o = redisTemplate.opsForValue().get(key.toString());
        return o;
    }
    @Override
    public Object removeObject(Object key) {
        log.info("删除缓存");
        if(null==redisTemplate){
            redisTemplate = (RedisTemplate<String, Object>) MySpringTool.getBean("redisTemplate");
        }
        redisTemplate.delete(key.toString());
        return null;
    }
    @Override
    public void clear() {
        log.info("清除缓存");
        if(null==redisTemplate){
            redisTemplate = (RedisTemplate<String, Object>) MySpringTool.getBean("redisTemplate");
        }
        Set<String> keys = redisTemplate.keys("*" + this.id + "*");
        redisTemplate.delete(keys);
    }
    @Override
    public int getSize() {
        if(null==redisTemplate){
            redisTemplate = (RedisTemplate<String, Object>) MySpringTool.getBean("redisTemplate");
        }
        Integer execute = redisTemplate.execute(new RedisCallback<Integer>() {
            @Override
            public Integer doInRedis(RedisConnection connection) throws DataAccessException {
                return connection.dbSize().intValue();
            }
        });
        return execute;
    }
}


步骤三:获取bean的工具类


package com.aaa.qy156.utils;
import jdk.internal.dynalink.beans.StaticClass;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
/**
 * @author :Teacher陈(86521760@qq.com)
 * @date :Created in 2022/10/22 16:34
 * @description:在运行时手动获取bean
 * @modified By:
 * @version:
 */
@Component
public class MySpringTool implements ApplicationContextAware {
    private static ApplicationContext myApplicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        myApplicationContext=applicationContext;
    }
/**
 * @create by: Teacher陈(86521760@qq.com)
 * @description: 获取bean
 * @create time: 2022/10/22 16:37
 * @return
 */
    public static Object getBean(String beanId){
        RedisTemplate<String, Object>  bean = myApplicationContext.getBean(beanId,RedisTemplate.class);
        return bean;
    }
}

步骤四:在映射文件或者在接口上使用cache注解中引入缓存接口的实现类

        <!--    eviction缓存过期策略,默认LRU(least recently use)  最近最少使用策略
                FIfO  (first in  first out ) 先进先出
        -->
        <cache type="com.aaa.qy156.utils.MyMybatisCacheImpl" eviction="LRU"></cache>

步骤五:测试

启动项目,请求数据发现第一次查询之后再次查询没有执行SQL语句,直接从缓存中查询,不是从数据库查询,结果如下图所示

查看日志要在配置文件中加入下面代码

logging.level.com.aaa.qy156.dao=debug

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值