(五) Redis 实现Mybatis 二级缓存

一. redis与spring boot整合

依赖配置:

<dependency>
	<groupId>org.springframework.boot</groupId>
 	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> 
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.7.0</version>
</dependency>

application配置:

# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0

二. Mybatis二级缓存

2.1 缓存类的实现

​ 要想使用Mybatis的二级缓存,必须要实现Cache接口,具体的实现如下:

public class RedisCache implements Cache {

    private RedisTemplate<Object, Object> redisTemplate;

    private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    private String id;

    public RedisCache(){}

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

    private RedisTemplate<Object, Object> getRedisTemplate() {
        if(null == this.redisTemplate) {
            this.redisTemplate = (RedisTemplate)ApplicationContextHolder.getBean("redisTemplate");
        }
        return this.redisTemplate;
    }

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

    @Override
    public void putObject(Object key, Object value) {
        if(value != null) {
            this.getRedisTemplate().opsForValue().set(key, value);
        }
    }

    @Override
    public Object getObject(Object key) {
        if(null != key) {
            return this.getRedisTemplate().opsForValue().get(key);
        }
        return null;
    }

    @Override
    public Object removeObject(Object key) {
        if(null != key) {
            this.getRedisTemplate().delete(key);
        }
        return null;
    }

    @Override
    public void clear() {
        Set<Object> set = getRedisTemplate().keys("*:" + this.id + "*");
        if(null != set) {
            getRedisTemplate().delete(set);
        }
    }

    @Override
    public int getSize() {
        return 0;
    }

    @Override
    public ReadWriteLock getReadWriteLock() {
        return this.readWriteLock;
    }
}

2.2 获取ApplicationContext

@Component
public class ApplicationContextHolder implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}

2.3 mapper.xml配置

<!--
        flushInterval: 清空缓存的时间间隔,单位为毫秒; 默认情况是不设置,也就是没有刷新间隔,缓存仅仅调用更新语句时刷新。
        size: 可以被设置为任意正整数, 缓存的数量,默认是1024;
        evication: LRU 移除最长时间不被使用的对象。
        blocking: 默认是false;
     -->
<cache size="1024" type="com.qf.cache.RedisCache" />
发布了56 篇原创文章 · 获赞 3 · 访问量 4425

猜你喜欢

转载自blog.csdn.net/Tang_5253/article/details/102616808