SpringBoot+Mybatis整合Redis实现二级缓存

一. redis与spring boot整合
  1. 添加pom依赖
<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>
  1. 代码中注入RedisTemplate使用即可
@RequestMapping("/redis")
@RestController
public class RedisController {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping("/set")
    public Object set() {
//        redisTemplate.opsForValue(); // opsForValue 就是之前操作字符串
//        redisTemplate.opsForList(); //opsForList, 操作列表
//        redisTemplate.opsForHash(); // hash(map)
//        redisTemplate.opsForZSet(); // zset
//        redisTemplate.opsForSet(); //set

        redisTemplate.opsForList().leftPushAll("users", "zhangsan", "lisi");

        return "success";   //set成功后直接进入redis客户端即可查看添加数据成功
    }

    @RequestMapping("/get")
    public Object get() {
        return redisTemplate.opsForList().range("users", 0, -1);
    }
}
二、 Mybatis二级缓存

一级缓存,就算Sqlsession级别缓存,二级缓存就是SqlsessionFactory级别缓存

2.1 缓存类的实现
package com.zqh.cache;

import com.zqh.config.ApplicationContextHolder;
import org.apache.ibatis.cache.Cache;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 1.使用@Component的方式,是spring的方式,会生成一个RedisCache的实例,纳入spring容器中,
 *    在这个容器中实例,是可以注入RedisTemplate。
 * 2.因为在每个mapper中,配置了二级缓存,对应的mapper会重新生成实例,生成的这个实例没有按照spring的规则来生成。
 *   所以这个类中加入了 @Resource @AutoWire都是无法获取。
 * 3.那么如何获取spring容器中的对象了?就需要先拿到spring容器,然后从容器中去手动取。
 */

public class RedisCache implements Cache {  //要想使用Mybatis的二级缓存,必须要实现Cache接口

    private RedisTemplate<Object, Object> redisTemplate;

    private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();

    private String id;

    // id的作用是将不同的mapper作一个区分
    public RedisCache(String id) {
        this.id = id;
    }

    // 获取RedisTemplate
    private RedisTemplate<Object, Object> getRedisTemplate() {
        this.redisTemplate = ApplicationContextHolder.getRedisTemplate();
        return redisTemplate;
    }

    @Override
    public void putObject(Object key, Object value) {
        getRedisTemplate().opsForValue().set(key, value);
    }

    @Override
    public Object getObject(Object key) {
        return getRedisTemplate().opsForValue().get(key);
    }

    @Override
    public Object removeObject(Object key) {
        return getRedisTemplate().delete(key);
    }

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

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

    // 不用实现
    @Override
    public void clear() {
    }

    @Override
    public ReadWriteLock getReadWriteLock() {
        return this.reentrantReadWriteLock;
    }
}
2.2 获取ApplicationContext
package com.zqh.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * spring提供了很多的接口 XXXXAware, 这一类的接口比较的特殊, 那么spring容器在启动的时候
 * 如果检测某个类实现了这一类接口,那么会去调用实现了该接口方法的实现。
 */
@Component
public class ApplicationContextHolder implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    // 该放方法会自动将spring容器的类 ApplicationContext, 传入给该方法。
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextHolder.applicationContext = applicationContext;
    }

    // 从容器中获取RedisTemplate
    public static RedisTemplate getRedisTemplate() {
        return applicationContext.getBean("redisTemplate", RedisTemplate.class);
    }
}
2.3 mapper.xml配置
    <!--
       在这里配置缓存,那么每个Mapper都会去生成 RedisCache的实现类
     -->
     <!--
        flushInterval: 清空缓存的时间间隔,单位为毫秒; 默认情况是不设置,也就是没有刷新间隔,缓存仅仅调用更新语句时刷新。
        size: 可以被设置为任意正整数, 缓存的数量,默认是1024;
        evication: LRU 移除最长时间不被使用的对象。
        blocking: 默认是false;
     -->
    <cache size="1024" type="com.qf.cache.RedisCache"></cache>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值