SpringData是Spring中資料操作的模組,包含對各種資料庫的整合,其中對Redis的整合模組就叫做SpringDataRedis。
技術支援
-
提供了對不同Redis客戶端的整合(Lettuce和Jedis)
-
提供了RedisTemplate統一API來操作Redis
-
支援Redis的釋出訂閱模型
-
支援Redis哨兵和Redis叢集
-
支援基於Lettuce的響應式程式設計
-
支援基於JDK、JSON、字串、Spring物件的資料序列化及反序列化
-
支援基於Redis的JDKCollection實現
依賴注入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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
<artifactId>redis-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redis-demo</name>
<description>redis-demo</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<!-- Redis依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 連線池依賴-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.12.0</version>
</dependency>
<!-- lombok依賴-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- test-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- 序列化依賴包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application配置檔案
spring:
data:
redis:
host: ${sky.redis.host}
password: ${sky.redis.password}
database: ${sky.redis.database}
port: ${sky.redis.port}
lettuce:
pool:
max-active: 8 # 最大連線
max-idle: 8 #最大空閒連線
min-idle: 0 #最小空閒連線
max-wait: 100ms #連線等待時間
sky:
redis:
host: x.x.x.x #?
port: 6379
password: 123456 #?
database: 0 #?