1. 程式人生 > 程式設計 >Spring boot整合redis lettuce程式碼例項

Spring boot整合redis lettuce程式碼例項

spring boot框架中已經集成了redis,在1.x.x的版本時預設使用的jedis客戶端,現在是2.x.x版本預設使用的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伺服器地址
spring.redis.host=127.0.0.1
#Redis伺服器連線埠
spring.redis.port=6379
#Redis伺服器連線密碼(預設為空)

spring.redis.password=123456
#Redis資料庫索引(預設為0)
spring.redis.database=0
##連線超時時間
spring.redis.timeout=60s

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

# Lettuce
# 連線池最大連線數(使用負值表示沒有限制)
spring.redis.lettuce.pool.max-active=8
# 連線池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.lettuce.pool.max-wait=10000
# 連線池中的最大空閒連線
spring.redis.lettuce.pool.max-idle=8
# 連線池中的最小空閒連線
spring.redis.lettuce.pool.min-idle=0
# 關閉超時時間
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();
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。