1. 程式人生 > >spring-boot-2.0.3之redis快取實現,不是你想的那樣哦

spring-boot-2.0.3之redis快取實現,不是你想的那樣哦

前言
  開心一刻

    小白問小明:“你前面有一個5米深的坑,裡面沒有水,如果你跳進去後該怎樣出來了?”小明:“躺著出來唄,還能怎麼出來?”小白:“為什麼躺著出來?”小明:“5米深的坑,還沒有水,跳下去不死就很幸運了,殘是肯定會殘的,不躺著出來,那能怎麼出來?”小白:“假設沒死也沒殘呢?”小明:“你當我超人了? 那也簡單,把腦子裡的水放出來就可以漂出來了。”小白:“你腦子裡有這麼多水嗎?”小明:“我腦子裡沒那麼多水我跳下去幹嘛?” 

  路漫漫其修遠兮,吾將上下而求索!

  springboot 1.x到2.x變動的內容還是挺多的,而2.x之間也存在細微的差別,本文不講這些差別(具體差別我也不知道,汗......),只講1.5.9與2.0.3的redis快取配置的區別

回到頂部
springboot1.5.9快取配置
  工程實現
    1.x系列配置應該都差不多,下面我們看看1.5.9中,springboot整合redis的快取實現

    pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

;
<modelVersion>4.0.0</modelVersion>

<groupId>com.lee</groupId>
<artifactId>spring-boot159.cache</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <java.version>1.8</java.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

</dependencies>

</project>
application.yml

  server:
port: 8888
spring:
#redis配置
redis:
database: 0
host: 127.0.0.1
port: 6379
password:

連線超時時間(毫秒)

timeout: 2000
pool:

連線池最大連線數(使用負值表示沒有限制)

max-active: 8

連線池最大阻塞等待時間(使用負值表示沒有限制)

max-wait: -1

連線池中的最大空閒連線

max-idle: 8

連線池中的最小空閒連線

min-idle: 0
cache:
type: redis
cache:
expire-time: 180  

    RedisCacheConfig.java

package com.lee.cache.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

/**

package com.lee.cache.service.impl;

import com.lee.cache.model.User;
import com.lee.cache.service.ICacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**

}
    上述只講了幾個主要的檔案,更多詳情請點springboot159-cache

  redis 怎樣儲存cache
    大家一定要把工程仔細看一遍,不然下面出現的一些名稱會讓我們感覺不知從哪來的;

spring-boot-2.0.3之redis快取實現,不是你想的那樣哦
    工程中的快取分兩種:快取管理器管理的快取(也就是一些列註解實現的快取)、redisTemplate操作的快取

      快取管理器管理的快取

        會在redis中增加2條資料,一個是型別為 zset 的 快取名~keys , 裡面存放了該快取所有的key, 另一個是對應的key,值為序列化後的json;快取名~keys可以理解成快取空間,與我們平時所說的具體的快取是不一樣的。另外對快取管理器的一些設定(全域性過期時間等)都會反映到快取管理器管理的所有快取上;上圖中的http://localhost:8888/getName和http://localhost:8888/listUser?pageNum=1&pageSize=3對應的是快取管理器管理的快取。

      redisTemplate操作的快取

        會在redis中增加1條記錄,key - value鍵值對,與我們通過redis-cli操作快取一樣;上圖中的http://localhost:8888/getUserName對應的是redisTemplate操作的快取。

回到頂部
spring2.0.3快取配置
  工程實現
    pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="
http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">;
<modelVersion>4.0.0</modelVersion>

<groupId>com.lee</groupId>
<artifactId>spring-boot-cache</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
</parent>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>

</dependencies>

</project>
    application.yml

server:
port: 8889
spring:
#redis配置
redis:
database: 0
host: 127.0.0.1
port: 6379
password:
lettuce:
pool:

接池最大連線數(使用負值表示沒有限制)

    max-active: 8
    # 連線池最大阻塞等待時間(使用負值表示沒有限制)
    max-wait: -1ms
    # 連線池中的最小空閒連線
    max-idle: 8
    # 連線池中的最大空閒連線
    min-idle: 0
# 連線超時時間
timeout: 2000ms

cache:
type: redis
cache:
test:
expire-time: 180
name: test
default:
expire-time: 200
    快取定製:RedisCacheManagerConfig.java

package com.lee.cache.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**

    CacheServiceImpl.java
package com.lee.cache.service.impl;

import com.lee.cache.model.User;
import com.lee.cache.service.ICacheService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**

}
    更多詳情請點spring-boot-cache

  redis 怎樣儲存cache
    我們來看圖說話,看看快取在redis中是如何儲存的

spring-boot-2.0.3之redis快取實現,不是你想的那樣哦
    工程中的快取分兩種:快取管理器管理的快取(也就是一些列註解實現的快取)、redisTemplate操作的快取

      快取管理器管理的快取

        會在redis中增加1條資料,key是以快取空間開頭的字串(快取空間名::快取key),值為序列化後的json;上圖中的http://localhost:8889/getName和http://localhost:8889/listUser?pageNum=1&pageSize=3對應的是快取管理器管理的快取。

      redisTemplate操作的快取

        會在redis中增加1條記錄,key - value鍵值對,與我們通過redis-cli操作快取一樣;上圖中的http://localhost:8889/getUserName對應的是redisTemplate操作的快取。

回到頂部
總結
  1、有時候我們引入spring-boot-starter-cache這個starter只是為了快速新增快取依賴,目的是引入spring-context-support;如果我們的應用中中已經有了spring-context-support,那麼我們無需再引入spring-boot-starter-cache,例如我們的應用中依賴了spring-boot-starter-web,而spring-boot-starter-web中又有spring-context-support依賴,所以我們無需再引入spring-boot-starter-cache。

  2、Supported Cache Providers,講了支援的快取型別以及預設情況下的快取載入方式,可以通讀下。

  3、只要我們引入了redis依賴,並將redis的連線資訊配置正確,springboot(2.0.3)根據我們的配置會給我們生成預設的快取管理器和redisTemplate;我們也可以自定義我們自己的快取管理器來替換掉預設的,只要我們自定義了快取管理器和redisTemplate,那麼springboot的預設生成的會替換成我們自定義的。

  4、快取管理器對快取的操作也是通過redisTemplate實現的,只是進行了統一的管理,並且能夠減少我麼的程式碼量,我們可以將更多的精力放到業務處理上。

  5、redis-cli -h 127.0.0.1 -p 6379 -a 123456與redis-cli -a 123456兩種方式訪問到的資料完全不一致,好像操作不同的庫一樣! 這個需要注意,有空我回頭看看這兩者到底有啥區別,有知道的朋友可以留個言。