使用 Java 操作 Redis 的方案很多,Jedis 是目前較為流行的一種方案,除了 Jedis ,還有很多其他解決方案,如下:

除了這些方案之外,還有一個使用也相當多的方案,就是 Spring Data Redis。
在傳統的 SSM 中,需要開發者自己來配置 Spring Data Redis ,這個配置比較繁瑣,主要配置 3 個東西:連接配接池、連接配接器資訊以及 key 和 value 的序列化方案。
在 Spring Boot 中,預設內建的 Redis 就是 Spring Data Redis,預設底層的連接配接池使用了 lettuce ,開發者可以自行修改為自己的熟悉的,例如 Jedis。
Spring Data Redis 針對 Redis 提供了非常友善的操作模闆 RedisTemplate 。這是 Spring Data 擅長的事情,那麼接下來我們就來看看 Spring Boot 中 Spring Data Redis 的具體用法。
方案一:Spring Data Redis
建立工程
建立工程,引入 Redis 依賴:
建立成功後,還需要手動引入 commos-pool2 的依賴,是以最終完整的 pom.xml 依賴如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
</dependencies>
這裡主要就是引入了 Spring Data Redis + 連接配接池。
配置 Redis 資訊
接下來配置 Redis 的資訊,資訊包含兩方面,一方面是 Redis 的基本資訊,另一方面則是連接配接池資訊:
spring.redis.database=0
spring.redis.password=123
spring.redis.port=6379
spring.redis.host=192.168.66.128
spring.redis.lettuce.pool.min-idle=5
spring.redis.lettuce.pool.max-idle=10
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=1ms
spring.redis.lettuce.shutdown-timeout=100ms
自動配置
當開發者在項目中引入了 Spring Data Redis ,并且配置了 Redis 的基本資訊,此時,自動化配置就會生效。
我們從 Spring Boot 中 Redis 的自動化配置類中就可以看出端倪:
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class 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;
}
}
這個自動化配置類很好了解:
- 首先标記這個是一個配置類,同時該配置在 RedisOperations 存在的情況下才會生效(即項目中引入了 Spring Data Redis)
- 然後導入在 application.properties 中配置的屬性
- 然後再導入連接配接池資訊(如果存在的話)
- 最後,提供了兩個 Bean ,RedisTemplate 和 StringRedisTemplate ,其中 StringRedisTemplate 是 RedisTemplate 的子類,兩個的方法基本一緻,不同之處主要展現在操作的資料類型不同,RedisTemplate 中的兩個泛型都是 Object ,意味者存儲的 key 和 value 都可以是一個對象,而 StringRedisTemplate 的 兩個泛型都是 String ,意味者 StringRedisTemplate 的 key 和 value 都隻能是字元串。如果開發者沒有提供相關的 Bean ,這兩個配置就會生效,否則不會生效。
使用
接下來,可以直接在 Service 中注入 StringRedisTemplate 或者 RedisTemplate 來使用:
@Service
public class HelloService {
@Autowired
RedisTemplate redisTemplate;
public void hello() {
ValueOperations ops = redisTemplate.opsForValue();
ops.set("k1", "v1");
Object k1 = ops.get("k1");
System.out.println(k1);
}
}
Redis 中的資料操作,大體上來說,可以分為兩種:
- 針對 key 的操作,相關的方法就在 RedisTemplate 中
- 針對具體資料類型的操作,相關的方法需要首先擷取對應的資料類型,擷取相應資料類型的操作方法是 opsForXXX
調用該方法就可以将資料存儲到 Redis 中去了,如下:
k1 前面的字元是由于使用了 RedisTemplate 導緻的,RedisTemplate 對 key 進行序列化之後的結果。
RedisTemplate 中,key 預設的序列化方案是 JdkSerializationRedisSerializer 。
而在 StringRedisTemplate 中,key 預設的序列化方案是 StringRedisSerializer ,是以,如果使用 StringRedisTemplate ,預設情況下 key 前面不會有字首。
不過開發者也可以自行修改 RedisTemplate 中的序列化方案,如下:
@Service
public class HelloService {
@Autowired
RedisTemplate redisTemplate;
public void hello() {
redisTemplate.setKeySerializer(new StringRedisSerializer());
ValueOperations ops = redisTemplate.opsForValue();
ops.set("k1", "v1");
Object k1 = ops.get("k1");
System.out.println(k1);
}
}
當然也可以直接使用 StringRedisTemplate:
@Service
public class HelloService {
@Autowired
StringRedisTemplate stringRedisTemplate;
public void hello2() {
ValueOperations ops = stringRedisTemplate.opsForValue();
ops.set("k2", "v2");
Object k1 = ops.get("k2");
System.out.println(k1);
}
}
另外需要注意 ,Spring Boot 的自動化配置,隻能配置單機的 Redis ,如果是 Redis 叢集,則所有的東西都需要自己手動配置,關于如何操作 Redis 集。