Redis不像Ehcache一样提供了针对MyBatis的二级缓存的实现,因此需要我们自己来实现缓存的逻辑,但是归根到底原理是一样的,就是实现MyBatis的org.apache.ibatis.cache.Cache
接口,在实现类中我们把数据的存取中间件变为了Redis而已,下面是一个实现的示例:
package top.easyblog.config.cache;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.Cache;
import org.springframework.data.redis.connection.RedisServerCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import top.easyblog.commons.utils.RedisUtils;
import top.easyblog.config.ApplicationContextHolder;
import java.util.Objects;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* 使用Redis作为MyBatis的二级缓存
*
* @author huangxin
*/
@Slf4j
public class MyBatisRedisCache implements Cache {
private final String id;
/**
* 读写锁
*/
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);
/**
* redis读写工具类
*/
private RedisUtils redisUtils = null;
/**
* redis key 过期时间
*/
private static final long EXPIRE_TIME_IN_MINUTES = 30;
public MyBatisRedisCache(String id) {
if (Objects.isNull(id)) {
throw new IllegalArgumentException("Cache instances require an ID");
}
this.id = id;
}
@Override
public String getId() {
return this.id;
}
/**
* 向MyBatis二级缓存中放值
*
* @param key
* @param value
*/
@Override
public void putObject(Object key, Object value) {
if (Objects.nonNull(value)) {
RedisUtils redisUtils = getRedisUtils();
if (value != null && !value.equals(getObject(key))) {
redisUtils.set(key.toString(), value, 60 * EXPIRE_TIME_IN_MINUTES, 0);
}
}
}
/**
* 从缓存中取值
*
* @param key
* @return
*/
@Override
public Object getObject(Object key) {
if (Objects.nonNull(key)) {
RedisUtils redisUtils = getRedisUtils();
if (redisUtils.hasKey(key.toString(), 0)) {
return redisUtils.get(key.toString(), 0);
}
}
return null;
}
@Override
public Object removeObject(Object key) {
if (Objects.nonNull(key)) {
RedisUtils redisUtils = getRedisUtils();
if (redisUtils.hasKey(key.toString(), 0)) {
return redisUtils.delete(0, key.toString());
}
}
return null;
}
@Override
@SuppressWarnings("unchecked")
public void clear() {
RedisUtils redisUtils = getRedisUtils();
RedisTemplate redisTemplate = redisUtils.getRedisTemplate(0);
redisTemplate.execute((RedisCallback) connection -> {
connection.flushDb();
return null;
});
}
@Override
public int getSize() {
RedisUtils redisUtils = getRedisUtils();
RedisTemplate redisTemplate = redisUtils.getRedisTemplate(0);
Long size = Long.parseLong(Objects.requireNonNull(redisTemplate.execute(RedisServerCommands::dbSize)).toString());
return size != null ? size.intValue() : 0;
}
@Override
public ReadWriteLock getReadWriteLock() {
return this.readWriteLock;
}
/**这里的RedisUtil是我自定义的一个操作Redis的工具类,使用的使用可以把它替换成你的Redis工具*/
private RedisUtils getRedisUtils() {
if (Objects.isNull(redisUtils)) {
synchronized (MyBatisRedisCache.class) {
redisUtils = ApplicationContextHolder.getBean("redisUtils");
}
}
return redisUtils;
}
}
之后在Mapper文件中指定缓存使用我们实现的这个缓存即可。
<cache type="org.mybatis.caches.MyBatisRedisCache">
<property name="eviction" value="LRU"/>
<property name="flushInterval" value="6000000"/>
<property name="size" value="1024"/>
<property name="readOnly" value="false"/>
</cache>