【Mybatis二级缓存】Redis开启Mybatis二级缓存

Redis开启Mybatis二级缓存

yml文件,配置redis的基本参数及开启mybatis的二级缓存

spring:
  redis:
    port: 6379
    host: 192.168.179.100
    password: 123456
#开启mybatis的二级缓存
mybatis:
  configuration:
    cache-enabled: true

1. 编写MyMybatisCacheImpl实现Cache接口,重写里面的方法!

package com.aaa.utils;

import lombok.extern.slf4j.Slf4j;
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 : 尚腾飞(838449693@qq.com)
 * @version : 1.0
 * @createTime : 2022/10/23 16:56
 * @description :
 */
@Slf4j
public class MyMybatisCacheImpl implements Cache {
    private String id;
    private RedisTemplate<String, Object> redisTemplate;

    public MyMybatisCacheImpl(String id) {
        this.id = id;
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public void putObject(Object o, Object o1) {
        if (null == redisTemplate) {
            redisTemplate = (RedisTemplate<String, Object>) MySpringTool.getBean("redisTemplate");
        }
        redisTemplate.opsForValue().set(o.toString(), o1);
    }

    @Override
    public Object getObject(Object o) {
        log.info("查询缓存");
        if (null == redisTemplate) {
            redisTemplate = (RedisTemplate<String, Object>) MySpringTool.getBean("redisTemplate");
        }
        return redisTemplate.opsForValue().get(o.toString());
    }

    @Override
    public Object removeObject(Object o) {
        log.info("删除缓存");
        if (null == redisTemplate) {
            redisTemplate = (RedisTemplate<String, Object>) MySpringTool.getBean("redisTemplate");
        }
        redisTemplate.delete(o.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;
    }
}

2. 编写MySpringTool工具类,能够从spring中手动获取bean

package com.aaa.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.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) {
        return myApplicationContext.getBean(beanId);
    }
}

MySpringTool目的是能够从spring中获取对象。

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

eviction缓存过期策略:
默认LRU(least recently use) 最近最少使用策略
FIfO (first in first out ) 先进先出

<cache type="com.aaa.utils.MyMybatisCacheImpl" eviction="LRU"/>

在这里插入图片描述

注解方式:
@CacheNamespace(implementation = MyMybatisCacheImpl.class)
通过注解的方式,没有测试成功,初步怀疑是@CacheNamespace注解只支持@Select @Insert ...等注解的sql语句,不能作用于mapper.xml文件


但是并没有定论,也可能是我使用的方式错误。

4. 测试

yml文件中开启debug模式,狂点几下查询的页面,去后台发现并没有查询数据库,缓存的命中率在逐渐增高,无限接近1,而不会达到1,正如我逝去的青春
在这里插入图片描述

顺手记录一下今天的一个莫名的Bug
就是虚拟机上一秒还正常使用,重启一下ens33网卡没有了???
嗯???很奇怪,搜了搜很多人都遇到了这个问题,解决办法也给我找到了,如下:

systemctl stop NetworkManager    #关闭 NetworkManager
systemctl disable NetworkManager #开机禁用 NetworkManager
systemctl start network.service  #开启网卡
service network restart          #重启网卡

可能是我没有正常关机造成的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

打乒乓球只会抽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值