利用Mybatis自身本地缓存结合redis 实现分布式缓存
前提: 做好Mybatis的环境和Redis 的链接环境即可, 无特殊要求
-
1、mybatis中应用级缓存<==>二级缓存 SqlSessionFactory 级别缓存 所有会话共享
-
2、如何开启(二级缓存)
加上标签: mapper.xml中的 —>本地缓存(此时只开启了本地的缓存, 若是集群部署则不起作用) -
3、查看Cache标签的缓存实现
查看 ibatis包下面的 Cache 接口 , Mybatis默认用的是 Cache的 PerpetualCache 实现的
org.apache.ibatis.cache.impl.PerpetualCache -
4、自定义RedisCache实现
1>、通过Mybatis默认Cache的源码得知 可以使用自定义Cache类 implement Cache 接口, 并对里面方法进行实现
2>、使用RedisCache实现
-
5、RedisCache代码
import com.wd.util.ApplicationContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.Cache;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* 自定义Redis缓存实现
*/
@Slf4j
public class RedisCache implements Cache {
// 当前放入缓存的mapper的namespace
private final String id;
// 必须存在构造方法 对id进行赋值
public RedisCache(String id) {
log.info("id: " + id);
this.id = id;
}
// 返回Cache的唯一标识
@Override
public String getId() {
return this.id;
}
/**
* 缓存中放入值
*/
@Override
public void putObject(Object key, Object value) {
log.info("putObject: key: " + key.toString());
log.info("putObject: value: " + value);
// 使用Redis中的Hash类型作为缓存存储模型 key(namespace) hashkey(key)-value(value)
getRedisTemplate().opsForHash().put(id, key.toString(), value);
}
/**
* 缓存中获取值
*/
@Override
public Object getObject(Object key) {
log.info("getObject: key: " + key.toString());
// 根据key 从redis的hash类型中获取数据
return getRedisTemplate().opsForHash().get(id, key.toString());
}
/**
* 根据指定的key删除缓存
* 注意: 这个方法为Mybatis的保留方法, 默认没有实现 后续版本可能会实现
*/
@Override
public Object removeObject(Object key) {
log.info("removeObject: key: " + key);
return null;
}
/**
* 清空缓存
*/
@Override
public void clear() {
log.info("clear");
// 清空 namespace 的缓存
getRedisTemplate().delete(id);
}
/**
* 用来计算缓存的数量
*/
@Override
public int getSize() {
// 获取hash中 key value的数量
return getRedisTemplate().opsForHash().size(id).intValue();
}
// 封装redisTemplate
private RedisTemplate getRedisTemplate(){
// 通过application工具类获取redisTemplate
RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtils.getBeanByName("redisTemplate");
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
}
- 6、由于RedisCache不是Spring工厂管理的,非springboot工厂管理的普通类中,如果要获取工厂管理的对象,我们不能再使用 @Autowired 等注入的注解。所以获取 RedisTemplate对象需要进行特殊处理
代码如下
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
// 用来获取SpringBoot创建好的工厂
@Component
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 getBeanByName(String beanName) {
return applicationContext.getBean(beanName);
}
}
问题存在:
1、如果项目中表查询之间没有任何关联查询使用现在的这种缓存方式没有任何问题,否则有些表(命名空间)的数据删除了,不会影响到已经缓存的关联查询的数据
2、现有的缓存方式在表连接查询过程中一定存在问题!