Springboot集成redislettuce的用法

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

作為一家“創(chuàng)意+整合+營銷”的成都網(wǎng)站建設(shè)機構(gòu),我們在業(yè)內(nèi)良好的客戶口碑。創(chuàng)新互聯(lián)公司提供從前期的網(wǎng)站品牌分析策劃、網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、創(chuàng)意表現(xiàn)、網(wǎng)頁制作、系統(tǒng)開發(fā)以及后續(xù)網(wǎng)站營銷運營等一系列服務(wù),幫助企業(yè)打造創(chuàng)新的互聯(lián)網(wǎng)品牌經(jīng)營模式與有效的網(wǎng)絡(luò)營銷方法,創(chuàng)造更大的價值。

spring boot框架中已經(jīng)集成了redis,在1.x.x的版本時默認使用的jedis客戶端,現(xiàn)在是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服務(wù)器地址
spring.redis.host=127.0.0.1
#Redis服務(wù)器連接端口
spring.redis.port=6379
#Redis服務(wù)器連接密碼(默認為空)
spring.redis.password=123456
#Redis數(shù)據(jù)庫索引(默認為0)
spring.redis.database=0
##連接超時時間
spring.redis.timeout=60s

# 以下連接池已在SpringBoot2.0不推薦使用
##連接池最大連接數(shù)(使用負值表示沒有限制)
#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
# 連接池最大連接數(shù)(使用負值表示沒有限制)
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
# 關(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的用法有進一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前名稱:Springboot集成redislettuce的用法
網(wǎng)頁地址:http://muchs.cn/article14/gjgige.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站品牌網(wǎng)站建設(shè)、云服務(wù)器、響應(yīng)式網(wǎng)站建站公司、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司