溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring boot集成redis lettuce的用法

發(fā)布時間:2020-08-04 10:15:14 來源:億速云 閱讀:1044 作者:小豬 欄目:編程語言

這篇文章主要講解了Spring boot集成redis lettuce的用法,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。

spring boot框架中已經(jīng)集成了redis,在1.x.x的版本時默認(rèn)使用的jedis客戶端,現(xiàn)在是2.x.x版本默認(rèn)使用的lettuce客戶端

引入依賴

<!-- spring boot redis 緩存引入 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
      <version>2.0.4.RELEASE</version>
    </dependency>

<!-- redis依賴commons-pool 這個依賴一定要添加 -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
    </dependency>

配置文件

#Redis 配置
#Redis服務(wù)器地址
spring.redis.host=127.0.0.1
#Redis服務(wù)器連接端口
spring.redis.port=6379
#Redis服務(wù)器連接密碼(默認(rèn)為空)
spring.redis.password=123456
#Redis數(shù)據(jù)庫索引(默認(rèn)為0)
spring.redis.database=0
##連接超時時間
spring.redis.timeout=60s

# 以下連接池已在SpringBoot2.0不推薦使用
##連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
#spring.redis.jedis.pool.max-active=10
##連接池最大阻塞等待時間(使用負(fù)值表示沒有限制)
#spring.redis.jedis.pool.max-wait=-1ms
##連接池中的最大空閑連接
#spring.redis.jedis.pool.max-idle=8
##連接池中的最小空閑連接
#spring.redis.jedis.pool.min-idle=0

# Lettuce
# 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
spring.redis.lettuce.pool.max-active=8
# 連接池最大阻塞等待時間(使用負(fù)值表示沒有限制)
spring.redis.lettuce.pool.max-wait=10000
# 連接池中的最大空閑連接
spring.redis.lettuce.pool.max-idle=8
# 連接池中的最小空閑連接
spring.redis.lettuce.pool.min-idle=0
# 關(guān)閉超時時間
spring.redis.lettuce.shutdown-timeout=100

配置config

@Configuration
@AutoConfigureAfter(RedisConfig.class)
public class RedisConfig {
 
//  @Bean
//  public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
//    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//    redisTemplate.setKeySerializer(new StringRedisSerializer());
//    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//    redisTemplate.setHashValueSerializer(new StringRedisSerializer());
//    redisTemplate.setValueSerializer(new StringRedisSerializer());
//    redisTemplate.setConnectionFactory(factory);
//    return redisTemplate;
//  }
 
  @Bean
  public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory factory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(factory);
    return template;
  }
 
  @Bean
  public HashOperations<String, String, String> hashOperations(RedisTemplate<String, String> redisTemplate) {
    return redisTemplate.opsForHash();
  }
 
  @Bean
  public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
    return redisTemplate.opsForValue();
  }
 
  @Bean
  public SetOperations<String, String> setOperations(RedisTemplate<String, String> redisTemplate) {
    return redisTemplate.opsForSet();
  }
 
  @Bean
  public ListOperations<String, String> listOperations(RedisTemplate<String, String> redisTemplate) {
    return redisTemplate.opsForList();
  }
}

看完上述內(nèi)容,是不是對Spring boot集成redis lettuce的用法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI