电竞比分网-中国电竞赛事及体育赛事平台

分享

Spring Boot 操作 Redis,三種方案全解析!

 jackeyqing 2019-06-12

在 Redis 出現(xiàn)之前,我們的緩存框架各種各樣,有了 Redis ,緩存方案基本上都統(tǒng)一了,關(guān)于 Redis,松哥之前有一個系列教程,尚不了解 Redis 的小伙伴可以參考這個教程:

使用 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 依賴如下:

  1. org.springframework.boot

  2. spring-boot-starter-data-redis

  3. org.springframework.boot

  4. spring-boot-starter-web

  5. org.apache.commons

  6. commons-pool2

這里主要就是引入了 Spring Data Redis + 連接池。

配置 Redis 信息

接下來配置 Redis 的信息,信息包含兩方面,一方面是 Redis 的基本信息,另一方面則是連接池信息:

  1. spring.redis.database=0

  2. spring.redis.password=123

  3. spring.redis.port=6379

  4. spring.redis.host=192.168.66.128

  5. spring.redis.lettuce.pool.min-idle=5

  6. spring.redis.lettuce.pool.max-idle=10

  7. spring.redis.lettuce.pool.max-active=8

  8. spring.redis.lettuce.pool.max-wait=1ms

  9. spring.redis.lettuce.shutdown-timeout=100ms

自動配置

當(dāng)開發(fā)者在項目中引入了 Spring Data Redis ,并且配置了 Redis 的基本信息,此時,自動化配置就會生效。

我們從 Spring Boot 中 Redis 的自動化配置類中就可以看出端倪:

  1. @Configuration

  2. @ConditionalOnClass(RedisOperations.class)

  3. @EnableConfigurationProperties(RedisProperties.class)

  4. @Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })

  5. public class RedisAutoConfiguration {

  6. @Bean

  7. @ConditionalOnMissingBean(name = 'redisTemplate')

  8. public RedisTemplateObject, Object> redisTemplate(

  9. RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {

  10. RedisTemplateObject, Object> template = new RedisTemplate<>();

  11. template.setConnectionFactory(redisConnectionFactory);

  12. return template;

  13. }

  14. @Bean

  15. @ConditionalOnMissingBean

  16. public StringRedisTemplate stringRedisTemplate(

  17. RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {

  18. StringRedisTemplate template = new StringRedisTemplate();

  19. template.setConnectionFactory(redisConnectionFactory);

  20. return template;

  21. }

  22. }

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

  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 來使用:

  1. @Service

  2. public class HelloService {

  3. @Autowired

  4. RedisTemplate redisTemplate;

  5. public void hello() {

  6. ValueOperations ops = redisTemplate.opsForValue();

  7. ops.set('k1', 'v1');

  8. Object k1 = ops.get('k1');

  9. System.out.println(k1);

  10. }

  11. }

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 進(jìn)行序列化之后的結(jié)果。

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

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

不過開發(fā)者也可以自行修改 RedisTemplate 中的序列化方案,如下:

  1. @Service

  2. public class HelloService {

  3. @Autowired

  4. RedisTemplate redisTemplate;

  5. public void hello() {

  6. redisTemplate.setKeySerializer(new StringRedisSerializer());

  7. ValueOperations ops = redisTemplate.opsForValue();

  8. ops.set('k1', 'v1');

  9. Object k1 = ops.get('k1');

  10. System.out.println(k1);

  11. }

  12. }

當(dāng)然也可以直接使用 StringRedisTemplate:

  1. @Service

  2. public class HelloService {

  3. @Autowired

  4. StringRedisTemplate stringRedisTemplate;

  5. public void hello2() {

  6. ValueOperations ops = stringRedisTemplate.opsForValue();

  7. ops.set('k2', 'v2');

  8. Object k1 = ops.get('k2');

  9. System.out.println(k1);

  10. }

  11. }

另外需要注意 ,Spring Boot 的自動化配置,只能配置單機的 Redis ,如果是 Redis 集群,則所有的東西都需要自己手動配置,關(guān)于如何操作 Redis 集群,松哥以后再來和大家分享。

方案二:Spring Cache

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

方案三:回歸原始時代

第三種方案,就是直接使用 Jedis 或者 其他的客戶端工具來操作 Redis ,這種方案在 Spring Boot 中也是支持的,雖然操作麻煩,但是支持,這種操作松哥之前也有介紹的文章,因此這里就不再贅述了,可以參考 Jedis 使用

總結(jié)

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

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多