欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot操作Redis三種方案全解析

 更新時間:2019年06月03日 10:07:34   作者:江南一點雨  
這篇文章主要介紹了SpringBoot操作Redis三種方案全解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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

除了這些方案之外,還有一個使用也相當(dāng)多的方案,就是 Spring Data Redis。

在傳統(tǒng)的 SSM 中,需要開發(fā)者自己來配置 Spring Data Redis ,這個配置比較繁瑣,主要配置 3 個東西:連接池、連接器信息以及 key 和 value 的序列化方案。

在 Spring Boot 中,默認(rèn)集成的 Redis 就是 Spring Data Redis,默認(rèn)底層的連接池使用了 lettuce ,開發(fā)者可以自行修改為自己的熟悉的,例如 Jedis。

Spring Data Redis 針對 Redis 提供了非常方便的操作模板 RedisTemplate 。這是 Spring Data 擅長的事情,那么接下來我們就來看看 Spring Boot 中 Spring Data Redis 的具體用法。

方案一:Spring Data Redis創(chuàng)建工程

創(chuàng)建工程,引入 Redis 依賴:

創(chuàng)建成功后,還需要手動引入 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>
</dependencies>
<dependency>
 <groupId>org.apache.commons</groupId>
 <artifactId>commons-pool2</artifactId>
</dependency>

這里主要就是引入了 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

自動配置

當(dāng)開發(fā)者在項目中引入了 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;
  }
}

這個自動化配置類很好理解:

  1. 首先標(biāo)記這個是一個配置類,同時該配置在 RedisOperations 存在的情況下才會生效(即項目中引入了 Spring Data Redis)
  2. 然后導(dǎo)入在 application.properties 中配置的屬性
  3. 然后再導(dǎo)入連接池信息(如果存在的話)
  4. 最后,提供了兩個 Bean ,RedisTemplate 和 StringRedisTemplate ,其中 StringRedisTemplate 是 RedisTemplate 的子類,兩個的方法基本一致,不同之處主要體現(xiàn)在操作的數(shù)據(jù)類型不同,RedisTemplate 中的兩個泛型都是 Object ,意味者存儲的 key 和 value 都可以是一個對象,而 StringRedisTemplate 的 兩個泛型都是 String ,意味者 StringRedisTemplate 的 key 和 value 都只能是字符串。如果開發(fā)者沒有提供相關(guān)的 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 中的數(shù)據(jù)操作,大體上來說,可以分為兩種:

  1. 針對 key 的操作,相關(guān)的方法就在 RedisTemplate 中
  2. 針對具體數(shù)據(jù)類型的操作,相關(guān)的方法需要首先獲取對應(yīng)的數(shù)據(jù)類型,獲取相應(yīng)數(shù)據(jù)類型的操作方法是 opsForXXX

調(diào)用該方法就可以將數(shù)據(jù)存儲到 Redis 中去了,如下:

k1 前面的字符是由于使用了 RedisTemplate 導(dǎo)致的,RedisTemplate 對 key 進行序列化之后的結(jié)果。

RedisTemplate 中,key 默認(rèn)的序列化方案是 JdkSerializationRedisSerializer 。

而在 StringRedisTemplate 中,key 默認(rèn)的序列化方案是 StringRedisSerializer ,因此,如果使用 StringRedisTemplate ,默認(rèn)情況下 key 前面不會有前綴。

不過開發(fā)者也可以自行修改 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);
 }
}

當(dāng)然也可以直接使用 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 集群,則所有的東西都需要自己手動配置,關(guān)于如何操作 Redis 集群,松哥以后再來和大家分享。

方案二:Spring Cache

通過 Spring Cache 的形式來操作 Redis,Spring Cache 統(tǒng)一了緩存江湖的門面,這種方案,松哥之前有過一篇專門的文章介紹,小伙伴可以移步這里:Spring Boot中,Redis緩存還能這么用!。

方案三:回歸原始時代

第三種方案,就是直接使用 Jedis 或者 其他的客戶端工具來操作 Redis ,這種方案在 Spring Boot 中也是支持的,雖然操作麻煩,但是支持

總結(jié)

Spring Boot 中,Redis 的操作,這里松哥給大家總結(jié)了三種方案,實際上前兩個使用廣泛一些,直接使用 Jedis 還是比較少,基本上 Spring Boot 中沒見過有人直接這么搞。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論