文章目錄
前文
Redis 的下載安裝 https://blog.csdn.net/Woo_home/article/details/89155996
Redis 的簡單實用 https://blog.csdn.net/Woo_home/article/details/103901770
簡介
Redis 的 Java 客戶端有很多種,例如 Jedis、JRedis、Spring Data Redis 等,SpringBoot 藉助於 Spring Data Redis 爲 Redis 提供了開箱即用的自動化配置,開發者只需要添加相關的依賴並配置 Redis 連接信息即可
添加依賴
<dependencies>
<!-- 導入 web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加 Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加 Jedis 客戶端-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
配置 Redis
spring.redis.database=0
# Redis 的實例地址
spring.redis.host=127.0.0.1
# Redis 的默認端口號
spring.redis.port=6379
# Redis 的登錄密碼
spring.redis.password=
# Redis 連接池的最大連接數
spring.redis.jedis.pool.max-active=8
# Redis 連接池中的最大空閒連接數
spring.redis.jedis.pool.max-idle=8
# 表示連接池的最大阻塞等待時間,默認爲 -1,表示沒有限制
spring.redis.jedis.pool.max-wait=-1ms
# 表示連接池的最小空閒連接數
spring.redis.jedis.pool.min-idle=0
在設置密碼的時候如果是不知道 Redis 的密碼的可以這樣做:
啓動 redis 的客戶端程序,輸入以下命令
config get requirepass # 獲取密碼
如果要設置新的密碼,輸入以下命令
config set requirepass 你要設置的密碼 # 設置密碼
然後再次輸入獲取密碼的命令時可能會報以下的錯誤
(error) NOAUTH Authentication required.
只要輸入 auth 你設置的密碼 就可以了
創建實體類
package com.example.demo.entity;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String name;
private String info;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", info='" + info + '\'' +
'}';
}
}
@RedisAutoConfiguration
SpringBoot 的自動配置類中提供了 RedisAutoConfiguration 註解進行 Redis 的自動裝配,源碼如下:
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
public RedisAutoConfiguration() {
}
@Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
從 @RedisAutoConfiguration 註解的源碼可以知道,該註解定義了兩個 Bean:
- RedisTemplate
- StringRedisTemplate
而且 application.properties 中的配置信息會被注入到 RedisProperties 中,如果開發者自己沒有提供 RedisTemplate 或者 StringRedisTemplate 實例,則 SpringBoot 默認會提供這兩個實例,RedisTemplate 和 StringRedisTemplate 實例則提供了 Redis 的基本操作方法
編寫控制器代碼
RedisTemplate 與 StringRedisTemplate 操作
RedisTemplate
package com.example.demo.controller;
import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private RedisTemplate redisTemplate;
@GetMapping("test")
public void test() {
ValueOperations opsForValue = redisTemplate.opsForValue();
User setUser = new User();
setUser.setId(1);
setUser.setName("John");
setUser.setInfo("我愛打籃球");
opsForValue.set("user",setUser);
User getUser = (User) opsForValue.get("user");
System.out.println(getUser);
}
}
在瀏覽器中訪問 http://localhost:8080/test,控制檯顯示如下:
StringRedisTemplate
package com.example.demo.controller;
import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@GetMapping("test")
public void test() {
ValueOperations<String,String> opsForValue = stringRedisTemplate.opsForValue();
opsForValue.set("name","lisa");
String name = opsForValue.get("name");
System.out.println(name);
}
}
在瀏覽器中訪問 http://localhost:8080/test,控制檯顯示如下:
-
StringRedisTemplate 是 RedisTemplate 的子類,StringRedisTemplate 中的 key 和 value 都是字符串,採用的序列化的方案是 StringRedisSerializer,而 RedisTemplate 則可以用來操作對象,RedisTemplate 採用的序列化方案是 JdkSerializationRedisSerializer,無論是 StringRedisTemplate 還是 RedisTemplate,操作 Redis 的方法都是一樣的
-
StringRedisTemplate 和 RedisTemplate 都是通過 opsForValue、opsForZSet 或者 opsForSet 等方法首先獲取一個操作對象,再使用該操作對象完成數據的讀寫