SpringBoot+Mybatis整合Redis實(shí)現(xiàn)二級(jí)緩存

一. 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成功后直接進(jìn)入redis客戶端即可查看添加數(shù)據(jù)成功
    }

    @RequestMapping("/get")
    public Object get() {
        return redisTemplate.opsForList().range("users", 0, -1);
    }
}

二、 Mybatis二級(jí)緩存

一級(jí)緩存,就算Sqlsession級(jí)別緩存,二級(jí)緩存就是SqlsessionFactory級(jí)別緩存

2.1 緩存類的實(shí)現(xiàn)
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的方式,會(huì)生成一個(gè)RedisCache的實(shí)例,納入spring容器中,
 *    在這個(gè)容器中實(shí)例,是可以注入RedisTemplate。
 * 2.因?yàn)樵诿總€(gè)mapper中,配置了二級(jí)緩存,對(duì)應(yīng)的mapper會(huì)重新生成實(shí)例,生成的這個(gè)實(shí)例沒有按照spring的規(guī)則來生成。
 *   所以這個(gè)類中加入了 @Resource @AutoWire都是無法獲取。
 * 3.那么如何獲取spring容器中的對(duì)象了?就需要先拿到spring容器,然后從容器中去手動(dòng)取。
 */

public class RedisCache implements Cache {  //要想使用Mybatis的二級(jí)緩存,必須要實(shí)現(xiàn)Cache接口

    private RedisTemplate<Object, Object> redisTemplate;

    private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();

    private String id;

    // id的作用是將不同的mapper作一個(gè)區(qū)分
    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;
    }

    // 不用實(shí)現(xiàn)
    @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容器在啟動(dòng)的時(shí)候
 * 如果檢測某個(gè)類實(shí)現(xiàn)了這一類接口,那么會(huì)去調(diào)用實(shí)現(xiàn)了該接口方法的實(shí)現(xiàn)。
 */
@Component
public class ApplicationContextHolder implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    // 該放方法會(huì)自動(dòng)將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配置
    <!--
       在這里配置緩存,那么每個(gè)Mapper都會(huì)去生成 RedisCache的實(shí)現(xiàn)類
     -->
     <!--
        flushInterval: 清空緩存的時(shí)間間隔,單位為毫秒; 默認(rèn)情況是不設(shè)置,也就是沒有刷新間隔,緩存僅僅調(diào)用更新語句時(shí)刷新。
        size: 可以被設(shè)置為任意正整數(shù), 緩存的數(shù)量,默認(rèn)是1024;
        evication: LRU 移除最長時(shí)間不被使用的對(duì)象。
        blocking: 默認(rèn)是false;
     -->
    <cache size="1024" type="com.qf.cache.RedisCache"></cache>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容