文章目录
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 #重启网卡
可能是我没有正常关机造成的